#1040 : 矩形判断
时间限制:1000ms
单点时限:1000ms
内存限制:256MB
-
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
描述
给出平面上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条线段是否恰好围成矩形。
#include <iostream>
#include <limits.h>
#include <set>
using namespace std;
struct Line {
int x1, y1, x2, y2;
};
int main()
{
int T;
cin >> T;
set<pair<int, int>> st;
while (T--) {
st.clear();
Line line1, line2, line3, line4;
cin >> line1.x1 >> line1.y1 >> line1.x2 >> line1.y2;
st.insert(make_pair(line1.x1, line1.y1));
st.insert(make_pair(line1.x2, line1.y2));
cin >> line2.x1 >> line2.y1 >> line2.x2 >> line2.y2;
st.insert(make_pair(line2.x1, line2.y1));
st.insert(make_pair(line2.x2, line2.y2));
cin >> line3.x1 >> line3.y1 >> line3.x2 >> line3.y2;
st.insert(make_pair(line3.x1, line3.y1));
st.insert(make_pair(line3.x2, line3.y2));
cin >> line4.x1 >> line4.y1 >> line4.x2 >> line4.y2;
st.insert(make_pair(line4.x1, line4.y1));
st.insert(make_pair(line4.x2, line4.y2));
if (st.size() != 4) {
cout << "NO" << endl;
continue;
}
double slope1, slope2, slope3, slope4;
slope1 = (line1.x1 == line1.x2) ? INT_MAX : (double)(line1.y2 - line1.y1) / (line1.x2 - line1.x1);
slope2 = (line2.x1 == line2.x2) ? INT_MAX : (double)(line2.y2 - line2.y1) / (line2.x2 - line2.x1);
slope3 = (line3.x1 == line3.x2) ? INT_MAX : (double)(line3.y2 - line3.y1) / (line3.x2 - line3.x1);
slope4 = (line4.x1 == line4.x2) ? INT_MAX : (double)(line4.y2 - line4.y1) / (line4.x2 - line4.x1);
if (slope1 == slope2) {
if (slope3 != slope4) {
cout << "NO" << endl;
continue;
}
//垂直
if (((line1.y1 - line1.y2) * (line3.y1 - line3.y2) +
(line1.x1 - line1.x2) * (line3.x1 - line3.x2)) == 0) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
///////////////////////////////////
else if (slope1 == slope3) {
if (slope2 != slope4) {
cout << "NO" << endl;
continue;
}
//垂直
if (((line1.y1 - line1.y2) * (line2.y1 - line2.y2) +
(line1.x1 - line1.x2) * (line2.x1 - line2.x2)) == 0) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
/////////////////////////////////
else if(slope1 == slope4) {
if (slope2 != slope3) {
cout << "NO" << endl;
continue;
}
//垂直
if (((line1.y1 - line1.y2) * (line3.y1 - line3.y2) +
(line1.x1 - line1.x2) * (line3.x1 - line3.x2)) == 0) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
/////////////////////////////
else {
cout << "NO" << endl;
}
}
return 0;
}