1. Angle and discriminant method (for freeform polygon)
Double angle = 0;
Realpointlist::iterator iter1 = Points.begin ();
for (Realpointlist::iterator Iter2 = (iter1 + 1); Iter2 < Points.end (); ++iter1, ++iter2)
{
Double x1 = (*iter1). x-p.x;
Double y1 = (*iter1). y-p.y;
Double x2 = (*iter2). x-p.x;
Double y2 = (*iter2). y-p.y;
Angle + = angle2d (x1, y1, x2, y2);
}
if (Fabs (angle-span::P I2) < 0.01) return true;
else return false;
Alternatively, you can use bounding box to speed up.
if (P.x < (*iter)->boundingbox.left | |
p.x > (*iter)->boundingbox.right | |
P.y < (*iter)->boundingbox.bottom | |
P.y > (*iter)->boundingbox.top) ...
For polygons, calculating bounding box is very simple. You just have to find the maximum minimum in the horizontal and vertical directions.
1. Angle and discriminant method (for freeform polygon)
Double angle = 0;
Realpointlist::iterator iter1 = Points.begin ();
for (Realpointlist::iterator Iter2 = (iter1 + 1); Iter2 < Points.end (); ++iter1, ++iter2)
{
Double x1 = (*iter1). x-p.x;
Double y1 = (*iter1). y-p.y;
Double x2 = (*iter2). x-p.x;
Double y2 = (*iter2). y-p.y;
Angle + = angle2d (x1, y1, x2, y2);
}
if (Fabs (angle-span::P I2) < 0.01) return true;
else return false;
Alternatively, you can use bounding box to speed up.
if (P.x < (*iter)->boundingbox.left | |
p.x > (*iter)->boundingbox.right | |
P.y < (*iter)->boundingbox.bottom | |
P.y > (*iter)->boundingbox.top) ...
For polygons, calculating bounding box is very simple. You just have to find the maximum minimum in the horizontal and vertical directions.
For triangles: 4th respectively, the angle of the intersection of the two points of the triangle is set to j1,j2,j3, as long as the j1+j2+j3>360 is not in the triangular range.
2. Horizontal/vertical intersection points of discrimination (for freeform)
Note that if p is a horizontal-left ray, if it is inside the polygon, then the intersection of the Ray and the polygon must be odd, and if p is outside the polygon, the number of intersections must be even (0 also). So, we can take each edge of the polygon in order to find the total number of intersections. There are special circumstances to consider. If you consider the side (P1,P2),
1 if the rays are just passing through P1 or P2, then this intersection will be counted 2 times, if P's coordinates are the same as the smaller ordinate in p1,p2, it is ignored directly.
2 If the ray level, then the ray either with no intersection, or countless, this situation is also directly ignored.
3 If the radial vertical, and P0 of the horizontal axis is smaller than the p1,p2 of the horizontal axis, it will inevitably intersect.
4 before you judge the intersection, judge whether P is on the top of the Edge (P1,P2), and if so, draw the conclusion directly: P and polygon inside.
For triangles: 4th respectively, the angle of the intersection of the two points of the triangle is set to j1,j2,j3, as long as the j1+j2+j3>360 is not in the triangular range.
2. Horizontal/vertical intersection points of discrimination (for freeform)
Note that if p is a horizontal-left ray, if it is inside the polygon, then the intersection of the Ray and the polygon must be odd, and if p is outside the polygon, the number of intersections must be even (0 also). So, we can take each edge of the polygon in order to find the total number of intersections. There are special circumstances to consider. If you consider the side (P1,P2),
1 if the rays are just passing through P1 or P2, then this intersection will be counted 2 times, if P's coordinates are the same as the smaller ordinate in p1,p2, it is ignored directly.
2 If the ray level, then the ray either with no intersection, or countless, this situation is also directly ignored.
3 If the radial vertical, and P0 of the horizontal axis is smaller than the p1,p2 of the horizontal axis, it will inevitably intersect.
4 before you judge the intersection, judge whether P is on the top of the Edge (P1,P2), and if so, draw the conclusion directly: P and polygon inside.
==================================================================================
int Cutility::isptongon (const acgepoint2darray& ptarr,acgepoint2d& PT)
{
if (Ptarr.length () <=2) return 0;
Whether the sentence is on line ===================================================
acgecurve2d* Pcurve = (acgecurve2d*) new acgepolyline2d (Ptarr);
if (Pcurve->ison (PT))
{
Delete Pcurve;
Pcurve= NULL;
return 1;
}
Delete Pcurve;
Pcurve = NULL;
Triangular ===========================================================
if (ptarr.length () = = 3)
{
Acgevector2d A,b,c;
A = ptarr[0]-pt;
b = ptarr[1]-pt;
c = ptarr[2]-pt;
Double angle = A.angleto (b);
Angle + + B.angleto (c);
Angle + + C.angleto (a);
if (Fabs (angle-pi*2.0) < 0.001) return 1;
return 0;
}
//Polygon ===============================================================
//whether outside extend
ACGEPOINT2D Ptmin,ptmax;
findextinptarr (Ptarr,ptmin,ptmax);
if (pt.x-ptmin.x < 0.0001 | | pt.x-ptmax.x > 0.0001) return 0;
if (Pt.y-ptmin.y < 0.0001 | | pt.y-ptmax.y > 0.0001) return 0;
The angle and =2pi of points to each vertex
Acgevector3darray Varr;
Acgevector3d pref;
for (int i=0;i<ptarr.length (); i++)
{
Acgevector3d Pvec = Acgepoint3d (ptarr[i].x,ptarr[i].y,0)-Acgepoint3d (pt.x,pt.y,0);
Varr.append (Pvec);
}
Double angle = 0.0;
int j=0;
for (int i=0;i<varr.length () -1;i++)
{
Pref = Varr[i].crossproduct (varr[i+1]);
Pref.normalize ();
Angle + +-pref.z * Varr[i].angleto (varr[i+1));
j + +;
}
Pref = Varr[j].crossproduct (varr[0]);
Pref.normalize ();
Angle + +-pref.z * Varr[j].angleto (VARR[0],PREF);
Double sumangle = pi*2.0;
if (fabs fabs (angle)-Sumangle) < 0.001) return 1;
else return 0;
}