I need to create an extension method for a GraphicsPath object that determines if the GraphicsPath is wound clockwise or counter-clockwise.
Something like this:
public static bool IsClockwise(GraphicsPath gp)
{
bool bClockWise = false;
PointF[] pts = gp.PathPoints;
foreach (PointF pt in pts)
{
//??
}
return bClockWise;
}
Note: I just assume that we’d need a foreach on the points in the GraphicsPath here, but if there is a better way, please feel free to offer.
The reason for this is because the FillMode of the GraphicsPath determines if overlapping sections of adjacent GraphicsPaths are ‘filled’ (Winding) or ‘holes’ (Alternate). However, this is only true if the adjacent GraphicsPaths are wound in the same direction.
If the two paths are wound in opposing directions, even setting the FillMode to Winding results in the (unwanted) holes.
Interestingly enough, the GraphicsPath DOES have a .Reverse() method that allows one to change the direction of the path (and this DOES solve the problem), but it is impossible to know WHEN the GraphicsPath needs to be .Reversed without knowing the direction of the path.
Can you help out with this extension method?
I’m mostly throwing this out there because this question caught my interest, it was something I have no expertise in, and I’d like to generate discussion.
I took the Point in Polygon pseudo-code and tried to make it fit your situation. It seemed you are only interested in determining if something is winding clockwise or counter-clockwise. Here is a simple test and a very simple (rough around the edges) C# implementation.
The difference that I’ve put in that makes this a bit specific to your problem is that I’ve calculated the average values of X and Y from all points and use that as the “point in polygon” value. So, passing in just an array of points, it will find something in the middle of them.
I’ve also made the assumption that the V i+1 should wrap around when it gets to the bounds of the points array so that
This hasn’t been optimized, it’s just something to potentially answers your question. And perhaps you’ve already come up with this yourself.
Here’s hoping for constructive criticism. 🙂