旋轉卡殼部分模板

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   for   代碼   div   

凸包直徑

旋轉卡殼凸包直徑詳解

//計算凸包直徑,輸入凸包ch,頂點個數為n,按逆時針排列,輸出直徑的平方

int rotating_calipers(int n){    int q = 1;    int ans = 0;    ch[n] = ch[0];    for(int i = 0 ; i < n;  i++)    {        while(mul(ch[i+1],ch[q+1],ch[i])>mul(ch[i+1],ch[q],ch[i]))//枚舉凸包一條邊並掃描其它點,通過計算三角形面積的方法找到最遠的點        q = (q+1)%n;        ans = max(ans,max(dis(ch[i]-ch[q]),dis(ch[i+1]-ch[q+1])));    }        return ans;}
 

 凸包間最小距離

struct Point{    double x,y;    Point(double x=0,double y=0):x(x),y(y) {} //建構函式 方便代碼編寫}p[N],q[N];typedef Point pointt;pointt operator + (Point a,Point b){    return Point(a.x+b.x,a.y+b.y);}pointt operator - (Point a,Point b){    return Point(a.x-b.x,a.y-b.y);}int dcmp(double x){    if(fabs(x)<eps) return 0;    else return x<0?-1:1;}bool operator == (const Point &a,const Point &b){    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}double dot(Point a,Point b){    return a.x*b.x+a.y*b.y;}double  dis(Point a){    return sqrt(dot(a,a));}double cross(Point a,Point b){    return a.x*b.y-a.y*b.x;}void anticlock(Point p[],int n)//逆時針排序{    for(int i = 0 ; i < n-2 ; i++)    {        double k = cross(p[i+1]-p[0],p[i+2]-p[0]);        if(dcmp(k)>0) return ;        else if(dcmp(k)<0)        {            reverse(p,p+n);            return ;        }    }}double distoline(Point a,Point b,Point c)//點到線段ab的最短距離{    if(dcmp(dis(a-b))==0) return dis(a-c);    if(dcmp(dot(a-b,a-c))<0) return dis(a-c);    if(dcmp(dot(b-a,b-c))<0) return dis(b-c);    return fabs(cross(a-b,a-c))/dis(a-b);}double dist(Point a,Point b,Point c,Point d)//線段ab和cd間的最短距離{    double ans = distoline(a,b,c);    ans = min(ans,distoline(a,b,d));    ans = min(ans,distoline(c,d,a));    ans = min(ans,distoline(c,d,b));    return ans;}double mul(Point a,Point b,Point c){    return cross(b-a,c-a);}double  solve(Point p[],int n,Point q[],int m){    int i;    int miny = 0,maxy = 0;    for(i = 0;i < n; i++)    {        if(p[i].y<p[miny].y)        miny = i;    }    for(i =0 ; i< m ; i++)    if(q[i].y>q[maxy].y) maxy = i;    double ans = dis(p[miny]-q[maxy]);    for(i = 0 ;i < n; i++)    {        double tmp;        while(tmp = mul(p[miny],p[miny+1],q[maxy+1])-mul(p[miny],p[miny+1],q[maxy])>eps)        maxy = (maxy+1)%m;        if(dcmp(tmp)>0) ans = min(ans,distoline(p[miny],p[miny+1],q[maxy]));        else        ans = min(ans,dist(p[miny],p[miny+1],q[maxy],q[maxy+1]));//邊-邊        miny = (miny+1)%n;    }    return ans;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.