您的位置:首页 > 编程语言 > C语言/C++

hihocoder#1040判断矩形

2015-07-26 21:53 495 查看
//先判断是否重合存在4个点然后判断是否是平行或者垂直的
#include <iostream>
#include <vector>
#include <set>
#include<stdio.h>
using namespace std;

struct xpoint
{
int x1,y1;
bool operator<(const xpoint&ls)const
{
if (x1==ls.x1)
return y1<ls.y1;
return x1<ls.x1;
}
bool operator==(const xpoint&ls)const
{
return x1==ls.x1&&y1==ls.y1;
}
};
struct Line
{
int x1,y1;
int x2,y2;
};
static int cnt = 0;

//判断垂直
static int dot(Line&l1,Line&l2)
{
int tmpx1 = l1.x2 - l1.x1;
int tmpy1 = l1.y2 - l1.y1;
int tmpx2 = l2.x2 - l2.x1;
int tmpy2 = l2.y2 - l2.y1;
return tmpx1*tmpx2+tmpy1*tmpy2;
}

//判断平行
static int xoper(Line&l1,Line&l2)
{
int tmpx1 = l1.x2 - l1.x1;
int tmpy1 = l1.y2 - l1.y1;
int tmpx2 = l2.x2 - l2.x1;
int tmpy2 = l2.y2 - l2.y1;
return tmpx1*tmpy2 - tmpx2*tmpy1;
}

static bool isCube(vector<Line>&lines)
{

//判断是否平行或者垂直
for(int i=0;i<lines.size();++i)
{
for (int j=i+1;j<lines.size();++j)
if (xoper(lines[i],lines[j])==0||
dot(lines[i],lines[j])==0)
continue;
else
return false;
}
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
vector<Line>lins;
int x1,x2,y1,y2;
Line tmp;
set<xpoint>vset;
xpoint pttmp;
for (int i=0;i<4;++i)
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
tmp.x1 = x1;tmp.x2 = x2;
tmp.y1 = y1;tmp.y2 = y2;
pttmp.x1 = x1;pttmp.y1 = y1;
vset.insert(pttmp);
pttmp.x1 = x2;pttmp.y1 = y2;
vset.insert(pttmp);
lins.push_back(tmp);
}
if (vset.size()==4)
{
if (isCube(lins))
printf("YES\n");
else
printf("NO\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm c++