1. 程式人生 > >Java練習 SDUT-2444_正方形

Java練習 SDUT-2444_正方形

正方形

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;
}