A detailed description of the polar angle

Source: Internet
Author: User

About Polar Sort:

In the plane to take a fixed point o, called the Pole, a ray of ox, called the polar axis, and then select a length unit and angle of the square direction (usually counterclockwise).

For any bit of m in the plane, the length of the segment OM is represented by the ρ (sometimes denoted by R), θ denotes the angle from ox to OM, ρ is called the polar diameter of point m, θ is called the polar angle of point m, and the order number pair (ρ,θ) is called the polar coordinates of M.

Then some of the points on the given plane are sorted by a selected center point into the clockwise (inverse) hand.

Four methods commonly used for polar sorting:

Before you say four methods, give the structure of a function and a storage point to be used

structPoint//Storage points{    Doublex, y;};DoubleCrossDoubleX1,DoubleY1,DoubleX2,Doubley2)//Calculate cross Product{    return(x1*y2-x2*y1);}DoubleCompare (point a,point b,point c)//Calculate Polar Angle{    returnCross ((b.x-a.x), (B.Y-A.Y), (c.x-a.x), (c.y-a.y));}

Method 1: Use the atan2 () function to sort from small to large at the polar angle.

About the atan2 () function: In the C language of MATH.H or C + + Cmath There are two function atan (double x) with atan2 (double y,double x) and the value they return is the radian to be converted to an angle and then processed by itself.

The former accepts a tangent (the slope of the line) to get the angle, but because the tangent of the regularity can have two angles, but it only returns one, because the Atan range is from -90~90 that it only handles 14 quadrants, so it is generally not used.

The second atan2 (double y,double x) where y represents the y-coordinate of the known point, and the same x, the return value is the angle between this point and the distance line and the x-axis positive direction, so that it can handle four quadrants of any case, its range corresponding to that is -180~180

bool CMP1 (point a,point B){    if(atan2 (a.y,a.x)! =atan2 (b.y,b.x)        )  Return atan2 (a.y,a.x) <atan2 (b.y,b.x);     Else return a.x<b.x;}

Method 2: Use the cross product to sort from small to large at the polar angle.

About the cross product: The cross product =0 refers to two vectors parallel (coincident), the cross product >0, then the vector a in vector b in the clockwise direction (rough understanding of a at the bottom of b); cross product <0, then vector a in vector b counterclockwise direction (roughly understood as a at the top of B)

BOOL CMP2 (Point A,point b) {point    C; // origin Point    0 ;     0 ;     if (Compare (c,a,b) = =0) // Calculate the cross product, the function is described above, if the cross product is equal, according to X from small to large sort        return a.x<b.x;     Else return Compare (c,a,b) >0;}

Method 3: Sort by quadrant from small to large and then by polar angle from small to large

intQuadrant (Point a)//Quadrant Sort, note contains four axes{    if(a.x>0&&a.y>=0)return 1; if(a.x<=0&&a.y>0)return 2; if(a.x<0&&a.y<=0)return 3; if(a.x>=0&&a.y<0)return 4;}BOOLCmp3 (Point A,point B)//Sort by quadrant from small to large and then from small to large by polar angle{    if(Quadrant (a) ==quadrant (b))//The return value is quadrant        returnCmp1 (A, b); ElseQuadrant (a) <Quadrant (b);}

A comparison of three methods:

The third method by quadrant from small to large sort and then by the polar angle from small to large sort is in the special needs of the time will be used, here do not compare.

With regard to the first method, using the atan2 sort, he and the main difference between using the cross-product sort are in precision and time.

Specific comparison: time: Compared to the calculation of the cross product, the use of atan2 time faster, this time will be faster (remember to do a problem with the atan2 sort over, with the cross product of T)

Accuracy: atan2 accuracy is not as high as fork, do a problem with anat2 because of the accuracy of WA.

So the two methods choose a suitable use according to the situation.

A detailed description of the polar angle

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.