0% found this document useful (0 votes)
782 views

Ray Casting Algorithm

This algorithm uses ray casting to determine if a point is inside or outside a polygon. It works by drawing a ray from the point in question and counting the number of times the ray intersects edges of the polygon. If the number of intersections is even, the point is outside the polygon. If it is odd, the point is inside. The algorithm follows specific rules when determining if an edge intersection occurs and calculates the x-coordinate of the intersection point to determine if it is to the right of the test point. It implements these steps to cast a ray, count intersections, and return 1 if the point is inside or 0 if it is outside the polygon.

Uploaded by

Sagarnil Nandy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
782 views

Ray Casting Algorithm

This algorithm uses ray casting to determine if a point is inside or outside a polygon. It works by drawing a ray from the point in question and counting the number of times the ray intersects edges of the polygon. If the number of intersections is even, the point is outside the polygon. If it is odd, the point is inside. The algorithm follows specific rules when determining if an edge intersection occurs and calculates the x-coordinate of the intersection point to determine if it is to the right of the test point. It implements these steps to cast a ray, count intersections, and return 1 if the point is inside or 0 if it is outside the polygon.

Uploaded by

Sagarnil Nandy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

RAY CASTING ALGORITHM

Fig1 :- A Polygon

One simple way of finding whether the point is inside or outside a simple
polygon is to test how many times a ray starting from the point intersects
the edges of the polygon. If the point in question is not on the boundary of
the polygon, the number of intersections is an even number if the point is
outside, and it is odd if inside. This algorithm is sometimes also known as
the crossing number algorithm or the even-odd rule algorithm. Now
why the name even-odd rule algorithm?? If we see the fig1 carefully then
there are two points in and out. Our first task is to draw a straight line to
the right of the point till it crosses the boundary of the polygon possibly
several times. Now if the point under consideration is outside the polygon
then the first it will go inside the polygon and then again out and
ultimately outside the polygon which signifies that cn is even where cn
counts the number of times a ray starting from the point P crosses the
polygon boundary edges. Similarly when a point is inside the polygon then
its next phase will be outside the polygon and several inside and outside
and ultimately outside which signifies that cn is odd since it started with
outside and ended with outside so it is quite obvious that cn will be odd.
Due to the even and odd calculation this algorithm is known as Even-Odd
Rule Algorithm.
There are some rules which has to be followed during the design of the
algorithm. These are:
1. An upward edge includes its starting endpoint, and excludes its final
endpoint.
2. A downward edge excludes its starting endpoint, and includes its final
endpoint.
3. Horizontal edges are excluded.
4. The edge-ray intersection point must be strictly right of the point P.

int point_in_poly(int N, int tgtx, int tgty)


{
int counter = 0;
int i;
double xintrsct;
int a,b,c,d;
a = x[0];
b = y[0];
for (i=1; i <=N; i++) {
c = x[i % N];
d = y[i % N];
if (tgty > (b < d ? b : d)) { // Downward Crossing.
if (tgty <= (b > d ? b : d)) { // Upward Crossing.
if (tgtx <= (a > c ? a : c)) { // Line Extended To The Right.
if (b != d) {
// compute the actual edge-ray intersect x-coordinate
xintrsct = (tgty-b) * (c-a) / (d-b) +a;
if (tgtx <= xintrsct)
counter++;
}
}
}
}
a=c;
b=d;
}
if (counter % 2 == 0)
return(0); // Outside The Polygon.
else
return(1); // Inside The Polygon.
}

In this algorithm tgtx and tgty is the x and y coordinate of the point under
consideration. The variables a, b, c, d hold the coordinates of the polygon.
Now after that all the Edge Crossing Rules are verified then we form the
equation of the line. Our aim is to draw a single straight line parallel to x-
axis. Now when it is parallel to x-axis so its y-axis is same as the y
coordinate of the point under consideration. Now our next task is to find
the point of intersection i.e. to find the x coordinate of the point of
intersection. Now we have to check that whether this x coordinate is
having a value greater than the point under consideration, if it is then it is
discarded otherwise accepted and the variable counter is incremented by
1. Now when all the points are traversed then we check whether count is
even or odd. If it is even then it is outside the polygon otherwise it is
inside. Since our aim is to check that whether a point is inside or outside a
polygon so we return 1 if it is inside the polygon and 0 if outside the
polygon.

You might also like