Check if given four points form a square

Last Updated : 15 Jun, 2026

Given an 2D array points[] which represents coordinates of four points in a plane. Find if the four points can form a square or not. Return true if they form a square else return false.

Examples:

Input: points[] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}}
Output: true
Explanation: These points form a square which can be clearly seen in the below image.

a1

Input: points[] = {{0, 0}, {1, 1}, {1, 0}, {0, 2}}
Output: false
Explanation: These four points do not form a square.

a2
Try It Yourself
redirect icon

To check for square, we need to check for following. 
a) All four sides formed by points are the same. 
b) The angle between any two sides is 90 degree. (This condition is required as Rhombus also has same sides) 
c) Check both the diagonals have the same distance.

The idea is to pick any point and calculate its distance from the rest of the points. Let the picked point be 'p'. To form a square, the distance of two points must be the same from 'p', let this distance be d. The distance from one point must be different from that d and must be equal to √2 times d (or distance square should be equal to 2 * d2). Let this point with different distance be 'q'. 

The above condition is not good enough as the point with a different distance can be on the other side. We also need to check that q is at the same distance from 2 other points and this distance is the same as d.

Here in the code below for the sake of simplicity we are calculating distance as (x1-x2)2 +(y1-y2)2. WE ARE NOT SQUARE ROOTING IT !

C++
#include <iostream>
#include <vector>

using namespace std;

// Returns the squared Euclidean distance between two points.
// Squared distance is sufficient because we only need comparisons
// and it avoids using sqrt().
int distanceSq(int a, int b, int p, int q)
{
    return (p - a) * (p - a) + (q - b) * (q - b);
}

// Checks whether the given four points form a valid square.
bool isSquare(vector<vector<int>> points)
{
    // A valid square cannot have overlapping points.
    for (int i = 0; i < 4; i++)
    {
        for (int j = i + 1; j < 4; j++)
        {
            if (points[i][0] == points[j][0] && points[i][1] == points[j][1])
            {
                return false;
            }
        }
    }

    // Compute distances from the first point to the other three points.
    int d2 = distanceSq(points[0][0], points[0][1], points[1][0], points[1][1]);

    int d3 = distanceSq(points[0][0], points[0][1], points[2][0], points[2][1]);

    int d4 = distanceSq(points[0][0], points[0][1], points[3][0], points[3][1]);

    // Case 1:
    // point[1] and point[2] are adjacent vertices,
    // point[3] is diagonally opposite to point[0].
    if (d2 == d3 && 2 * d2 == d4)
    {
        // Remaining three sides must also be equal.
        int d = distanceSq(points[1][0], points[1][1], points[3][0], points[3][1]);

        if ((d == distanceSq(points[2][0], points[2][1], points[3][0], points[3][1]) && d == d2) == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Case 2:
    // point[2] and point[3] are adjacent vertices,
    // point[1] is diagonally opposite to point[0].
    if (d3 == d4 && 2 * d3 == d2)
    {
        int d = distanceSq(points[1][0], points[1][1], points[2][0], points[2][1]);

        if ((d == distanceSq(points[1][0], points[1][1], points[3][0], points[3][1]) && d == d3) == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Case 3:
    // point[1] and point[3] are adjacent vertices,
    // point[2] is diagonally opposite to point[0].
    if (d2 == d4 && 2 * d2 == d3)
    {
        int d = distanceSq(points[1][0], points[1][1], points[2][0], points[2][1]);

        if ((d == distanceSq(points[2][0], points[2][1], points[3][0], points[3][1]) && d == d2) == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // None of the three configurations satisfied square properties.
    return false;
}

int main()
{
    vector<vector<int>> points = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

    if (isSquare(points))
        cout << "true" << endl;
    else
        cout << "false" << endl;

    return 0;
}
Java
class GFG {

    // Returns the squared Euclidean distance between two
    // points. Squared distance is sufficient because we
    // only need comparisons and it avoids using sqrt().
    static int distanceSq(int a, int b, int p, int q)
    {
        return (p - a) * (p - a) + (q - b) * (q - b);
    }

    // Checks whether the given four points form a valid
    // square.
    static boolean isSquare(int[][] points)
    {
        // A valid square cannot have overlapping points.
        for (int i = 0; i < 4; i++) {
            for (int j = i + 1; j < 4; j++) {
                if (points[i][0] == points[j][0]
                    && points[i][1] == points[j][1]) {
                    return false;
                }
            }
        }

        // Compute distances from the first point to the
        // other three points.
        int d2 = distanceSq(points[0][0], points[0][1],
                            points[1][0], points[1][1]);

        int d3 = distanceSq(points[0][0], points[0][1],
                            points[2][0], points[2][1]);

        int d4 = distanceSq(points[0][0], points[0][1],
                            points[3][0], points[3][1]);

        // Case 1:
        // point[1] and point[2] are adjacent vertices,
        // point[3] is diagonally opposite to point[0].
        if (d2 == d3 && 2 * d2 == d4) {

            // Remaining three sides must also be equal.
            int d = distanceSq(points[1][0], points[1][1],
                               points[3][0], points[3][1]);

            return d == distanceSq(points[2][0], points[2][1],
                              points[3][0], points[3][1]) && d == d2;
        }

        // Case 2:
        // point[2] and point[3] are adjacent vertices,
        // point[1] is diagonally opposite to point[0].
        if (d3 == d4 && 2 * d3 == d2) {

            int d = distanceSq(points[1][0], points[1][1],
                               points[2][0], points[2][1]);

            return d == distanceSq(points[1][0], points[1][1],
                              points[3][0], points[3][1]) && d == d3;
        }

        // Case 3:
        // point[1] and point[3] are adjacent vertices,
        // point[2] is diagonally opposite to point[0].
        if (d2 == d4 && 2 * d2 == d3) {

            int d = distanceSq(points[1][0], points[1][1],
                               points[2][0], points[2][1]);

            return d == distanceSq(points[2][0], points[2][1],
                              points[3][0], points[3][1]) && d == d2;
        }

        // None of the three configurations satisfied square
        // properties.
        return false;
    }

    public static void main(String[] args)
    {
        int[][] points
            = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } };

        System.out.println(isSquare(points) ? "true"  : "false");
    }
}
Python
# Returns the squared Euclidean distance between two points.
# Squared distance is sufficient because we only need comparisons
# and it avoids using sqrt().
def distance_sq(a, b, p, q):
    return (p - a) ** 2 + (q - b) ** 2


# Checks whether the given four points form a valid square.
def isSquare(points):

    # A valid square cannot have overlapping points.
    for i in range(4):
        for j in range(i + 1, 4):
            if points[i][0] == points[j][0] and points[i][1] == points[j][1]:
                return False

    # Compute distances from the first point to the other three points.
    d2 = distance_sq(points[0][0], points[0][1],
                     points[1][0], points[1][1])

    d3 = distance_sq(points[0][0], points[0][1],
                     points[2][0], points[2][1])

    d4 = distance_sq(points[0][0], points[0][1],
                     points[3][0], points[3][1])

    # Case 1:
    # point[1] and point[2] are adjacent vertices,
    # point[3] is diagonally opposite to point[0].
    if d2 == d3 and 2 * d2 == d4:

        # Remaining three sides must also be equal.
        d = distance_sq(points[1][0], points[1][1],
                        points[3][0], points[3][1])

        return (d == distance_sq(points[2][0], points[2][1],
                                 points[3][0], points[3][1])
                and d == d2)

    # Case 2:
    # point[2] and point[3] are adjacent vertices,
    # point[1] is diagonally opposite to point[0].
    if d3 == d4 and 2 * d3 == d2:

        d = distance_sq(points[1][0], points[1][1],
                        points[2][0], points[2][1])

        return (d == distance_sq(points[1][0], points[1][1],
                                 points[3][0], points[3][1])
                and d == d3)

    # Case 3:
    # point[1] and point[3] are adjacent vertices,
    # point[2] is diagonally opposite to point[0].
    if d2 == d4 and 2 * d2 == d3:

        d = distance_sq(points[1][0], points[1][1],
                        points[2][0], points[2][1])

        return (d == distance_sq(points[2][0], points[2][1],
                                 points[3][0], points[3][1])
                and d == d2)

    # None of the three configurations satisfied square properties.
    return False


# Driver Code
if __name__ == "__main__":
    points = [[0, 0], [0, 1], [1, 0], [1, 1]]

    print("true" if isSquare(points) else "false")
C#
using System;
using System.Collections.Generic;

class GFG {
    // Returns the squared Euclidean distance between two
    // points. Squared distance is sufficient because we
    // only need comparisons and it avoids using sqrt().
    static int DistanceSq(int a, int b, int p, int q)
    {
        return (p - a) * (p - a) + (q - b) * (q - b);
    }

    // Checks whether the given four points form a valid
    // square.
    static bool isSquare(List<List<int> > points)
    {
        // A valid square cannot have overlapping points.
        for (int i = 0; i < 4; i++) {
            for (int j = i + 1; j < 4; j++) {
                if (points[i][0] == points[j][0]
                    && points[i][1] == points[j][1]) {
                    return false;
                }
            }
        }

        // Compute distances from the first point to the
        // other three points.
        int d2 = DistanceSq(points[0][0], points[0][1],
                            points[1][0], points[1][1]);

        int d3 = DistanceSq(points[0][0], points[0][1],
                            points[2][0], points[2][1]);

        int d4 = DistanceSq(points[0][0], points[0][1],
                            points[3][0], points[3][1]);

        // Case 1:
        // point[1] and point[2] are adjacent vertices,
        // point[3] is diagonally opposite to point[0].
        if (d2 == d3 && 2 * d2 == d4) {
            int d = DistanceSq(points[1][0], points[1][1],
                               points[3][0], points[3][1]);

            return d
                == DistanceSq(points[2][0], points[2][1],
                              points[3][0], points[3][1])
                && d == d2;
        }

        // Case 2:
        // point[2] and point[3] are adjacent vertices,
        // point[1] is diagonally opposite to point[0].
        if (d3 == d4 && 2 * d3 == d2) {
            int d = DistanceSq(points[1][0], points[1][1],
                               points[2][0], points[2][1]);

            return d
                == DistanceSq(points[1][0], points[1][1],
                              points[3][0], points[3][1])
                && d == d3;
        }

        // Case 3:
        // point[1] and point[3] are adjacent vertices,
        // point[2] is diagonally opposite to point[0].
        if (d2 == d4 && 2 * d2 == d3) {
            int d = DistanceSq(points[1][0], points[1][1],
                               points[2][0], points[2][1]);

            return d
                == DistanceSq(points[2][0], points[2][1],
                              points[3][0], points[3][1])
                && d == d2;
        }

        // None of the three configurations satisfied square
        // properties.
        return false;
    }

    static void Main()
    {
        List<List<int> > points = new List<List<int> >{
            new List<int>{ 0, 0 }, new List<int>{ 0, 1 },
            new List<int>{ 1, 0 }, new List<int>{ 1, 1 }
        };

        Console.WriteLine(isSquare(points) ? "true"
                                           : "false");
    }
}
JavaScript
// Returns the squared Euclidean distance between two
// points. Squared distance is sufficient because we only
// need comparisons and it avoids using Math.sqrt().
function distanceSq(a, b, p, q)
{
    return (p - a) * (p - a) + (q - b) * (q - b);
}

// Checks whether the given four points form a valid square.
function isSquare(points)
{

    // A valid square cannot have overlapping points.
    for (let i = 0; i < 4; i++) {
        for (let j = i + 1; j < 4; j++) {
            if (points[i][0] === points[j][0]
                && points[i][1] === points[j][1]) {
                return false;
            }
        }
    }

    // Compute distances from the first point to the other
    // three points.
    let d2 = distanceSq(points[0][0], points[0][1],
                        points[1][0], points[1][1]);

    let d3 = distanceSq(points[0][0], points[0][1],
                        points[2][0], points[2][1]);

    let d4 = distanceSq(points[0][0], points[0][1],
                        points[3][0], points[3][1]);

    // Case 1:
    // point[1] and point[2] are adjacent vertices,
    // point[3] is diagonally opposite to point[0].
    if (d2 === d3 && 2 * d2 === d4) {

        let d = distanceSq(points[1][0], points[1][1],
                           points[3][0], points[3][1]);

        return d === distanceSq(
                       points[2][0], points[2][1],
                       points[3][0], points[3][1])
               && d === d2;
    }

    // Case 2:
    // point[2] and point[3] are adjacent vertices,
    // point[1] is diagonally opposite to point[0].
    if (d3 === d4 && 2 * d3 === d2) {

        let d = distanceSq(points[1][0], points[1][1],
                           points[2][0], points[2][1]);

        return d === distanceSq(
                       points[1][0], points[1][1],
                       points[3][0], points[3][1])
               && d === d3;
    }

    // Case 3:
    // point[1] and point[3] are adjacent vertices,
    // point[2] is diagonally opposite to point[0].
    if (d2 === d4 && 2 * d2 === d3) {

        let d = distanceSq(points[1][0], points[1][1],
                           points[2][0], points[2][1]);

        return d === distanceSq(
                       points[2][0], points[2][1],
                       points[3][0], points[3][1])
               && d === d2;
    }

    // None of the three configurations satisfied square
    // properties.
    return false;
}

// Driver Code
const points = [ [ 0, 0 ], [ 0, 1 ], [ 1, 0 ], [ 1, 1 ] ];

console.log(isSquare(points) ? "true" : "false");

Output
true

Time Complexity: O(1), all operations are being carried out in O(1) constant time.
Auxiliary Space: O(1), no extra space required

Extended Problem: 

Comment