您的位置:首页 > 其它

#1040 : 矩形判断

2015-09-12 09:37 190 查看
我觉得测试用例可能很少很easy

import java.awt.Point;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();

double[] slope = new double[4];
while (T-- != 0) {
for (int i = 0; i < 4; i++) {
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
if (x1 == x2) {
slope[i] = Integer.MAX_VALUE;
}
else {
slope[i] = (y1 - y2)/(x1 - x2 + 0.0);
}
}

if (isRectangle(slope)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}

private static boolean isEquals(double a, double b){
if (a - b >= -0.0000000001 && a - b <= 0.0000000001) {
return true;
}
else {
return false;
}
}
private static boolean isRectangle(double[] slope) {
// TODO Auto-generated method stub

double s1 = slope[0], s2 = slope[0];
int count = 0;
for (int i = 1; i < slope.length; i++) {
if (!isEquals(slope[i], s1) && !isEquals(slope[i], s2)) {
count++;
s2 = slope[i];
}
}

if (count == 0 || count > 1) {
return false;
}
if (s1 > s2) {
s1 = s1 + s2;
s2 = s1 - s2;
s1 = s1 - s2;
}

if (isEquals(s1, 0) && isEquals(s2, Integer.MAX_VALUE)) {
return true;
}
else {
if (isEquals((s1 * s2 + 1), 0)) {
return true;
}
else {
return false;
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: