Open In App

How to Check if Given Four Points Form a Square using JavaScript ?

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A square is a two-dimensional shape consisting of four sides in which all sides are equal and diagonals are also equal. The angle between any two adjacent sides is 90 degrees. We are going to learn How we can check if given four points form a square using JavaScript.

Below are the approaches to check if given four points form a square using JavaScript:

Distance Check

In this approach, Create a function to calculate the squared distance between two points. The distance formula for the two points between is (distance)2 = (x2 – x1)^2 + (y2 – y1)^2. Create another function to check if the given points form a square. It calculates the squared distances between all pairs of points and sorts them. Check the properties of the square i.e. all distances are equal and diagonals are equal.

Example: To demonstrate checking if given four points form a square using the Distance Check.

JavaScript
function distanceSquared(p1, p2) {
    return Math.pow(p2[0] - p1[0], 2) 
        + Math.pow(p2[1] - p1[1], 2);
}

function isSquare(p1, p2, p3, p4) {
    const distances = [
        distanceSquared(p1, p2),
        distanceSquared(p1, p3),
        distanceSquared(p1, p4),
        distanceSquared(p2, p3),
        distanceSquared(p2, p4),
        distanceSquared(p3, p4)
    ];

    distances.sort((a, b) => a - b);
    return (
        distances[0] > 0 &&
        distances[0] === distances[1] &&
        distances[0] === distances[2] &&
        distances[0] === distances[3] &&
        distances[4] === distances[5]
    );
}
const p1 = [0, 0];
const p2 = [0, 1];
const p3 = [1, 1];
const p4 = [1, 0];
console.log(isSquare(p1, p2, p3, p4)); 

Output
true

Time Complexity: O(1)

Space Complexity: O(1)

Mid-point Check

In this approach, Calculate the squared distance between two points and calculate the midpoint between two points. Define a function to check if four given points could form a square and calculate squared distances from the first point to the other three points, if any distances are zero return false. Check three cases for potential square formation based on distance relationships, if any condition is met return true if none of any condition is met return false.

Example: To demonstrate checking if given four points form a square using Mid-point Check.

JavaScript
function distanceSquared(p1, p2) {
    const dx = p1.x - p2.x;
    const dy = p1.y - p2.y;
    return dx * dx + dy * dy;
}
function midpoint(p1, p2) {
    return {
        x: (p1.x + p2.x) / 2,
        y: (p1.y + p2.y) / 2
    };
}
function isSquare(p1, p2, p3, p4) {
    const d2 = distanceSquared(p1, p2);
    const d3 = distanceSquared(p1, p3);
    const d4 = distanceSquared(p1, p4);
    if (d2 === 0 || d3 === 0 || d4 === 0) return false;
    if (d2 === d3 && 2 * d2 === d4) {
        const d = distanceSquared(p2, p4);
        return (d === distanceSquared(p3, p4) && d === d2);
    }
    if (d3 === d4 && 2 * d3 === d2) {
        const d = distanceSquared(p2, p3);
        return (d === distanceSquared(p2, p4) && d === d3);
    }
    if (d2 === d4 && 2 * d2 === d3) {
        const d = distanceSquared(p2, p3);
        return (d === distanceSquared(p3, p4) && d === d2);
    }
    return false;
}
const p1 = { x: 0, y: 0 };
const p2 = { x: 0, y: 1 };
const p3 = { x: 1, y: 1 };
const p4 = { x: 1, y: 0 };
console.log(isSquare(p1, p2, p3, p4)); 

Output
true

Time Complexity: O(1)

Space Complexity: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads