How to check if given four points form a square
Last Updated :
25 Oct, 2022
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++
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
bool isSquare(Point p1, Point p2, Point p3, Point p4)
{
int d2 = distSq(p1, p2);
int d3 = distSq(p1, p3);
int d4 = distSq(p1, p4);
if (d2 == 0 || d3 == 0 || d4 == 0)
return false ;
if (d2 == d3 && 2 * d2 == d4
&& 2 * distSq(p2, p4) == distSq(p2, p3)) {
return true ;
}
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 ;
}
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
class GFG
{
static class Point
{
int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
static boolean isSquare(Point p1, Point p2, Point p3, Point p4)
{
int d2 = distSq(p1, p2);
int d3 = distSq(p1, p3);
int d4 = distSq(p1, p4);
if (d2 == 0 || d3 == 0 || d4 == 0 )
return false ;
if (d2 == d3 && 2 * d2 == d4
&& 2 * distSq(p2, p4) == distSq(p2, p3))
{
return true ;
}
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 ;
}
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" );
}
}
|
Python3
class Point:
def __init__( self , x, y):
self .x = x
self .y = y
def distSq(p, q):
return (p.x - q.x) * (p.x - q.x) + \
(p.y - q.y) * (p.y - q.y)
def isSquare(p1, p2, p3, p4):
d2 = distSq(p1, p2)
d3 = distSq(p1, p3)
d4 = distSq(p1, p4)
if d2 = = 0 or d3 = = 0 or d4 = = 0 :
return False
if d2 = = d3 and 2 * d2 = = d4 and \
2 * distSq(p2, p4) = = distSq(p2, p3):
return True
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
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' )
|
C#
using System;
class GFG
{
class Point
{
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
static bool isSquare(Point p1, Point p2, Point p3, Point p4)
{
int d2 = distSq(p1, p2);
int d3 = distSq(p1, p3);
int d4 = distSq(p1, p4);
if (d2 == 0 || d3 == 0 || d4 == 0)
return false ;
if (d2 == d3 && 2 * d2 == d4
&& 2 * distSq(p2, p4) == distSq(p2, p3))
{
return true ;
}
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 ;
}
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" );
}
}
|
Javascript
<script>
function distSq( p, q){
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
function isSquare(p1, p2, p3, p4){
let d2 = distSq(p1, p2);
let d3 = distSq(p1, p3);
let d4 = distSq(p1, p4);
if (d2 == 0 || d3 == 0 || d4 == 0)
return false ;
if (d2 == d3 && 2 * d2 == d4
&& 2 * distSq(p2, p4) == distSq(p2, p3)) {
return true ;
}
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 ;
}
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" );
</script>
|
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...