In work, we often encounter the problem of determining the location of a point at another point. For example, we need to determine the position of p2 in p1, that is, to find that p2 falls in area 1 and 2 relative to p1, in 3, 4, note that p1 is not the coordinate origin, and the coordinate origin is in the upper left corner of the screen (the screen coordinate here ). There are many solutions to this problem. We can use the vector angle. The angle method involves multiplication and division of vectors, which affects the speed. The method provided here only needs to judge the coordinate value of the point.
The specific algorithm is described as follows:
1. Convert p1 and p2 to take p1 as the coordinate origin, that is, the pan coordinate system. Given is the converted Coordinate System
2. We can see that:
In Area 1, there are: | x |> | y |, x> 0.
In area 2, there are: | x | <| y |, y <0
In Area 3, there are: | x |> | y |, x <0
In area 4, there are: | x | <| y |, y> 0.
The Code is as follows:
Bool getdirect (<br/> Point P1, <br/> point P2, <br/> Int & nregion) <br/>{< br/> float FDIs = (float) SQRT (double) (p2.x-p1.x) * (p2.x-p1.x) + (p2.y-p1.y) * (p2.y-p1.y ))); <br/> If (FDIS <0.001) <br/>{< br/> // highlights overlap <br/> nregion = 0; <br/> return false; <br/>}</P> <p> // convert p2 to a coordinate system centered on P1 <br/> p2.x-= p1.x; <br/> p2.y-= p1.y; </P> <p> If (ABS (p2.x)> ABS (p2.y) & p2.x> 0) <br/>{< br/> nregion = 1; <br/>}< br/> else if (ABS (p2.x)> ABS (p2.y) & p2.x <0) <br/>{< br/> nregion = 3; <br/>}< br/> else if (ABS (p2.x) <ABS (p2.y) & p2.y <0) <br/>{< br/> nregion = 2; <br/>}< br/> else if (ABS (p2.x) <ABS (p2.y) & p2.y> 0) <br/>{< br/> nregion = 4; <br/>}</P> <p> return true; <br/>}
Conclusion: This method can be used to determine the situation in other regions. You only need to modify the judgment conditions.