Home >Java >javaTutorial >How to confirm in Java whether given four points form a square?
A square is a two-dimensional shape with four sides of equal length. Opposite sides of a square are parallel, all four interior angles are right angles, and the diagonals are of equal length. In this article we will examine how to confirm whether a given four points form a square.
We will get a square with four points, namely A, B, C, D, as shown in the figure −
We need to check from these points whether they form a square. In order to check this, it should satisfy the following conditions −
The distance between point A and point C, and the distance between point B and point D i.e. "x" should be equal.
The distance between point A and point B, the distance between point B and point C, the distance between point C and point D, the distance between point D and point A i.e. "z" should equal.
We will find the distance between two points using the formula -
$$\mathrm{d=\sqrt{(x_{2}-x_{1})^2(y_{2}-y_{1})^2}}$$
Point 1 will be (x1, y1), point 2 will be (x2, y2).
let's start!
Given four input points are -
P1(3,7), P2(4,3), P3(7,8), P4(1,9)
Put it into the distance formula and check if the square condition is met, the result will be -
The given four points do not form a square.
Given four input points are -
P1(20,20), P2(20,10), P3(10,10), P4(10,20)
Put it into the distance formula and check if the square condition is met, the result will be -
Given four points form a square.
Step-1 − Declare and initialize variables.
Step-2 − Find the distance between center 1 and center 2 of the circle.
Step 3 - Check the five distance conditions.
Step-4 − Print the results.
We provide solutions in different ways.
By using static input
By using user-defined methods
Let's look at the program and its output one by one.
In this method, point values are assigned. Then according to the algorithm we will find out whether the given four points form a square.
The Chinese translation ofpublic class Main{ //main method public static void main(String[] args){ //declaring variables int x1=3, x2=4, x3=7, x4=1; int y1=7, y2=3, y3=8, y4=9; double d1, d2, d3, d4, d5, d6; //applyinng logic d1 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); d2 = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2); d3 = (x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3); d4 = (x1 - x4) * (x1 - x4) + (y1 - y4) * (y1 - y4); d5 = (x4 - x2) * (x4 - x2) + (y4 - y2) * (y4 - y2); d6 = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1); if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0 || d5 == 0 || d6 == 0){ System.out.println("Given four points do not form a square"); } else if (d1 == d2 && d2 == d3 && d3 == d4 && d5 == d6){ //prints if four points form square System.out.println("Given four points form a square"); } else { //prints if four points do not form square System.out.println("Given four points do not form a square"); } } }
Given four points do not form a square
In this method, point values are assigned. Then a user-defined method is called by passing the given value and based on the algorithm it determines whether the given four points form a square.
The Chinese translation ofpublic class Main{ //main method public static void main(String[] args){ //creating objects of Point Point p1 = new Point(20, 20); Point p2 = new Point( 20, 10 ); Point p3 = new Point(10, 10 ); Point p4 = new Point( 10, 20 ); //calling user defined method if(isSquare(p1, p2, p3, p4)==true){ //print if four points form a square System.out.println("Given four points form a square"); } else{ //print if points does not form a square System.out.println("Given four points do not form a square"); } } // Declaring Point class static class Point{ int x, y; public Point(int x, int y){ this.x = x; this.y = y; } }; //function to find square of distance from point 'p' to point 'q' static int distSq(Point p, Point q){ return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y); } //user defined method static boolean isSquare(Point p1, Point p2, Point p3, Point p4){ int d1 = distSq(p1, p2); int d2 = distSq(p2, p3); int d3 = distSq(p3, p4); int d4 = distSq(p4, p1); int d5 = distSq(p1, p3); int d6 = distSq(p2, p4); if (d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0 || d5 == 0 || d6 == 0) return false; if (d1 == d2 && d2 == d3 && d3 == d4 && d5 == d6){ //it returns true if (p1, p2, p3, p4) form a square return true; } //it returns false if (p1, p2, p3, p4) do not form a square return false; } }
Given four points form a square
In this article, we explored different ways to check if a line touches, intersects, or lies outside a circle using the Java programming language.
The above is the detailed content of How to confirm in Java whether given four points form a square?. For more information, please follow other related articles on the PHP Chinese website!