uva 11800 - Determine the Shape
题意:给定平面上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; }