Navigation Menu

Skip to content

Commit

Permalink
snap to first point of new line
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@5437 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed May 11, 2006
1 parent 52625f7 commit 65fc91a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/plugins/grass/qgsgrassedit.cpp
Expand Up @@ -1140,6 +1140,49 @@ void QgsGrassEdit::snap ( QgsPoint & point )
point.setY(y);
}

void QgsGrassEdit::snap ( QgsPoint & point, double startX, double startY )
{
double x = point.x();
double y = point.y();

double thresh = threshold();

// Start
double startDist = hypot ( x-startX, y-startY);
bool startIn = false;
if ( startDist <= thresh ) startIn = true;

// Nearest node
double nodeX, nodeY;
double nodeDist;
bool nodeIn = false;
int node = mProvider->findNode ( x, y, thresh );

if ( node > 0 )
{
mProvider->nodeCoor ( node, &nodeX, &nodeY );
nodeDist = hypot ( x-nodeX, y-nodeY);
nodeIn = true;
}
std::cerr << "startIn = " << startIn << std::endl;
std::cerr << "nodeIn = " << nodeIn << std::endl;
std::cerr << "startDist = " << startDist << std::endl;
std::cerr << "nodeDist = " << nodeDist << std::endl;

// Choose
if ( (startIn && !nodeIn) || (startIn && nodeIn && startDist < nodeDist) )
{
x = startX; y = startY;
}
else if ( (!startIn && nodeIn) || (startIn && nodeIn && startDist > nodeDist) )
{
x = nodeX; y = nodeY;
}

point.setX(x);
point.setY(y);
}

void QgsGrassEdit::newPoint(void) { startTool(QgsGrassEdit::NEW_POINT); }
void QgsGrassEdit::newLine(void) {
std::cerr << "QgsGrassEdit::newLine" << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/grass/qgsgrassedit.h
Expand Up @@ -395,6 +395,8 @@ public slots:
/** Snap to nearest node in current threshold */
void snap ( QgsPoint & point );
void snap ( double *x, double *y);
/** Snap point line considering line starting point */
void snap ( QgsPoint & point, double startX, double startY);

/** Attributes */
QgsGrassAttributes *mAttributes;
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/grass/qgsgrassedittools.cpp
Expand Up @@ -151,8 +151,20 @@ void QgsGrassEditNewLine::mouseClick(QgsPoint & point, Qt::ButtonState button)
{
switch ( button ) {
case Qt::LeftButton:
e->snap ( point );
if ( e->mEditPoints->n_points > 2 )
{
e->snap ( point, e->mEditPoints->x[0], e->mEditPoints->y[0] );
}
else
{
e->snap ( point );
}
Vect_append_point ( e->mEditPoints, point.x(), point.y(), 0.0 );

// Draw
Vect_reset_line ( e->mPoints );
Vect_append_points ( e->mPoints, e->mEditPoints, GV_FORWARD );
e->displayDynamic ( e->mPoints );
break;
case Qt::MidButton:
if ( e->mEditPoints->n_points > 0 ) {
Expand Down

0 comments on commit 65fc91a

Please sign in to comment.