# Monthly Archives: July 2014

## Find a number’s prime factors in C#

Once upon a time, I read an article where the author said something like this: My professor asked us whether we had prime factored our Social Security numbers yet. Being a normal person, I laughed with everyone else. Being a … Continue reading

Posted in algorithms, mathematics | | 5 Comments

## Perform geometric operations on polygons in C#

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 – … Continue reading

## Find a minimal bounding rectangle for a polygon in C#

This program assumes that its Points array contains the points in a convex polygon in counterclockwise order. If you have some other group of points, first find their convex hull and then orient the resulting polygon counterclockwise. See these related … Continue reading

## Triangulate a polygon in C#

Triangulating a polygon means breaking it into triangles. This is easy for simply shapes such as rectangles. It’s even easy for convex polygons. It’s more interesting for non-convex polygons. To triangulate a polygon, first define an ear to be a … Continue reading

## Find the centroid of a polygon in C#

The centroid of a polygon is its “center of mass.” If you were to cut the polygon out of cardboard or wood, the centroid would be the point where you could balance the polygon on a pin. Note that the … Continue reading

## Reverse the orientation of a polygon in C#

Reversing the orientation of a polygon is easy. Simply reverse the order of its points. If the points are stored in an array of Point or PointF, you can just call the Array.Reverse method. The Polygon class’s OrientPolygonClockwise method shown … Continue reading

## Determine whether a polygon is oriented clockwise or counterclockwise in C#

The post Calculate the area of a polygon in C# explains how to calculate the “signed area” of a polygon. That Polygon class’s SignedPolygonArea method returns a positive area if the polygon is oriented clockwise and a negative area if … Continue reading

## Calculate the area of a polygon in C#

You can calculate the area of a polygon by adding the areas of the trapezoids defined by the polygon’s edges dropped to the X-axis. If two adjacent points along the polygon’s edges have coordinates (x1, y1) and (x2, y2) as … Continue reading

## Determine whether a point is inside a polygon in C#

One way to determine whether a point lies within a polygon is to add up the angles between the point and adjacent points on the polygon taken in order. For example, if the point in question is P and points … Continue reading

Posted in algorithms, geometry, graphics, mathematics | | 18 Comments

## Determine whether a polygon is convex in C#

To see if a polygon is convex, calculate the angles at each of the polygon’s corners. If all of the angles have the same sign (either positive or negative depending on the orientation), then the polygon is convex. Rather than … Continue reading

Posted in algorithms, geometry, graphics, mathematics | | 10 Comments