Open In App

How to check if given four points form a square

Last Updated : 25 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given 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) 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

Examples:

Input: p1 = { 20, 10 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }
Output: Yes
Explanation: 

 

Input: p1 = { 20, 20 }, p2 = { 10, 20 }, p3 = { 20, 20 }, p4 = { 10, 10 }
Output:  No

 

Approach: 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. 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.

Below are the implementations of the above idea. 

C++




// A C++ program to check if four given points form a square or not.
#include <iostream>
using namespace std;
 
// Structure of a point in 2D space
struct Point {
    int x, y;
};
 
// A utility function to find square of distance
// from point 'p' to point 'q'
int distSq(Point p, Point q)
{
    return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
 
// This function returns true if (p1, p2, p3, p4) form a
// square, otherwise false
bool isSquare(Point p1, Point p2, Point p3, Point p4)
{
    int d2 = distSq(p1, p2); // from p1 to p2
    int d3 = distSq(p1, p3); // from p1 to p3
    int d4 = distSq(p1, p4); // from p1 to p4
 
    if (d2 == 0 || d3 == 0 || d4 == 0)   
        return false;
 
    // If lengths if (p1, p2) and (p1, p3) are same, then
    // following conditions must met to form a square.
    // 1) Square of length of (p1, p4) is same as twice
    // the square of (p1, p2)
    // 2) Square of length of (p2, p3) is same
    // as twice the square of (p2, p4)
 
    if (d2 == d3 && 2 * d2 == d4
        && 2 * distSq(p2, p4) == distSq(p2, p3)) {
        return true;
    }
 
    // The below two cases are similar to above case
    if (d3 == d4 && 2 * d3 == d2
        && 2 * distSq(p3, p2) == distSq(p3, p4)) {
        return true;
    }
    if (d2 == d4 && 2 * d2 == d3
        && 2 * distSq(p2, p3) == distSq(p2, p4)) {
        return true;
    }
 
    return false;
}
 
// Driver program to test above function
int main()
{
    Point p1 = { 20, 10 }, p2 = { 10, 20 },
          p3 = { 20, 20 }, p4 = { 10, 10 };
    isSquare(p1, p2, p3, p4) ? cout << "Yes" : cout << "No";
    return 0;
}


Java




// A Java program to check if four given points form a square or not.
 
class GFG
{
 
// Structure of a point in 2D space
static class Point
{
    int x, y;
 
        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
     
};
 
// A utility 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);
}
 
// This function returns true if (p1, p2, p3, p4) form a
// square, otherwise false
static boolean isSquare(Point p1, Point p2, Point p3, Point p4)
{
    int d2 = distSq(p1, p2); // from p1 to p2
    int d3 = distSq(p1, p3); // from p1 to p3
    int d4 = distSq(p1, p4); // from p1 to p4
 
    if (d2 == 0 || d3 == 0 || d4 == 0)   
        return false;
 
    // If lengths if (p1, p2) and (p1, p3) are same, then
    // following conditions must met to form a square.
    // 1) Square of length of (p1, p4) is same as twice
    // the square of (p1, p2)
    // 2) Square of length of (p2, p3) is same
    // as twice the square of (p2, p4)
 
    if (d2 == d3 && 2 * d2 == d4
        && 2 * distSq(p2, p4) == distSq(p2, p3))
    {
        return true;
    }
 
    // The below two cases are similar to above case
    if (d3 == d4 && 2 * d3 == d2
        && 2 * distSq(p3, p2) == distSq(p3, p4))
    {
        return true;
    }
    if (d2 == d4 && 2 * d2 == d3
        && 2 * distSq(p2, p3) == distSq(p2, p4))
    {
        return true;
    }
 
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    Point p1 = new Point(20, 10), p2 = new Point( 10, 20 ),
        p3 = new Point(20, 20 ), p4 = new Point( 10, 10 );
    System.out.println(isSquare(p1, p2, p3, p4)==true ? "Yes" : "No");
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# A Python3 program to check if
# four given points form a square or not.
class Point:
     
    # Structure of a point in 2D space
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
# A utility function to find square of
# distance from point 'p' to point 'q'
def distSq(p, q):
    return (p.x - q.x) * (p.x - q.x) +\
           (p.y - q.y) * (p.y - q.y)
 
# This function returns true if (p1, p2, p3, p4)
# form a square, otherwise false
def isSquare(p1, p2, p3, p4):
 
    d2 = distSq(p1, p2) # from p1 to p2
    d3 = distSq(p1, p3) # from p1 to p3
    d4 = distSq(p1, p4) # from p1 to p4
 
    if d2 == 0 or d3 == 0 or d4 == 0:   
        return False
 
    # If lengths if (p1, p2) and (p1, p3) are same, then
    # following conditions must be met to form a square.
    # 1) Square of length of (p1, p4) is same as twice
    # the square of (p1, p2)
    # 2) Square of length of (p2, p3) is same
    # as twice the square of (p2, p4)
 
    if d2 == d3 and 2 * d2 == d4 and \
                    2 * distSq(p2, p4) == distSq(p2, p3):
        return True
 
    # The below two cases are similar to above case
    if d3 == d4 and 2 * d3 == d2 and \
                    2 * distSq(p3, p2) == distSq(p3, p4):
        return True
 
    if d2 == d4 and 2 * d2 == d3 and \
                    2 * distSq(p2, p3) == distSq(p2, p4):
        return True
 
    return False
 
# Driver Code
if __name__=="__main__":
    p1 = Point(20, 10)
    p2 = Point(10, 20)
    p3 = Point(20, 20)
    p4 = Point(10, 10)
     
    if isSquare(p1, p2, p3, p4):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by Mayank Chaudhary
# aka chaudhary_19


C#




// A C# program to check if four given points form a square or not.
using System;
 
class GFG
{
 
// Structure of a point in 2D space
class Point
{
    public int x, y;
 
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
     
};
 
// A utility 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);
}
 
// This function returns true if (p1, p2, p3, p4) form a
// square, otherwise false
static bool isSquare(Point p1, Point p2, Point p3, Point p4)
{
    int d2 = distSq(p1, p2); // from p1 to p2
    int d3 = distSq(p1, p3); // from p1 to p3
    int d4 = distSq(p1, p4); // from p1 to p4
 
    if (d2 == 0 || d3 == 0 || d4 == 0)   
        return false;
 
    // If lengths if (p1, p2) and (p1, p3) are same, then
    // following conditions must met to form a square.
    // 1) Square of length of (p1, p4) is same as twice
    // the square of (p1, p2)
    // 2) Square of length of (p2, p3) is same
    // as twice the square of (p2, p4)
    if (d2 == d3 && 2 * d2 == d4
        && 2 * distSq(p2, p4) == distSq(p2, p3))
    {
        return true;
    }
 
    // The below two cases are similar to above case
    if (d3 == d4 && 2 * d3 == d2
        && 2 * distSq(p3, p2) == distSq(p3, p4))
    {
        return true;
    }
    if (d2 == d4 && 2 * d2 == d3
        && 2 * distSq(p2, p3) == distSq(p2, p4))
    {
        return true;
    }
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    Point p1 = new Point(20, 10), p2 = new Point(10, 20),
        p3 = new Point(20, 20), p4 = new Point(10, 10);
    Console.WriteLine(isSquare(p1, p2, p3, p4) == true ? "Yes" : "No");
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// JavaScript program to check if four given points form a square or not.
 
// A utility function to find square of distance
// from point 'p' to point 'q'
function distSq( p, q){
    return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
 
// This function returns true if (p1, p2, p3, p4) form a
// square, otherwise false
function isSquare(p1,  p2,  p3, p4){
    let d2 = distSq(p1, p2); // from p1 to p2
    let d3 = distSq(p1, p3); // from p1 to p3
    let d4 = distSq(p1, p4); // from p1 to p4
 
    if (d2 == 0 || d3 == 0 || d4 == 0)   
        return false;
 
    // If lengths if (p1, p2) and (p1, p3) are same, then
    // following conditions must met to form a square.
    // 1) Square of length of (p1, p4) is same as twice
    // the square of (p1, p2)
    // 2) Square of length of (p2, p3) is same
    // as twice the square of (p2, p4)
 
    if (d2 == d3 && 2 * d2 == d4
        && 2 * distSq(p2, p4) == distSq(p2, p3)) {
        return true;
    }
 
    // The below two cases are similar to above case
    if (d3 == d4 && 2 * d3 == d2
        && 2 * distSq(p3, p2) == distSq(p3, p4)) {
        return true;
    }
    if (d2 == d4 && 2 * d2 == d3
        && 2 * distSq(p2, p3) == distSq(p2, p4)) {
        return true;
    }
 
    return false;
}
 
// Driver program to test above function
let p1 = { x:20, y:10 }
let p2 = { x:10, y:20 }
let p3 = { x:20, y:20 }
let p4 = { x:10, y:10 }
isSquare(p1, p2, p3, p4) ? document.write("Yes") : document.write("No");
 
// This code is contributed by rohitsingh07052.
</script>


Output

Yes

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: 

 



Similar Reads

Find Four points such that they form a square whose sides are parallel to x and y axes
Given 'n' pair of points, the task is to find four points such that they form a square whose sides are parallel to x and y axes or print "No such square" otherwise. If more than one square is possible then choose the one with the maximum area. Examples: Input : n = 6, points = (1, 1), (4, 4), (3, 4), (4, 3), (1, 4), (4, 1)Output : side of the squar
12 min read
Check if it is possible to travel all points in given time by moving in adjacent four directions
Given 3 arrays X[], Y[], and T[] all of the size N where X[i] and Y[i] represent the i-th coordinate and T[i] represents the time in seconds. Find it is possible to reach all the coordinates (X[i], Y[i]) in time T[i] from starting coordinates (0, 0). The pointer can move in four directions ( x +1, y ), ( x − 1, y ), ( x, y + 1) and (x, y − 1). From
13 min read
Check whether four points make a parallelogram
Given four points in a 2-dimensional space we need to find out whether they make a parallelogram or not.   A parallelogram has four sides. Two opposite sides are parallel and are of same lengths.   Examples: Points = [(0, 0), (4, 0), (1, 3), (5, 3)]Above points make a parallelogram.Points = [(0, 0), (2, 0), (4, 0), (2, 2)]Above points does not make
17 min read
Check if four segments form a rectangle
We are given four segments as a pair of coordinates of their end points. We need to tell whether those four line segments make a rectangle or not. Examples: Input : segments[] = [(4, 2), (7, 5), (2, 4), (4, 2), (2, 4), (5, 7), (5, 7), (7, 5)] Output : Yes Given these segment make a rectangle of length 3X2. Input : segment[] = [(7, 0), (10, 0), (7,
9 min read
Euler's Four Square Identity
According to Euler's four square identity, the product of any two numbers a and b can be expressed as a sum of four squares if a and b both can individually be expressed as the sum of four squares.Mathematically, if a = [Tex]c1^2 + c2^2 + c3^2 + c4^2 [/Tex]and b = [Tex]d1^2 + d2^2 + d3^2 + d4^2 [/Tex]Then, a * b = [Tex]e1^2 + e2^2 + e3^2 + e4^2 [/T
26 min read
Lagrange's four square theorem
Lagrange's Four Square Theorem states that every natural number can be written as sum of squares of four non negative integers. For eg. [Tex]1 = 0^2 + 0^2 + 0^2 +1^2 [/Tex]Similarly [Tex]2 = 0^2 + 0^2 + 1^2 +1^2 [/Tex]Similarly for any [Tex]n\in N, n = a^2 + b^2 + c^2 + d^2 where \ a, b, c, d, \in N \cup \{0\} [/Tex] The above identity may be deriv
8 min read
Find the area of the shaded region formed by the intersection of four semicircles in a square
Given the length of the side of a square a, the task is to find the area of the shaded region formed by the intersection of four semicircles in a square as shown in the image below: Examples: Input: a = 10 Output: 57Input: a = 19 Output: 205.77 Approach: Area of the shaded region will be: Area(semicircle1) + Area(semicircle2) + Area(semicircle3) +
4 min read
Find the point on X-axis from given N points having least Sum of Distances from all other points
Given an array arr[] consisting of N integers, denoting N points lying on the X-axis, the task is to find the point which has the least sum of distances from all other points. Example: Input: arr[] = {4, 1, 5, 10, 2} Output: (4, 0) Explanation: Distance of 4 from rest of the elements = |4 - 1| + |4 - 5| + |4 - 10| + |4 - 2| = 12 Distance of 1 from
11 min read
Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
A circle is given with k equidistant points on its circumference. 2 points A and B are given in the circle. Find the count of all obtuse angles (angles larger than 90 degree) formed from /_ACB, where C can be any point in circle other than A or B. Note : A and B are not equal. A &lt; B. Points are between 1 and K(both inclusive). Examples : Input :
5 min read
Check if the given 2-D points form T-shape or not
Given the coordinates of 5 2-dimensional points, check if they form a closed T shape. Print 'Yes' if they form T shape and 'No' otherwise. Note: Coordinates should be distinct and integers.There are 4-types of T shaped formations possible according to the given conditions: Examples: Input: [[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]] Output: Yes Input:
12 min read
Article Tags :