Judge whether a point is in the irregular image of the internal algorithm, as the irregular image composed of points, to determine whether a point in the irregular rectangle inside, first on
Algorithm is implemented as follows, the algorithm is simple, the pro-test effective
Public classPositionalgorithmhelper {/// <summary> ///determine if the current position is in an irregular shape/// </summary> /// <param name= "Nvert" >Irregular -shaped fixed-point number</param> /// <param name= "Vertx" >current x-coordinate</param> /// <param name= "Verty" >current y-coordinate</param> /// <param name= "Testx" >irregular shape x coordinate set</param> /// <param name= "testy" >irregular shape y-coordinate set</param> /// <returns></returns> Public Static BOOLPositionpnpoly (intNvert, list<Double> Vertx, list<Double> Verty,DoubleTESTX,Doubletesty) { intI, j, C =0; for(i =0, j = Nvert-1; i < Nvert; j = i++) { if(((Verty[i] > Testy)! = (Verty[j] > Testy)) && (Testx < (Vertx[j]-vertx[i]) * (Testy-verty[i])/(ver TY[J]-verty[i]) +Vertx[i])) {C=1+C;; } } ifC2==0) { return false; } Else { return true; } } }
To test with coordinates:
classProgram {Static voidMain (string[] args) {test1 (); } /// <summary> ///test1/// </summary> Public Static voidtest1 () {//Irregular image coordinatesList<position> Position =NewList<position>(); Position. ADD (NewPosition () {x =6, y =0 }); Position. ADD (NewPosition () {x =Ten, y =2 }); Position. ADD (NewPosition () {x = -, y =2 }); Position. ADD (NewPosition () {x = -, y =6 }); Position. ADD (NewPosition () {x = -, y =Ten }); Position. ADD (NewPosition () {x = -, y =6 }); Position. ADD (NewPosition () {x = A, y =6 }); Position. ADD (NewPosition () {x = -, y =8 }); Position. ADD (NewPosition () {x =Ten, y =8 }); Position. ADD (NewPosition () {x =8, y =6 }); Position. ADD (NewPosition () {x = A, y =4 }); Position. ADD (NewPosition () {x =6, y =4 }); Position. ADD (NewPosition () {x =8, y =2 }); //user's current position coordinatesList<position> userpositions =NewList<position>(); Userpositions.add (NewPosition () {x = -, y =4 }); Userpositions.add (NewPosition () {x = the, y =4 }); Userpositions.add (NewPosition () {x =Ten, y =6 }); Userpositions.add (NewPosition () {x =8, y =5 }); //irregular image x-coordinate setlist<Double> xlist = position. Select (x =x.x). ToList (); //irregular image y-coordinate setlist<Double> ylist = position. Select (x =x.y). ToList (); foreach(varUserpositioninchuserpositions) { BOOLresult =Positionalgorithmhelper.positionpnpoly (position. Count, Xlist, Ylist, userposition.x, USERPOSITION.Y); if(Result) {Console.WriteLine (string. Format ("{0},{1} "within" coordinates", userposition.x, USERPOSITION.Y)); } Else{Console.WriteLine (string. Format ("{0},{1} "not in" coordinates", userposition.x, USERPOSITION.Y)); } } } }
Two different ways:
/// <summary> ///determines whether a point is within a polygon. ///----------Principle----------///Notice that if P is in the polygon, the intersection of the ray with the polygon must be an odd number if it is a horizontal left ray from P .///if P is outside the polygon, the number of intersections must be even (0). ///Therefore, we can consider each edge of the polygon in order to find out the total number of intersections. There are some special cases to consider. If you consider the side (P1,P2),///1) If the ray passes through P1 or P2, the intersection will be counted as 2 times, if the coordinates of P are the same as the smaller ordinate in the P1,P2, the case is ignored directly .///2) If the ray level, then the Ray has no intersection, or there are countless, this situation is also directly ignored. ///3) If the ray is upright, and the horizontal axis of the P0 is less than the horizontal axis of P1,P2, it must intersect. ///4) Before judging the intersection, the first is whether P is on the Edge (P1,P2), and if so, the direct conclusion is: P re-polygon inside. /// </summary> /// <param name= "CheckPoint" >to judge the point</param> /// <param name= "polygonpoints" >Vertex of Polygon</param> /// <returns></returns> Public Static BOOLIsInPolygon2 (Position checkPoint, list<position>polygonpoints) { intCounter =0; inti; Doublexinters; Position P1, p2; intPointcount =Polygonpoints.count; P1= polygonpoints[0]; for(i =1; I <= Pointcount; i++) {P2= polygonpoints[i%Pointcount]; if(Checkpoint.y > Math.min (p1.y, P2.Y)//The y of the check point is greater than the minimum y of the segment endpoint&& checkpoint.y <= Math.max (p1.y, P2.Y))//y of the check point is less than the maximum y of the segment endpoint { if(Checkpoint.x <= Math.max (p1.x, p2.x))//The X of the check point is less than the maximum x for the end of the line segment (judging by the left ray of the check point). { if(P1.y! = p2.y)//line segments are uneven on the x-axis{xinters= (CHECKPOINT.Y-P1.Y) * (p2.x-p1.x)/(P2.Y-P1.Y) +p1.x; if(p1.x = = p2.x | | checkpoint.x <=xinters) {Counter++; } }}} P1=P2; } if(Counter%2==0) { return false; } Else { return true; } } /// <summary> ///determines whether a point is within a polygon. ///----------Principle----------///Notice that if P is in the polygon, the intersection of the ray with the polygon must be an odd number if it is a horizontal left ray from P .///if P is outside the polygon, the number of intersections must be even (0). /// </summary> /// <param name= "CheckPoint" >to judge the point</param> /// <param name= "polygonpoints" >Vertex of Polygon</param> /// <returns></returns> Public Static BOOLIsinpolygon (Position checkPoint, list<position>polygonpoints) { BOOLInside =false; intPointcount =Polygonpoints.count; Position P1, p2; for(inti =0, j = pointcount-1; i < Pointcount; j = i, i++)//The first point and the last point as the first line, followed by the first and second points as the second line, followed by the second point with the third point, the third point with the fourth point ...{P1=Polygonpoints[i]; P2=Polygonpoints[j]; if(Checkpoint.y <p2.y) {//P2 above the Rays . if(P1.y <=checkpoint.y) {//P1 just below the ray or the ray. if((CHECKPOINT.Y-P1.Y) * (p2.x-p1.x) > (checkpoint.x-p1.x) * (P2.Y-P1.Y))//slope judgment, between P1 and P2 and on the right side of P1P2 { //if the intersection of the Ray and polygon is odd, it is inside the polygon and outside the polygon if it is an even number of intersections. //because the inside initial value is false, that is, the number of intersections is zero. So when there is a first intersection, it must be an odd number, then inside, this is inside= (!inside)//So when there is a second intersection, it must be even, then externally, at this time inside= (!inside)Inside = (!inside); } } } Else if(Checkpoint.y <p1.y) {//P2 right in the beam or below the ray, P1 on the beam . if((CHECKPOINT.Y-P1.Y) * (p2.x-p1.x) < (checkpoint.x-p1.x) * (P2.Y-P1.Y))//slope judgment, between P1 and P2 and on the right side of P1P2{inside= (!inside); } } } returninside; }
Point in Polygon algorithm, C # Determines whether a point is inside a complex polygon