Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3425444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:35:41+00:00 2026-05-18T06:35:41+00:00

I need to create an extension method for a GraphicsPath object that determines if

  • 0

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.

Fillmode Winding vs Alternate

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:35:41+00:00Added an answer on May 18, 2026 at 6:35 am

    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.

    [Test]
    public void Test_DetermineWindingDirection()
    {
    
        GraphicsPath path = new GraphicsPath();
    
        // Set up points collection
        PointF[] pts = new[] {new PointF(10, 60),
                        new PointF(50, 110),
                        new PointF(90, 60)};
        path.AddLines(pts);
    
        foreach(var point in path.PathPoints)
        {
            Console.WriteLine("X: {0}, Y: {1}",point.X, point.Y);
        }
    
        WindingDirection windingVal = DetermineWindingDirection(path.PathPoints);
        Console.WriteLine("Winding value: {0}", windingVal);
        Assert.AreEqual(WindingDirection.Clockwise, windingVal);
    
        path.Reverse();
        foreach(var point in path.PathPoints)
        {
            Console.WriteLine("X: {0}, Y: {1}",point.X, point.Y);
        }
    
        windingVal = DetermineWindingDirection(path.PathPoints);
        Console.WriteLine("Winding value: {0}", windingVal);
        Assert.AreEqual(WindingDirection.CounterClockWise, windingVal);
    }
    
    public enum WindingDirection
    {
        Clockwise,
        CounterClockWise
    }
    
    public static WindingDirection DetermineWindingDirection(PointF[] polygon)
    {
        // find a point in the middle
        float middleX = polygon.Average(p => p.X);
        float middleY = polygon.Average(p => p.Y);
        var pointInPolygon = new PointF(middleX, middleY);
        Console.WriteLine("MiddlePoint = {0}", pointInPolygon);
    
        double w = 0;
        var points = polygon.Select(point =>
                                        { 
                                            var newPoint = new PointF(point.X - pointInPolygon.X, point.Y - pointInPolygon.Y);
                                            Console.WriteLine("New Point: {0}", newPoint);
                                            return newPoint;
                                        }).ToList();
    
        for (int i = 0; i < points.Count; i++)
        {
            var secondPoint = i + 1 == points.Count ? 0 : i + 1;
            double X = points[i].X;
            double Xp1 = points[secondPoint].X;
            double Y = points[i].Y;
            double Yp1 = points[secondPoint].Y;
    
            if (Y * Yp1 < 0)
            {
                double r = X + ((Y * (Xp1 - X)) / (Y - Yp1));
                if (r > 0)
                    if (Y < 0)
                        w = w + 1;
                    else
                        w = w - 1;
            }
            else if ((Y == 0) && (X > 0))
            {
                if (Yp1 > 0)
                    w = w + .5;
                else
                    w = w - .5;
            }
            else if ((Yp1 == 0) && (Xp1 > 0))
            {
                if (Y < 0)
                    w = w + .5;
                else
                    w = w - .5;
            }
        }
        return w > 0 ? WindingDirection.ClockWise : WindingDirection.CounterClockwise;
    }
    

    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

    if (i + 1 == points.Count)
        // use points[0] instead
    

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need an enum or something similiar to do something like this: public enum
Background : I need to create a custom like extension method for linqtohiberante to
I need a way to create an extension method off of an IEnumerable that
I need to create an extension method to array class, but this extension method
So I have this method that get executed repeatedly public static boolean isReady(String dirPath,
I am trying to create an extension method that i can forward a IList
How I can create an extension method like Html.DisplayTextFor in Asp.net mvc ? I
I'm trying to create a generic extension method, that works on typed data tables
This may seem a bit odd, but I really need to create a workaround
i have a data structure like this public class Employee { public string Name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.