How to determine the position of one point in another point in 2D

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.