Point in Polygon, Ray Method: ending infinite line
Posted
by
user2878528
on Game Development
See other posts from Game Development
or by user2878528
Published on 2014-06-06T00:56:50Z
Indexed on
2014/06/06
3:41 UTC
Read the original article
Hit count: 232
Having a bit of trouble with point in polygon collision detection using the ray method
i.e. http://en.wikipedia.org/wiki/Point_in_polygon
My problem is I need to give an end to the infinite line created.
As with this infinite line I always get an even number of intersections and hence an invalid result.
i.e. ignore or intersection to the right of the point being checked
what I have
what I want
My current code based of Mecki awesome response
for (int side = 0; side < vertices.Length; side++)
{
// Test if current side intersects with ray.
// create infinite line
// See: http://en.wikipedia.org/wiki/Linear_equation
a = end_point.Y - start_point.Y;
b = start_point.X - end_point.X;
c = end_point.X * start_point.Y - start_point.X * end_point.Y;
//insert points of vector
d2 = a * vertices[side].Position.X + b * vertices[side].Position.Y + c;
if (side - 1 < 0)
d1 = a * vertices[vertices.Length - 1].Position.X + b * vertices[vertices.Length - 1].Position.Y + c;
else
d1 = a * vertices[side-1].Position.X + b * vertices[side-1].Position.Y + c;
// If points have opposite sides, intersections++;
if (d1 > 0 && d2 < 0 )
intersections++;
if (d1 < 0 && d2 > 0 )
intersections++;
}
//if intersections odd inside = true
if ((intersections % 2) == 1)
inside = true;
else
inside = false;
© Game Development or respective owner