diff --git a/src/core/qgsgeometry.cpp b/src/core/qgsgeometry.cpp index e07f3a6488e5..c41a9dcd0fae 100644 --- a/src/core/qgsgeometry.cpp +++ b/src/core/qgsgeometry.cpp @@ -2453,7 +2453,7 @@ double QgsGeometry::closestSegmentWithContext( if ( index > 0 ) { - if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist ) + if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist ) { closestSegmentIndex = index; sqrDist = testdist; @@ -2497,7 +2497,7 @@ double QgsGeometry::closestSegmentWithContext( } if ( prevx && prevy ) { - if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist ) + if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist ) { closestSegmentIndex = pointindex; sqrDist = testdist; @@ -2539,7 +2539,7 @@ double QgsGeometry::closestSegmentWithContext( } if ( prevx && prevy ) { - if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist ) + if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist ) { closestSegmentIndex = index; sqrDist = testdist; @@ -2587,7 +2587,7 @@ double QgsGeometry::closestSegmentWithContext( } if ( prevx && prevy ) { - if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist ) + if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist ) { closestSegmentIndex = pointindex; sqrDist = testdist; @@ -4679,102 +4679,6 @@ bool QgsGeometry::exportGeosToWkb() return FALSE; } - - - -double QgsGeometry::distanceSquaredPointToSegment( - const QgsPoint& point, - double *x1, double *y1, - double *x2, double *y2, - QgsPoint& minDistPoint ) -{ - - double nx, ny; //normal vector - - nx = *y2 - *y1; - ny = -( *x2 - *x1 ); - - double t; - t = ( point.x() * ny - point.y() * nx - *x1 * ny + *y1 * nx ) / (( *x2 - *x1 ) * ny - ( *y2 - *y1 ) * nx ); - - if ( t < 0.0 ) - { - minDistPoint.setX( *x1 ); - minDistPoint.setY( *y1 ); - } - else if ( t > 1.0 ) - { - minDistPoint.setX( *x2 ); - minDistPoint.setY( *y2 ); - } - else - { - minDistPoint.setX( *x1 + t *( *x2 - *x1 ) ); - minDistPoint.setY( *y1 + t *( *y2 - *y1 ) ); - } - - return ( minDistPoint.sqrDist( point ) ); -#if 0 - double d; - - // Proportion along segment (0 to 1) the perpendicular of the point falls upon - double t; - - - // Projection of point on the segment - double xn; - double yn; - - double segmentsqrdist = ( *x2 - *x1 ) * ( *x2 - *x1 ) + - ( *y2 - *y1 ) * ( *y2 - *y1 ); - - // QgsDebugMsg(QString("Entered with (%1, %2) (%3, %4) %5.").arg(*x1).arg(*y1).arg(*x2).arg(*y2).arg(segmentsqrdist)); - - - if ( segmentsqrdist == 0.0 ) - { - // segment is a point - xn = *x1; - yn = *y1; - } - else - { - - d = - ( point.x() - *x1 ) * ( *x2 - *x1 ) - + ( point.y() - *y1 ) * ( *y2 - *y1 ); - - t = d / segmentsqrdist; - - // Do not go beyond end of line - // (otherwise this would be a comparison to an infinite line) - if ( t < 0.0 ) - { - xn = *x1; - yn = *y1; - } - else if ( t > 1.0 ) - { - xn = *x2; - yn = *y2; - } - else - { - xn = *x1 + t * ( *x2 - *x1 ); - yn = *y1 + t * ( *y2 - *y1 ); - } - - } - - minDistPoint.set( xn, yn ); - - return ( - ( xn - point.x() ) *( xn - point.x() ) + - ( yn - point.y() ) *( yn - point.y() ) - ); -#endif //0 -} - bool QgsGeometry::convertToMultiType() { if ( !mGeometry ) diff --git a/src/core/qgsgeometry.h b/src/core/qgsgeometry.h index db908a99f67d..405ea13c45f0 100644 --- a/src/core/qgsgeometry.h +++ b/src/core/qgsgeometry.h @@ -419,14 +419,6 @@ class CORE_EXPORT QgsGeometry // Private functions - /** Squared distance from point to the given line segment - * TODO: Perhaps move this to QgsPoint - */ - double distanceSquaredPointToSegment( const QgsPoint& point, - double *x1, double *y1, - double *x2, double *y2, - QgsPoint& minDistPoint ); - /** Converts from the WKB geometry to the GEOS geometry. @return true in case of success and false else */ diff --git a/src/core/qgspoint.cpp b/src/core/qgspoint.cpp index db267c256509..ae82369ba085 100644 --- a/src/core/qgspoint.cpp +++ b/src/core/qgspoint.cpp @@ -238,3 +238,32 @@ int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const return 2; } + +double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const +{ + double nx, ny; //normal vector + + nx = y2 - y1; + ny = -( x2 - x1 ); + + double t; + t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx ); + + if ( t < 0.0 ) + { + minDistPoint.setX( x1 ); + minDistPoint.setY( y1 ); + } + else if ( t > 1.0 ) + { + minDistPoint.setX( x2 ); + minDistPoint.setY( y2 ); + } + else + { + minDistPoint.setX( x1 + t *( x2 - x1 ) ); + minDistPoint.setY( y1 + t *( y2 - y1 ) ); + } + + return ( sqrDist( minDistPoint ) ); +} diff --git a/src/core/qgspoint.h b/src/core/qgspoint.h index 139a4d158d21..bd4e6538ecb2 100644 --- a/src/core/qgspoint.h +++ b/src/core/qgspoint.h @@ -144,6 +144,8 @@ class CORE_EXPORT QgsPoint /**Returns the squared distance between this and other point*/ double sqrDist( const QgsPoint& other ) const; + double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const; + //! equality operator bool operator==( const QgsPoint &other );