geometry: Area() method in SHAPE_LINE_CHAIN

This commit is contained in:
Tomasz Włostowski 2017-10-19 23:14:36 +02:00
parent d9bfbb4fec
commit d2c9a5d81a
2 changed files with 22 additions and 3 deletions

View File

@ -123,11 +123,11 @@ void SHAPE_LINE_CHAIN::Remove( int aStartIndex, int aEndIndex )
}
int SHAPE_LINE_CHAIN::Distance( const VECTOR2I& aP ) const
int SHAPE_LINE_CHAIN::Distance( const VECTOR2I& aP, bool aOutlineOnly ) const
{
int d = INT_MAX;
if( IsClosed() && PointInside( aP ) )
if( IsClosed() && PointInside( aP ) && !aOutlineOnly )
return 0;
for( int s = 0; s < SegmentCount(); s++ )
@ -612,3 +612,20 @@ const VECTOR2I SHAPE_LINE_CHAIN::PointAlong( int aPathLength ) const
return CPoint( -1 );
}
double SHAPE_LINE_CHAIN::Area() const
{
if( !m_closed )
return 0.0;
double area = 0.0;
int size = m_points.size();
for( int i = 0, j = size - 1; i < size; ++i )
{
area += ( (double) m_points[j].x + m_points[i].x ) * ( (double) m_points[j].y - m_points[i].y );
j = i;
}
return -area * 0.5;
}

View File

@ -299,7 +299,7 @@ public:
* @param aP the point
* @return minimum distance.
*/
int Distance( const VECTOR2I& aP ) const;
int Distance( const VECTOR2I& aP, bool aOutlineOnly = false ) const;
/**
* Function Reverse()
@ -602,6 +602,8 @@ public:
const VECTOR2I PointAlong( int aPathLength ) const;
double Area() const;
private:
/// array of vertices
std::vector<VECTOR2I> m_points;