UVA11800 Determine the Shape(计算几何基础)

  

Sample Input
6
0 0
2 0
2 2
0 2
0 0
3 0
3 2
0 2
0 0
8 4
5 0
3 4
0 0
2 0
3 2
1 2
0 0
5 0
4 3
1 3
0 0
5 0
4 3
1 4
Sample Output
Case 1: Square
Case 2: Rectangle
Case 3: Rhombus
Case 4: Parallelogram
Case 5: Trapezium
Case 6: Ordinary Quadrilateral

给出四个点的坐标,保证任意三点不共线,判断这四个点构成的四边形是什么图形?

正方形,矩形,菱形,平行四边形,梯形,普通四边形 

题目不难,但是要考虑的地方比较多,梯形判断有一组对边平行即可

平行四边形判断两组对边平行

  • 菱形在判断对角线是否垂直
  • 矩形在判断临边是否垂直
  • 正方形满足上述全部条件

还有一个问题要注意,究竟判断哪一组边合适,根据图的同构,只有三种情况,以此判断即可

 

    struct Point
    {
        double x,y;
        Point(double x=0,double y=0):x(x),y(y){}
    };

typedef Point Vector;
Vector operator+(Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y); }
Vector operator-(Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
double Dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; }
double Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; }

int go(Point a,Point b,Point c,Point d)
{
    int ans=0,flag=0;
    Vector ab=b-a,ad=d-a,cb=b-c,cd=d-c;
    if(Cross(ab,cd)==0 || Cross(ad,cb)==0) ans=1; //梯形
    if(Cross(ab,cd)==0 && Cross(ad,cb)==0){ //平行四边形
        ans=2;
        if(Dot(ab,ad)==0) ans=3,flag=1;
        if(Dot(a-c,b-d)==0) ans=4;
        if(ans==4 && flag==1) ans=5;
    }
    return ans;
}

int main()
{
    //IOS;
    int num=0;
    rush(){
        printf("Case %d: ",++num);
        Point a,b,c,d;
        sff(a.x,a.y); sff(b.x,b.y); sff(c.x,c.y); sff(d.x,d.y);
        int ans=go(a,b,c,d);
        ans=max(ans,go(a,c,b,d));
        ans=max(ans,go(a,b,d,c));
        switch(ans){
            case 0:puts("Ordinary Quadrilateral"); break;
            case 1:puts("Trapezium"); break;
            case 2:puts("Parallelogram"); break;
            case 3:puts("Rectangle"); break;
            case 4:puts("Rhombus"); break;
            case 5:puts("Square"); break;
        }
    }
    //PAUSE;
    return 0;
}

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页