Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic 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
import java.util.*; class myclass { int x[]; int y[]; myclass(int x[], int y[]) { this.x = x; this.y = y; } void jisuan() { int f = 1; //int c[] = new int[4]; int i = 0; int xx, yy; xx = x[3] - x[0]; yy = y[3] - y[0]; int t = xx*xx + yy*yy; for(i = 0; i < 3; i++) { int tp = (x[i+1] - x[i])*(x[i+1] - x[i]) + (y[i+1] - y[i])*(y[i+1] - y[i]); if(t != tp) { f = 0; System.out.println("NO"); return; //break; } } int ta = (x[2] - x[0])*(x[2] - x[0]) + (y[2] - y[0])*(y[2] - y[0]); int tb = (x[3] - x[1])*(x[3] - x[1]) + (y[3] - y[1])*(y[3] - y[1]); if(ta == tb) System.out.println("YES"); else System.out.println("NO"); } } class Main { public static void main(String[] args) { Scanner ss = new Scanner(System.in); int flag; int n; n = ss.nextInt(); while(n>0) { int x[] = new int[4]; int y[] = new int[4]; for(int i = 0; i < 8; i++) { if(i % 2 == 0) { x[i / 2] = ss.nextInt(); }else{ y[(i - 1)/2] = ss.nextInt(); } } /*for(int i = 0; i < 4; i++) System.out.printf("%d ", x[i]); System.out.println("*"); for(int i = 0; i < 4; i++) System.out.printf("%d ", y[i]); System.out.println("*");*/ myclass my = new myclass(x, y); my.jisuan(); n--; } ss.close(); } }