hihoCoder1040 矩形判断

#1040 : 矩形判断

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形。

输入

输入第一行是一个整数T(1<=T<=100),代表测试数据的数量。

每组数据包含4行,每行包含4个整数x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 100000);其中(x1, y1), (x2,y2)代表一条线段的两个端点。

输出

每组数据输出一行YES或者NO,表示输入的4条线段是否恰好围成矩形。

样例输入
3
0 0 0 1
1 0 1 1
0 1 1 1
1 0 0 0
0 1 2 3
1 0 3 2
3 2 2 3
1 0 0 1
0 1 1 0
1 0 2 0
2 0 1 1
1 1 0 1
样例输出
YES
YES
NO

分析:先判断四条边是否首尾相连,即是否构成四边形,再判断四边形是否是矩形。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Node{
    int x1,y1,x2,y2;
}a[10];

int f(Node A,Node B)
{
    int ans=0;
    if(A.x1==B.x1&&A.y1==B.y1) ans+=1;
    if(A.x1==B.x2&&A.y1==B.y2) ans+=10;
    if(A.x2==B.x1&&A.y2==B.y1) ans+=100;
    if(A.x2==B.x2&&A.y2==B.y2) ans+=1000;
    return ans;
}

int par(Node A,Node B)//判断平行 
{
    if((A.x2-A.x1)*(B.y2-B.y1)==(B.x2-B.x1)*(A.y2-A.y1)) return 1;
    return 0;
}

int ver(Node A,Node B)//判断垂直
{
    if((A.x2-A.x1)*(B.x2-B.x1)+(A.y2-A.y1)*(B.y2-B.y1)==0)
    return 1;
    return 0;
} 

int check()
{
    int i,m=0,n=0;
    for(i=1;i<4;i++)
        if(!f(a[0],a[i]))//无公共点
            break;
    for(int j=1;j<4;j++)
        if(j!=i)
        {
            if(m==0) m=j;
            else n=j;
        }
    if(i==4) return 0;
    
    int flag=f(a[0],a[n]);
    if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
    flag=f(a[n],a[i]);
    if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
    flag=f(a[i],a[m]);
    if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
    flag=f(a[0],a[m]);
    if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
    
    if(!(par(a[0],a[i])&&par(a[m],a[n]))) return 0;
    if(!(ver(a[0],a[n])&&ver(a[0],a[m]))) return 0;
    if(!(ver(a[i],a[n])&&ver(a[i],a[m]))) return 0;
    return 1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--) 
    {
        for(int i=0;i<4;i++)
            scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
        if(!check()) printf("NO
");
        else printf("YES
");
    }
    return 0;
}
View Code




原文地址:https://www.cnblogs.com/ACRykl/p/9560613.html