geometry: revert SHAPE_LINE_CHAIN::PointInside/PointOnEdge() optimizations so that it's consistent with other collision checking methods (i.e. SEG::Distance() )

Fixes: lp:1810935
* https://bugs.launchpad.net/kicad/+bug/1810935
This commit is contained in:
Tomasz Włostowski 2019-01-28 13:24:22 +01:00
parent fd0381ea09
commit 4a0fba309a
1 changed files with 21 additions and 33 deletions

View File

@ -350,31 +350,26 @@ int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP ) const
bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const
{ {
if( !m_closed || PointCount() < 3 || !BBox().Contains( aP ) ) if( !m_closed || SegmentCount() < 3 )
return false; return false;
bool inside = false; int cur = CSegment( 0 ).Side( aP );
/** if( cur == 0 )
* To check for interior points, we draw a line in the positive x direction from return false;
* the point. If it intersects an even number of segments, the point is outside the
* line chain (it had to first enter and then exit). Otherwise, it is inside the chain. for( int i = 1; i < SegmentCount(); i++ )
*
* Note: slope might be denormal here in the case of a horizontal line but we require our
* y to move from above to below the point (or vice versa)
*/
for( int i = 0; i < PointCount(); i++ )
{ {
const VECTOR2D p1 = CPoint( i ); const SEG s = CSegment( i );
const VECTOR2D p2 = CPoint( i + 1 ); // CPoint wraps, so ignore counts
const VECTOR2D diff = p2 - p1;
if( ( ( p1.y > aP.y ) != ( p2.y > aP.y ) ) && // if( aP == s.A || aP == s.B ) // edge does not belong to the interior!
( aP.x - p1.x < ( diff.x / diff.y ) * ( aP.y - p1.y ) ) ) // return false;
inside = !inside;
if( s.Side( aP ) != cur )
return false;
} }
return inside; return true;
} }
@ -382,26 +377,19 @@ bool SHAPE_LINE_CHAIN::PointOnEdge( const VECTOR2I& aP ) const
{ {
if( !PointCount() ) if( !PointCount() )
return false; return false;
else if( PointCount() == 1 ) else if( PointCount() == 1 )
return m_points[0] == aP; return m_points[0] == aP;
for( int i = 0; i < PointCount(); i++ ) for( int i = 0; i < SegmentCount(); i++ )
{ {
const VECTOR2I& p1 = CPoint( i ); const SEG s = CSegment( i );
const VECTOR2I& p2 = CPoint( i + 1 );
if( aP == p1 ) if( s.A == aP || s.B == aP )
return true; return true;
if( p1.x == p2.x && p1.x == aP.x && ( p1.y > aP.y ) != ( p2.y > aP.y ) ) if( s.Distance( aP ) <= 1 )
return true; return true;
const VECTOR2D diff = p2 - p1;
if( aP.x >= p1.x && aP.x <= p2.x )
{
if( round_nearest( p1.y + ( diff.y / diff.x ) * ( aP.x - p1.x ) ) == aP.y )
return true;
}
} }
return false; return false;