正方形
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给出四个点,判断这四个点能否构成一个正方形。
Input
输入的第一行包含一个整数T(T≤30)表示数据组数,每组数据只有一行,包括8个整数x1, y1, x2, y2,x3,y3,x4,y4(数据均在-1000,1000 之间)以逆时针顺序给出四个点的坐标。
Output
每组数据输出一行,如果是正方形,则输出: YES, 否则,输出:NO。
Sample Input
2
0 0 1 0 1 1 0 1
-1 0 0 -2 1 0 2 0
Sample Output
YES
NO
题解:根据正方形的判定公式,对角线相等且垂直,来判断是不是正方形。
import java.util.*;
public class Main {
public static void main(String[] agrs)
{
Scanner cin = new Scanner(System.in);
point a,b,c,d;
int t;
t = cin.nextInt();
while(t-->0)
{
a = new point();
b = new point();
c = new point();
d = new point();
a.x = cin.nextInt();
a.y = cin.nextInt();
b.x = cin.nextInt();
b.y = cin.nextInt();
c.x = cin.nextInt();
c.y = cin.nextInt();
d.x = cin.nextInt();
d.y = cin.nextInt();
if(judge(a,b,c,d)==1)
System.out.println("YES");
else
System.out.println("NO");
}
cin.close();
}
static int judge(point a,point b,point c,point d)
{
int x,y;
x = (a.x-c.x) * (a.x-c.x) + (a.y-c.y) * (a.y-c.y);
y = (b.x-d.x) * (b.x-d.x) + (b.y-d.y) * (b.y-d.y);
if(x==y&&((a.x-c.x) * (b.x-d.x)==-((a.y-c.y) * (b.y-d.y))))
return 1;
return 0;
}
}
class point
{
int x,y;
}