Original Post
I want to know if a given point is inside a polygon. That I am able to do, my problem is that I need the point to be counted even if it's slightly outside the polygon(On the edge or something like that). The method I use to find out of the point is inside the polygon is the following. Take the point and test it against each of the polgyon's edges. If the sum of the angles is 2pi then the point is outside. By calculating the normal of each edge I am able to drag the point a little bit inward and fix my problem, however, if the polygon is too small and/or too thin then the point will cross the polygon entirely :S. Edit: Nevermind problem solved, figured out that instead of moving the point I could move the edge, silly me. [Edited by - Antonym on May 27, 2009 4:35:26 PM]
bool PlayState::InsidePolygon(b2Vec2 *polygon, int n, b2Vec2 p)
{
int i;
double angle=0;
b2Vec2 p1,p2;
for(i=0;i<n;i++){
b2Vec2 edge = polygon[(i+1)%n] - polygon;
b2Vec2 normal = b2Cross(edge, 1.0f);
normal.Normalize();
normal.x *= .2;
normal.y *= .2;
b2Vec2 newPoint = p - normal;
p1.x = polygon.x - newPoint.x;
p1.y = polygon.y - newPoint.y;
p2.x = polygon[(i+1)%n].x - newPoint.x;
p2.y = polygon[(i+1)%n].y - newPoint.y;
angle += Angle2D(p1.x,p1.y,p2.x,p2.y);
}
if (abs(angle) < b2_pi)
return false;
else
return true;
}
double PlayState::Angle2D(double x1, double y1, double x2, double y2)
{
double dtheta,theta1,theta2;
theta1 = atan2(y1,x1);
theta2 = atan2(y2,x2);
dtheta = theta2 - theta1;
while (dtheta > b2_pi)
dtheta -= b2_pi*2;
while (dtheta < -b2_pi)
dtheta += b2_pi*2;
return(dtheta);
}