// TODO: move to geometry utils double distance2D( const QgsPolylineXY &coords ) { int np = coords.count(); if ( np == 0 ) return 0; double x0 = coords[0].x(), y0 = coords[0].y(); double x1, y1; double dist = 0; for ( int i = 1; i < np; ++i ) { x1 = coords[i].x(); y1 = coords[i].y(); dist += std::sqrt( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) ); x0 = x1; y0 = y1; } return dist; }
// TODO: move to geometry utils double closestSegment( const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon ) { double sqrDist = std::numeric_limits<double>::max(); const QgsPointXY *pldata = pl.constData(); int plcount = pl.count(); double prevX = pldata[0].x(), prevY = pldata[0].y(); double segmentPtX, segmentPtY; for ( int i = 1; i < plcount; ++i ) { double currentX = pldata[i].x(); double currentY = pldata[i].y(); double testDist = QgsGeometryUtils::sqrDistToLine( pt.x(), pt.y(), prevX, prevY, currentX, currentY, segmentPtX, segmentPtY, epsilon ); if ( testDist < sqrDist ) { sqrDist = testDist; vertexAfter = i; } prevX = currentX; prevY = currentY; } return sqrDist; }