From cb917e4c42e9d948adf276642fce776d746fd65b Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 26 Jul 2021 09:01:22 -0700 Subject: [PATCH] Avoid nonsensical intersections When two segments are approximately parallel, we shouldn't look to find their intersection. It will never be the nearest point and it overflows the KiROUND call Fixes https://gitlab.com/kicad/code/kicad/issues/8858 --- pcbnew/tools/pcb_grid_helper.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pcbnew/tools/pcb_grid_helper.cpp b/pcbnew/tools/pcb_grid_helper.cpp index 91121d3724..640cb72a31 100644 --- a/pcbnew/tools/pcb_grid_helper.cpp +++ b/pcbnew/tools/pcb_grid_helper.cpp @@ -88,14 +88,22 @@ VECTOR2I PCB_GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSe VECTOR2I nearest = Align( aPoint ); + SEG pos_slope( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ); + SEG neg_slope( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ); + int max_i = 2; + pts[0] = aSeg.A; pts[1] = aSeg.B; - pts[2] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ) ); - pts[3] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ) ); + + if( !aSeg.ApproxParallel( pos_slope ) ) + pts[max_i++] = aSeg.IntersectLines( pos_slope ); + + if( !aSeg.ApproxParallel( neg_slope ) ) + pts[max_i++] = aSeg.IntersectLines( neg_slope ); int min_d = std::numeric_limits::max(); - for( int i = 0; i < 4; i++ ) + for( int i = 0; i < max_i; i++ ) { if( pts[i] && aSeg.Distance( *pts[i] ) <= c_gridSnapEpsilon ) {