Common methods for polar sorting

Source: Internet
Author: User

four methods commonly used for polar sorting:

Write in front: Structure and function of storage points

1 structPoint//Storage points2 {3     Doublex, y;4 };5 6 DoubleCrossDoubleX1,DoubleY1,DoubleX2,Doubley2)//Calculate cross Product7 {8     return(x1*y2-x2*y1);9 }Ten  One DoubleCompare (point a,point b,point c)//Calculate Polar Angle A { -     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.

1 BOOL CMP1 (point a,point B) 2 {3     if (ATAN2 (a.y,a.x)! =atan2 (b.y,b.x)) 4         return atan2 (a.y,a.x) <atan2 (b.y,b.x); 5     Else return a.x<b.x; 6 }

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

Cross product =0 refers to two vectors parallel (coincident), the cross product >0, then the vector a in vector b in the clockwise direction (can be understood as a at the bottom of b); cross product <0, then vector a in vector b counterclockwise direction (can be understood as a at the top of B).

1 BOOLCMP2 (point a,point B)2 {3Point C;//origin Point4c.x =0;5C.Y =0;6     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 sort7         returna.x<b.x;8     Else returnCompare (C,a,b) >0;9}

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

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

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.

The first method, using atan2 sorting, and using the cross-product sort of the main difference in precision and time.

Specific comparison: time: Compared with the calculation of the cross product, the use of atan2 time fast;

Accuracy: atan2 accuracy is not as high as fork.

Common methods for polar sorting

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.