CDOJ 93 King's Sanctuary【判断四边形形状】

King's Sanctuary

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

The king found his adherents were building four sanctuaries for him. He is interested about the positions of the sanctuaries and wants to know whether they would form a parallelogram, rectangle, diamond, square or anything else.

Input

The first line of the input is  T T( 1T1000 1≤T≤1000), which stands for the number of test cases you need to solve. Each case contains four lines, and there are two integers in each line, which shows the position of the four sanctuaries. And it is guaranteed that the positions are given clockwise. And it is always a convex polygon, if you connect the four points clockwise.

Output

For every test case, you should output Case #t: first, where  t t indicates the case number and counts from  1 1, then output the type of the quadrilateral.

Sample input and output

Sample Input Sample Output
5
0 0
1 1
2 1
1 0
0 0
0 1
2 1
2 0
0 0
2 1
4 0
2 -1
0 0
0 1
1 1
1 0
0 0
1 1
2 1
3 0
Case #1: Parallelogram
Case #2: Rectangle
Case #3: Diamond
Case #4: Square
Case #5: Others

Source

Sichuan State Programming Contest 2012


原题链接:http://acm.uestc.edu.cn/#/problem/show/93


题意:按顺时针(clockwise)方向给你四个点的坐标,判断它是否是平行四边形,矩形,菱形,正方形。


几何,肯定是用向量,具体判断四边形的形状,就看你初高中的几何了,具体判断,看下面这张图。



AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cmath>
using namespace std;
struct point
{
    int x,y;
} p[4];
int main()
{
    int T;
    int kase=0;
    cin>>T;
    while(T--)
    {
        for(int i=0; i<4; i++)
        {
            cin>>p[i].x>>p[i].y;
        }
        int px= (p[1].x-p[0].x);
        int py= (p[1].y-p[0].y);

        int qx= (p[2].x-p[3].x);
        int qy= (p[2].y-p[3].y);

        int rx= (p[2].x-p[1].x);
        int ry=(p[2].y-p[1].y);

        int tx=p[3].x-p[0].x;
        int ty=p[3].y-p[0].y;
        printf("Case #%d: ",++kase);
        if(px==qx&&py==qy&&rx==tx&&ry==ty)//两组对边分别相等-->平行四边形
        {
            if(px*rx+py*ry==0)//+邻边垂直-->矩形或正方形
            {
                if(px*px+py*py==rx*rx+ry*ry)//+邻边相等-->正方形
                {
                    cout<<"Square"<<endl;
                }
                else
                    cout<<"Rectangle"<<endl;
            }
            else if(px*px+py*py==rx*rx+ry*ry)//+邻边不垂直但相等-->菱形
            {
                cout<<"Diamond"<<endl;
            }
            else//平行四边形
                cout<<"Parallelogram"<<endl;
        }
        else
            cout<<"Others"<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值