This example contains a Polygon class that perform several useful geometric operations for polygons. The following list describes the key methods provided by the class. Click the links to see posts that describe the methods in greater detail.
- PolygonIsConvex – Returns true if the polygon is convex
- PointInPolygon – Returns true if a point in inside the polygon
- PolygonArea – Returns the polygon’s area
- FindCentroid – Returns the polygon’s centroid (“center of mass”)
- PolygonIsOrientedClockwise – Returns true if the polygon’s points are oriented clockwise
- OrientPolygonClockwise – Orients the polygon’s points clockwise
- Triangulate – Chops the polygon into triangles
- FindSmallestBoundingRectangle – Returns a smallest rectangle that surrounds the polygon (not necessarily parallel to the X and Y axes)
To use the example, click to add points to the polygon. Then use function keys or the menu to perform the operations.
The “Point in Polygon” test is a bit odd because it uses the mouse’s current position as the point you want to test. If you use the menu item to perform the test, the point is wherever the menu item is. To test points without using the menu, press F3.
Note also that the Bounding Rectangle operation only works for convex polygons.