Use a GraphicsPath to determine whether a point lies inside a polygon in C#

point lies inside a polygon

The example Determine whether a point is inside a polygon in C# shows a mathematical technique for telling if a point lies inside a polygon. This example uses .NET’s GraphicsPath class to do the same thing.

The following PointIsInPolygon method wraps up a call to a GraphicsPath object’s IsVisible method.

// Return true if the point is inside the polygon.
private bool PointIsInPolygon(Point[] polygon, Point target_point)
    // Make a GraphicsPath containing the polygon.
    GraphicsPath path = new GraphicsPath();

    // See if the point is inside the path.
    return path.IsVisible(target_point);            

This code creates a GraphicsPath object and adds the polygon to it. It then calls the object’s IsVisible method to see whether the point lies within the polygon.

The program’s MouseMove event handler shown in the following code uses PointIsInPolygon to change the form’s cursor to a cross hair when the mouse is over the polygon.

// Set the cursor depending on whether
// the mouse is over the polygon.
private void Form1_MouseMove(object sender, MouseEventArgs e)
    Cursor new_cursor;
    if (PointIsInPolygon(Points, e.Location))
        new_cursor = Cursors.Cross;
    else new_cursor = Cursors.Default;

    // Update the cursor.
    if (this.Cursor != new_cursor) this.Cursor = new_cursor;

If the PointIsInPolygon method indicates the point lies inside the polygon, the code sets new_cursor to the cross cursor. Otherwise it sets new_cursor to the default cursor. Then if new_cursor is different from the form’s current cursor, the code sets the form’s cursor equal to new_cursor.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, geometry, graphics, mathematics and tagged , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to Use a GraphicsPath to determine whether a point lies inside a polygon in C#

  1. Martn Roberge says:

    Can’t use the exemple. Properties folder is empty. I use visual Studio 2015

    • RodStephens says:

      It works for me. I don’t know why it would fail for you. Did you perhaps not decompress the downloaded zip file? I get a different error if I open the zip file in Windows Explorer and try to open the .sln file, but that’s the most obvious thing I can think of.

      The example was built using VS 2008. If you download and decompress it, and then open the .sln file in VS 2015, it should ask if you want to make a one-way upgrade to VS 2015. If you say yes, Visual Studio should create a Properties directory with files in it and it should run.

      • Martn Roberge says:

        I found the problem and fix-it. When Visual studio 2015 rebuild the solution, Properties folder are not copied. So I have to copy all missig file manualy. Your exemple work great Thank a lot for this

Leave a Reply

Your email address will not be published. Required fields are marked *