Open In App

How to Check if Given Four Points form a Square or not in JavaScript ?

Last Updated : 21 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given the coordinates of four points in a plane, find if the four points form a square or not. 

To check for square, we need to check for following. 
a) All four sides formed by points are the same. 
b) Check both the diagonals have the same distance.

Table of Content

Approach 1: Geometric method

  • Calculating the distances between all pairs of points.
  • Checking if there are four equal side lengths and two equal diagonal lengths.
  • If the four side lengths are equal and the diagonals are equal and twice the length of a side, the points form a square.

Example: In this example, we have used Geometric method to check if given four points form a square or not.

Javascript




// Function to calculate the distance
// between two points
function calculateDistance(point1, point2){
    const dx = point1.x - point2.x;
    const dy = point1.y - point2.y;
    return (dx * dx + dy * dy);
}
 
// Function to check if four points form a
// square
function arePointsFormingSquare(point1,
    point2, point3, point4){
    // Calculate distances between all pairs
    // of points
    const distances = [
        calculateDistance(point1, point2),
        calculateDistance(point2, point3),
        calculateDistance(point3, point4),
        calculateDistance(point4, point1),
        calculateDistance(point1, point3),
        calculateDistance(point2, point4),
    ];
 
    // Sort distances to group equal
    // lengths together
    distances.sort((a, b) => a - b);
 
    // Check if we have 4 equal side
    // lengths and 2 equal diagonal lengths
    return (
     
        // Check if the first two sides are equal
        distances[0] === distances[1] &&
         
        // Check if the second two sides are equal
        distances[1] === distances[2] &&
         
        // Check if the third two sides are equal
        distances[2] === distances[3] &&
         
        // Check if the diagonals are equal
        distances[4] === distances[5] &&
         
        // Check if the diagonals are
        //    twice the length of a side
        distances[0] * 2 === distances[4]
    );
}
 
// Example usage
const point1 = { x: 0, y: 0 };
const point2 = { x: 1, y: 0 };
const point3 = { x: 1, y: 1 };
const point4 = { x: 0, y: 1 };
 
const result = arePointsFormingSquare(
    point1, point2, point3, point4);
console.log(result);


Output:

true

Approach 2: Using Set:

  • Computes squared distance between two points using the Euclidean formula.
  • Calculate the distance of two opposite sides(i.e. calculate diagonol length)
  • Set is used to store all the distances.
  • If the size of the set is 2, the conditions for forming a square are satisfied.

Example: In this example, we have used Set data structure to check if given four points form a square or not.

Javascript




function distanceSquared(point1, point2) {
    return Math.pow(point1[0] - point2[0], 2) +
    Math.pow(point1[1] - point2[1], 2);
}
 
function isSquare(points) {
    if (points.length !== 4) return false;
 
    const distances = new Set();
 
    for (let i = 0; i < 4; i++) {
        for (let j = i + 1; j < 4; j++) {
            distances.add(distanceSquared(points[i],
            points[j]));
        }
    }
 
    // A square has exactly 2 unique
    //distances (side length and diagonal length)
    return distances.size === 2;
}
 
const points = [[0, 0], [0, 1], [1, 0], [1, 1]];
console.log(isSquare(points));


Output:

true


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads