uva 11800 – Determine the Shape

來源:互聯網
上載者:User

題意:給定平面上4個點,沒有3點共線;判斷這4個點能組成怎樣的四邊形。

正方形:Square

矩形:Rectangle

菱形:Rhombus

平行四邊形:Parallelogram

梯形:Trapezium

普通四邊形:Ordinary Quadrilateral

#include<iostream>#include<algorithm>using namespace std;typedef struct points{    double x,y;    points(double xx=0,double yy=0):x(xx),y(yy){}}vectors;points p[4],ch[4];bool operator < (points a,points b){    return a.x<b.x || (a.x==b.x && a.y<b.y);}vectors operator - (points a,points b){    return vectors(a.x-b.x,a.y-b.y);}double dot(vectors a,vectors b){    return a.x*b.x+a.y*b.y;}double len(vectors a){    return dot(a,a);}double cross(vectors a,vectors b){    return a.x*b.y-a.y*b.x;}bool convex(){    sort(p,p+4);    int m=0;    for(int i=0;i<4;i++)    {        while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;        ch[m++]=p[i];    }    int k=m;    for(int i=2;i>=0;i--)    {        while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;        ch[m++]=p[i];    }    return m==5;}int main(){    int i,t;    cin>>t;    for(i=1;i<=t;i++)    {        for(int j=0;j<4;j++) cin>>p[j].x>>p[j].y;        cout<<"Case "<<i<<": ";        if(convex())        {            vectors u=ch[1]-ch[0],v=ch[3]-ch[2];            vectors w=ch[2]-ch[1],r=ch[0]-ch[3];            if(cross(u,v))            {                if(cross(w,r)) cout<<"Ordinary Quadrilateral"<<endl;                else cout<<"Trapezium"<<endl;            }            else            {                if(cross(w,r)) cout<<"Trapezium"<<endl;                else                {                    if(dot(u,w))                    {                        if(len(u)==len(w)) cout<<"Rhombus"<<endl;                        else cout<<"Parallelogram"<<endl;                    }                    else                    {                        if(len(u)==len(w)) cout<<"Square"<<endl;                        else cout<<"Rectangle"<<endl;                    }                }            }        }        else cout<<"Ordinary Quadrilateral"<<endl;    }    return 0;}

聯繫我們

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