極角排序的幾種方法

來源:互聯網
上載者:User

極角排序:給定平面上的一些點,把它們按照一個選定的中心點排成順(逆)時針。方法是把每個點於中心點轉化成向量,通過判斷叉積的正負來判斷點的相對位置。


以下轉自:點擊開啟連結


這裡講了幾種極角排序方法,http://www.csie.ntnu.edu.tw/~u91029/PointLinePlane2.html,我這裡只說下atan2還有象限排序。

因為純粹叉積是不能排360度的,所以需要改一些東西。

atan2是按向量排序的,這個很好啊,可是就是可能會傷精度,那麼會死很慘的,雖然我還沒遇到用atan2傷精度的題~

atan2的參數是向量的y座標,x座標,排序範圍是(-180°, +180°]。


象限排序是不損精度的,因為只用到叉積還有一些比較而已。

先判斷兩個點是在哪兩個象限,然後如果不在同一個象限,直接象限比較,如果在同一個象限,那麼用叉積比較即可,邊界問題考慮下。



大小判定函數。

[cpp]  view plain copy const double eps = 1e-6;   bool dy(double x,double y)  {   return x > y + eps;} // x > y    bool xy(double x,double y)  {   return x < y - eps;} // x < y    bool dyd(double x,double y) {   return x > y - eps;} // x >= y    bool xyd(double x,double y) {   return x < y + eps;}     // x <= y    bool dd(double x,double y)  {   return fabs( x - y ) < eps;}  // x == y  

atan2排序

[cpp]  view plain copy bool cmp(point& a,point& b)     {         double t1 = atan2(a.y - C.y, a.x - C.x);       double t2 = atan2(b.y - C.y, b.x - C.x);       if( dd(t1, t2) ) return xy(fabs(a.x),fabs(b.x));       return xy(a.t, b.t);     }  


象限極角排序代碼

[cpp]  view plain copy int quad(point a)// 判斷象限的函數,每個象限包括半個座標軸    {       if( dy(a.x,0) && xyd(a.y,0) ) return 1;       if( xyd(a.x,0) && dy(a.y,0) ) return 2;       if( xy(a.x,0) && xyd(a.y,0) ) return 3;       if( dyd(a.x,0) && xy(a.y,0) ) return 4;   }    bool cmp(point& a,point& b)     {         point p1 = a,p2 = b;       p1.x -= C.x; p1.y -= C.y;       p2.x -= C.x; p2.y -= C.y;       int l1,l2;       l1 = quad(p1); l2 = quad(p2);       if( l1 == l2 )       {           double c = crossProduct(C,a,b);           return xy(c,0) || dd(c,0.0) && xy(fabs(a.x),fabs(b.x));       }       return l1 < l2;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.