Java Program to Check If Given Four Points Form a Square

In the previous article, we have seen Java Program to Find Total Area of Two Overlapping Rectangles

In this article, we will see how to check whether the given 4 points form a square or not by using Java programming language.

Java Program to Check If Given Four Points Form a Square

Before jumping into the program directly, let’s see how we can check that given four points form a square or not.

Explanation:

Let us assume there is a Quadrilateral having 4 points A,B,C,D

Now in order to check if the quadrilateral is a valid square or not, we have to check 2 conditions

  1. Distance of all the sides be equal I.e AB=BC=CD=DA
  2. Distance of diagonals be equal I.e AC=BD

Now in order to know the distance between 2 points we have the following formula as

AB = √[(x2−x1)2 + (y2−y1)2]

Where,

  • Coordinate of A is (x1,y1)
  • Coordinate of B is (x2,y2)

Example:

Let A(0,0), B(0,1), C(1,1), D(1,0)

Now,

AB =  √[(x2−x1)2 + (y2−y1)2] = √(0+1) =√1

BC = √(1+0) = √1

CD = √(0+1) = √1

DA = √(1+0) = √1

Since AB=BC=CD=DA, condition 1 satisfies.

Now,

AC = √(1+1) = √2

BD = √(1+1) = √2

Since AC=BD, condition 2 satisfies.

Since, both conditions satisfies hence we can say quadrilateral ABCD is a valid square.

Let’s see different ways to check whether the given 4 points form a square or not.

Method-1: Java Program to Check If Given Four Points Form a Square By Using Static Input Value

Approach:

  • Declare an integer variable say ‘x1’ & ‘y1’ and assign the value to it, which holds the coordinate value of the point A.
  • Declare an integer variable say ‘x2’ & ‘y2’ and assign the value to it, which holds the coordinate value of the point B.
  • Declare an integer variable say ‘x3’ & ‘y3’ and assign the value to it, which holds the coordinate value of the point C.
  • Declare an integer variable say ‘x4’ & ‘y4’ and assign the value to it, which holds the coordinate value of the point D.
  • Now, we will find the distance between all the points using the formula

AB = √(((x2−x1)*(x2−x1)) + ((y2−y1)*(y2−y1)))

BC = √(((x3−x2)*(x3−x2)) + ((y3−y2)*(y3−y2)))

CD = √(((x4−x3)*(x4−x3)) + ((y4−y3)*(y4−y3)))

DA = √(((x4−x1)*(x4−x1)) + ((y4−y1)*(y4−y1)))

AC = √(((x3−x1)*(x3−x1)) + ((y3−y1)*(y3−y1)))

BD = √(((x4−x2)*(x4−x2)) + ((y4−y2)*(y4−y2)))

  • Now, if AB==BC && BC==CD && CD==DA && AC==BD then print the result as valid square.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
            int x1 = 0;// x-cordinate of A
    		int y1 = 0;// y-cordinate of A
    		int x2 = 0;// x-cordinate of B
    		int y2 = 1;// y-cordinate of B
    		int x3 = 1;// x-cordinate of C
    		int y3 = 1;// y-cordinate of C
    		int x4 = 1;// x-cordinate of D
    		int y4 = 0;// y-cordinate of D
    		
            // distance formula using 2 point 2d fomula 
    		double AB = Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
    		double BC = Math.sqrt(((x3-x2)*(x3-x2)) + ((y3-y2)*(y3-y2)));
    		double CD = Math.sqrt(((x4-x3)*(x4-x3)) + ((y4-y3)*(y4-y3)));
    		double DA = Math.sqrt(((x4-x1)*(x4-x1)) + ((y4-y1)*(y4-y1)));
    		double AC = Math.sqrt(((x3-x1)*(x3-x1)) + ((y3-y1)*(y3-y1)));
    		double BD = Math.sqrt(((x4-x2)*(x4-x2)) + ((y4-y2)*(y4-y2)));
    		
            // checking conditions
    		if(AB==BC && BC==CD && CD==DA && AC==BD)
    		    System.out.println("Valid square");
    		else
    		    System.out.println("Not a valid square");
    }
}

Output:

Valid square

Method-2: Java Program to Check If Given Four Points Form a Square By Using Dynamic Input Value

Approach:

  • Declare an integer variable say ‘x1’ & ‘y1’ which holds the coordinate value of the point A.
  • Declare an integer variable say ‘x2’ & ‘y2’ which holds the coordinate value of the point B.
  • Declare an integer variable say ‘x3’ & ‘y3’ which holds the coordinate value of the point C.
  • Declare an integer variable say ‘x4’ & ‘y4’ which holds the coordinate value of the point D.
  • Then we will take the value of ‘x1’, ‘y1’, ‘x2’, ‘y2’, ‘r1’, ‘r2’ as user input using scanner class.
  • Now, we will find the distance between all the points using the formula

AB = √(((x2−x1)*(x2−x1)) + ((y2−y1)*(y2−y1)))

BC = √(((x3−x2)*(x3−x2)) + ((y3−y2)*(y3−y2)))

CD = √(((x4−x3)*(x4−x3)) + ((y4−y3)*(y4−y3)))

DA = √(((x4−x1)*(x4−x1)) + ((y4−y1)*(y4−y1)))

AC = √(((x3−x1)*(x3−x1)) + ((y3−y1)*(y3−y1)))

BD = √(((x4−x2)*(x4−x2)) + ((y4−y2)*(y4−y2)))

  • Now, if AB==BC && BC==CD && CD==DA && AC==BD then print the result as valid square.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
    public static void main(String [] args)
    {
        Scanner s = new Scanner(System.in); 
        System.out.println("Enter the value of x1 coordinate of A:");
        int x1 = s.nextInt(); // x-cordinate of A
        System.out.println("Enter the value of y1 coordinate of A:");
        int y1 = s.nextInt();// y-cordinate of A
        System.out.println("Enter the value of x1 coordinate of B:");
        int x2 = s.nextInt();// x-cordinate of B
        System.out.println("Enter the value of y1 coordinate of B:");
        int y2 = s.nextInt();// y-cordinate of B
        System.out.println("Enter the value of x1 coordinate of C:");
        int x3 = s.nextInt();// x-cordinate of C
        System.out.println("Enter the value of y1 coordinate of C:");
        int y3 = s.nextInt();// y-cordinate of C
        System.out.println("Enter the value of x1 coordinate of D:");
        int x4 = s.nextInt();// x-cordinate of D
        System.out.println("Enter the value of y1 coordinate of A:");
        int y4 = s.nextInt();// y-cordinate of D
        
        // distance formula using 2 point 2d fomula 
        double AB = Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
        double BC = Math.sqrt(((x3-x2)*(x3-x2)) + ((y3-y2)*(y3-y2)));
        double CD = Math.sqrt(((x4-x3)*(x4-x3)) + ((y4-y3)*(y4-y3)));
        double DA = Math.sqrt(((x4-x1)*(x4-x1)) + ((y4-y1)*(y4-y1)));
        double AC = Math.sqrt(((x3-x1)*(x3-x1)) + ((y3-y1)*(y3-y1)));
        double BD = Math.sqrt(((x4-x2)*(x4-x2)) + ((y4-y2)*(y4-y2)));
        
        // checking conditions
        if(AB==BC && BC==CD && CD==DA && AC==BD)
        System.out.println("Valid square");
        else
        System.out.println("Not a valid square");
    }
}
Output:

Case-1
Enter the value of x1 coordinate of A:
4
Enter the value of y1 coordinate of A:
4
Enter the value of x1 coordinate of B:
4
Enter the value of y1 coordinate of B:
8
Enter the value of x1 coordinate of C:
8
Enter the value of y1 coordinate of C:
8
Enter the value of x1 coordinate of D:
8
Enter the value of y1 coordinate of A:
4
Valid square

Case-2
Enter the value of x1 coordinate of A:
1
Enter the value of y1 coordinate of A:
2
Enter the value of x1 coordinate of B:
3
Enter the value of y1 coordinate of B:
4
Enter the value of x1 coordinate of C:
5
Enter the value of y1 coordinate of C:
6
Enter the value of x1 coordinate of D:
7
Enter the value of y1 coordinate of A:
8
Not a valid square

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Articles: