From 2d8264124d65af3d8422d7522efdb57f381cca24 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Thu, 27 May 2021 23:40:25 +0200 Subject: [PATCH] geometry: SHAPE_LINE_CHAIN::PathLength() now can accept the maximum index of the segment to calculate the length to --- .../kimath/include/geometry/shape_line_chain.h | 2 +- libs/kimath/src/geometry/shape_line_chain.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libs/kimath/include/geometry/shape_line_chain.h b/libs/kimath/include/geometry/shape_line_chain.h index 2103cd8d41..64e6f1eb9a 100644 --- a/libs/kimath/include/geometry/shape_line_chain.h +++ b/libs/kimath/include/geometry/shape_line_chain.h @@ -610,7 +610,7 @@ public: * belonging to our line. * @return: path length in Euclidean metric or -1 if aP does not belong to the line chain. */ - int PathLength( const VECTOR2I& aP ) const; + int PathLength( const VECTOR2I& aP, int aIndex = -1 ) const; /** * Function CheckClearance() diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index 7ef74846a7..1b96e444a3 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -842,7 +842,7 @@ int SHAPE_LINE_CHAIN::Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& } -int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP ) const +int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP, int aIndex ) const { int sum = 0; @@ -851,7 +851,21 @@ int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP ) const const SEG seg = CSegment( i ); int d = seg.Distance( aP ); - if( d <= 1 ) + bool indexMatch = true; + + if( aIndex >= 0 ) + { + if( aIndex == SegmentCount() ) + { + indexMatch = ( i == SegmentCount() - 1 ); + } + else + { + indexMatch = ( i == aIndex ); + } + } + + if( indexMatch ) { sum += ( aP - seg.A ).EuclideanNorm(); return sum;