locked
Determine if the point is in the polygon, C# RRS feed

  • Question

  • Hi,
    I have created some points and then I used e.Graphics.FillPolygon(Brushes.Blue,Point);
    How could i find out if the e.X and e.Y are in the polygon.

    Thanks
    Sunday, December 9, 2007 4:02 PM

Answers

  • Converted C++ example

    Code Block

    static bool PointInPolygon(Point p, Point[] poly)

    {

    Point p1, p2;

     

    bool inside = false;

     

    if (poly.Length < 3)

    {

    return inside;

    }

     

    Point oldPoint = new Point(

    poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

     

    for (int i = 0; i < poly.Length; i++)

    {

    Point newPoint = new Point(poly[i].X, poly[i].Y);

     

    if (newPoint.X > oldPoint.X)

    {

    p1 = oldPoint;

    p2 = newPoint;

    }

    else

    {

    p1 = newPoint;

    p2 = oldPoint;

    }

     

    if ((newPoint.X < p.X) == (p.X <= oldPoint.X)

    && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X)

     < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))

    {

    inside = !inside;

    }

     

    oldPoint = newPoint;

    }

     

    return inside;

    }

     

     

    --

    Regards,

    Daniel Kuppitz

    Sunday, December 9, 2007 4:29 PM

All replies

  • Converted C++ example

    Code Block

    static bool PointInPolygon(Point p, Point[] poly)

    {

    Point p1, p2;

     

    bool inside = false;

     

    if (poly.Length < 3)

    {

    return inside;

    }

     

    Point oldPoint = new Point(

    poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

     

    for (int i = 0; i < poly.Length; i++)

    {

    Point newPoint = new Point(poly[i].X, poly[i].Y);

     

    if (newPoint.X > oldPoint.X)

    {

    p1 = oldPoint;

    p2 = newPoint;

    }

    else

    {

    p1 = newPoint;

    p2 = oldPoint;

    }

     

    if ((newPoint.X < p.X) == (p.X <= oldPoint.X)

    && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X)

     < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))

    {

    inside = !inside;

    }

     

    oldPoint = newPoint;

    }

     

    return inside;

    }

     

     

    --

    Regards,

    Daniel Kuppitz

    Sunday, December 9, 2007 4:29 PM
  • Thank you for the code.  

    Wednesday, November 19, 2008 10:27 PM
  • Thanks for the code, i know im a little late... but better late then never right?

    I forgot to say thanks then, so i am doing it now. Thanx again
    Saturday, December 6, 2008 6:05 PM
  • I'd like to thank you for this as well, found it by searching Google and works like a charm, thanks :)
    Sunday, February 7, 2010 3:35 PM
  • Is a very good method...

     

    But I don't understand what is the fundament to that works at this way

     

    Can you explain me that please?

     

    Thanx :)

    Tuesday, March 15, 2011 12:41 AM
  • Hi,

     

    I too have used the code in this thread and was curious how it works.  So ... I had a look at it and tried to figure it out.  My best attempt at an explanation can be found at: http://conceptual-misfire.awardspace.com/point_in_polygon.htm.  I've included an app which hopefully helps with the explanation of this code.

     

    If you want to know anything else, let me know.

     

    Hope it helps

    • Proposed as answer by Adrian773 Wednesday, November 26, 2014 1:37 AM
    Monday, April 18, 2011 10:27 PM
  • What I need to do? In this case:

     

    I have a rectangle with points (x,y):

    (1,1), (3,1), (1,3), (3,3)

    And my point is (3,2), this code return OUTSIDE, but the point is in the line of two points and that for me is inside.

    So, what I need to change in the code for that? Thanks a lot.


    JavierSk8 C# .NET
    Wednesday, January 11, 2012 10:58 PM
  • Your rectangle is not really a rectangle, it is more of a bow tie. The correct coordinates are:

    (1,1), (3,1), (3,3), (1,3), 

    Thursday, March 29, 2012 12:46 PM