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,58 +350,46 @@ int SHAPE_LINE_CHAIN::PathLength( 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;
bool inside = false;
int cur = CSegment( 0 ).Side( aP );
/**
* To check for interior points, we draw a line in the positive x direction from
* 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.
*
* 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++ )
if( cur == 0 )
return false;
for( int i = 1; i < SegmentCount(); i++ )
{
const VECTOR2D p1 = CPoint( i );
const VECTOR2D p2 = CPoint( i + 1 ); // CPoint wraps, so ignore counts
const VECTOR2D diff = p2 - p1;
const SEG s = CSegment( i );
if( ( ( p1.y > aP.y ) != ( p2.y > aP.y ) ) &&
( aP.x - p1.x < ( diff.x / diff.y ) * ( aP.y - p1.y ) ) )
inside = !inside;
// if( aP == s.A || aP == s.B ) // edge does not belong to the interior!
// return false;
if( s.Side( aP ) != cur )
return false;
}
return inside;
return true;
}
bool SHAPE_LINE_CHAIN::PointOnEdge( const VECTOR2I& aP ) const
{
if( !PointCount() )
return false;
else if( PointCount() == 1 )
if( !PointCount() )
return false;
else if( PointCount() == 1 )
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 VECTOR2I& p2 = CPoint( i + 1 );
const SEG s = CSegment( i );
if( aP == p1 )
if( s.A == aP || s.B == aP )
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;
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;