12,348,846 members (64,340 online)
Tip/Trick
alternative version

29.5K views
17 bookmarked
Posted

# Check if a Point is Inside the Polygon Using Ray-Casting Algorithm

, 29 Jul 2013 CPOL
 Rate this:
An implementation of ray-casting algorithm to check if a given point is inside or outside the polygon.

## Introduction

There are many applications that require to know if a given point is inside or outside the polygon. For an example, if we need to select a polygon by clicking on it then we will need a way to know whether the user click inside or outside the polygon. Ray casting algorithm is one popular way to do it. In this trick I included an implementation of raycasting algorithm for a polygon selection in a canvas.

## Background

In this section I briefly explain how the ray casting algorithm can be used for check whether a point is inside or outside the polygon. When a point is given then we virtually draw a line from a point far away outside from the polygon to the given point. then we calculate the number of intersection of the virtual line with the edges of the polygon. if the number of intersections is odd then according to the ray casting theory we can conclude that the point is inside the polygon. Otherwise the point is outside the polygon. You can find more about ray casting algorithm from here.

## Using the code

In this code there are two main functionalities. One is drawing of a polygon inside a usercontrol and second is select of deselect the polygon. I am not going to explain the drawing part of the polygon since the every line in the code is explained with a comment.

The following code is used for check the intersections

``` //check the intersections
if (((polyK.Y > currentPoint.Y) != (polyJ.Y > currentPoint.Y)) &&
(currentPoint.X < (polyJ.X - polyK.X) * (currentPoint.Y - polyK.Y) / (polyJ.Y - polyK.Y) + polyK.X)) ```

polyK and polyJ are adjacent points from the polygon.

According to the algorithm all what we need to know is whether the number of intersections is odd or even hence we use a flag that switches between odd and even as in the following code line.

`oddNodes = !oddNodes; `

After testing the each and every edge of the polygon for the intersection we can check whether the number of intersection is odd or even. If it is odd then the point should be inside the polygon so we flag it as selected as in the following code.

```if (oddNodes)
{
//mouse point is inside the polygon
isSelected = true;
}
else //if even number of intersections
{
//mouse point is outside the polygon so deselect the polygon
isSelected = false;
}```

## Share

 Student University of Moratuwa Sri Lanka
PhD student specializing in video content analysis. Interested in Image processing, HCI and Digital music production. Computer and technology enthusiast. Love coding and sharing my knowledge.

## You may also be interested in...

 First Prev Next
 Thanks Member 1058266418-Apr-16 1:20 Member 10582664 18-Apr-16 1:20
 Re: Thanks Ravimal Bandara23-Apr-16 7:44 Ravimal Bandara 23-Apr-16 7:44
 Nice Article but not seem to work in few cases Learning WIX7-Jan-15 19:46 Learning WIX 7-Jan-15 19:46
 Re: Nice Article but not seem to work in few cases Ravimal Bandara8-Jan-15 5:53 Ravimal Bandara 8-Jan-15 5:53
 Re: Nice Article but not seem to work in few cases Learning WIX8-Jan-15 16:03 Learning WIX 8-Jan-15 16:03
 Re: Nice Article but not seem to work in few cases Ravimal Bandara9-Jan-15 4:42 Ravimal Bandara 9-Jan-15 4:42
 My vote of 5 Galatei30-Jul-13 10:10 Galatei 30-Jul-13 10:10
 My vote of 1 Galatei27-Jul-13 2:48 Galatei 27-Jul-13 2:48
 Re: My vote of 1 Ravimal Bandara27-Jul-13 4:47 Ravimal Bandara 27-Jul-13 4:47
 Re: My vote of 1 Galatei28-Jul-13 10:07 Galatei 28-Jul-13 10:07
 Re: My vote of 1 Ravimal Bandara28-Jul-13 17:00 Ravimal Bandara 28-Jul-13 17:00
 Re: My vote of 1 Galatei29-Jul-13 10:34 Galatei 29-Jul-13 10:34
 Re: My vote of 1 Ravimal Bandara29-Jul-13 21:27 Ravimal Bandara 29-Jul-13 21:27
 My vote of 5 Amir Mohammad Nasrollahi26-Jul-13 20:33 Amir Mohammad Nasrollahi 26-Jul-13 20:33
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jun-16 14:50 Refresh 1