square
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
Given four points, determine whether these four points can form a square.
Input
The first line of input contains an integer T (T≤30) indicating the number of data groups, each group of data has only one line, including 8 integers x1, y1, x2, y2, x3, y3, x4, y4 (the data are all in -1000 ,1000) gives the coordinates of the four points in counterclockwise order.
Output
Each set of data outputs one line, if it is a square, output: YES, otherwise, output: 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
Build a class related to points. From some theorems, we can know that a rhombus with a right angle is a square, so the problem can be completely transformed into the problem of finding the distance between two points
package hello; import java.util. *; class Point{ int x, y; public Point (int x, int y) { this.x = x; this.y = y; } public double dist(Point p) { return (p.x-x)*(p.x-x) + (p.y-y)*(p.y-y); } } public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t; int x1, y1, x2, y2, x3, y3, x4, y4; double d1, d2, d3, d4; t = cin.nextInt (); while(t-- != 0) { x1 = cin.nextInt(); y1 = cin.nextInt(); x2 = cin.nextInt(); y2 = cin.nextInt(); x3 = cin.nextInt(); y3 = cin.nextInt(); x4 = cin.nextInt(); y4 = cin.nextInt(); Point p = new Point(x1, y1); d1 = p.dist(new Point(x2, y2)); p = new Point(x2, y2); d2 = p.dist(new Point(x3, y3)); p = new Point(x3, y3); d3 = p.dist(new Point(x4, y4)); p = new Point(x1, y1); d4 = p.dist(new Point(x4, y4)); double d = p.dist(new Point(x3, y3)); //System.out.println(d1+" "+d2+" "+d3+" "+d4+" "+d); if(d1==d2 && d2==d3 && d3==d4 &&d4 == d1 &&d1+d2==d) { System.out.println("YES"); } else { System.out.println("NO"); } } cin.close(); } }