public List<IntPoint> OptimizeShape( List<IntPoint> shape )
{
// optimized shape
List<IntPoint> optimizedShape = new List<IntPoint>( );
if ( shape.Count <= 3 )
{
// do nothing if shape has 3 points or less
optimizedShape.AddRange( shape );
}
else
{
float angle = 0;
// add first 2 points to the new shape
optimizedShape.Add( shape[0] );
optimizedShape.Add( shape[1] );
int pointsInOptimizedHull = 2;
for ( int i = 2, n = shape.Count; i < n; i++ )
{
// add new point
optimizedShape.Add( shape[i] );
pointsInOptimizedHull++;
// get angle between 2 vectors, which start from the next to last point
angle = GeometryTools.GetAngleBetweenVectors( optimizedShape[pointsInOptimizedHull - 2],
optimizedShape[pointsInOptimizedHull - 3], optimizedShape[pointsInOptimizedHull - 1] );
if ( ( angle > maxAngleToKeep ) &&
( ( pointsInOptimizedHull > 3 ) || ( i < n - 1 ) ) )
{
// remove the next to last point
optimizedShape.RemoveAt( pointsInOptimizedHull - 2 );
pointsInOptimizedHull--;
}
}
if ( pointsInOptimizedHull > 3 )
{
// check the last point
angle = GeometryTools.GetAngleBetweenVectors( optimizedShape[pointsInOptimizedHull - 1],
optimizedShape[pointsInOptimizedHull - 2], optimizedShape[0] );
if ( angle > maxAngleToKeep )
{
optimizedShape.RemoveAt( pointsInOptimizedHull - 1 );
pointsInOptimizedHull--;
}
if ( pointsInOptimizedHull > 3 )
{
// check the first point
angle = GeometryTools.GetAngleBetweenVectors( optimizedShape[0],
optimizedShape[pointsInOptimizedHull - 1], optimizedShape[1] );
if ( angle > maxAngleToKeep )
{
optimizedShape.RemoveAt( 0 );
}
}
}
}
return optimizedShape;
}
}