正方形
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
Hint
Source
//package leslie;
//向量的应用:正方形对角线长度相等且垂直,本题中将x1,y1看作一个向量的起始
平面中,设有向量a={x1, y1},向量b={x2, y2},则有
(1)向量的加:a+b={x1+x2, y1+y2}
(2)向量的减:a-b={x1-x2, y1-y2}
(3)向量的点积(内积,运算结果是标量):a·b=|a|*|b|*cosθ=x1*x2 + y1*y2,a⊥b当且仅当a·b=0
(4)向量的叉积(外积,运算结果是向量):a×b=x1*y2 - x2*y1注:文中出现的*为数学乘法,×为叉积运算符。a∥b当且仅当a×b=0(0向量)
(5) 向量的模(长度):L=(开根号(x*x+y*y))
package leslie;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n=input.nextInt();
while(n>0)
{
n--;
int x1=input.nextInt();
int y1=input.nextInt();
int x2=input.nextInt();
int y2=input.nextInt();
int x3=input.nextInt();
int y3=input.nextInt();
int x4=input.nextInt();
int y4=input.nextInt();
int flag=0;
if(((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)==(x4-x2)*(x4-x2)+(y4-y2)*(y4-y2))&&((x3-x1)*(x4-x2)+(y3-y1)*(y4-y2)==0))//1.向量a的模等于b的 2.两向量垂直
flag=1;
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
}
input.close();
}
}