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 Rhombusalso 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++
// A C++ program to check if four given points form a square or not.#include<iostream>usingnamespacestd;// Structure of a point in 2D spacestructPoint{intx,y;};// A utility function to find square of distance// from point 'p' to point 'q'intdistSq(Pointp,Pointq){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 falseboolisSquare(Pointp1,Pointp2,Pointp3,Pointp4){intd2=distSq(p1,p2);// from p1 to p2intd3=distSq(p1,p3);// from p1 to p3intd4=distSq(p1,p4);// from p1 to p4if(d2==0||d3==0||d4==0)returnfalse;// 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)){returntrue;}// The below two cases are similar to above caseif(d3==d4&&2*d3==d2&&2*distSq(p3,p2)==distSq(p3,p4)){returntrue;}if(d2==d4&&2*d2==d3&&2*distSq(p2,p3)==distSq(p2,p4)){returntrue;}returnfalse;}// Driver program to test above functionintmain(){Pointp1={20,10},p2={10,20},p3={20,20},p4={10,10};isSquare(p1,p2,p3,p4)?cout<<"Yes":cout<<"No";return0;}
// A C++ program to check if four given points form a square or not.
// A Java program to check if four given points form a// square or not.classGFG{// Structure of a point in 2D spacestaticclassPoint{intx,y;publicPoint(intx,inty){this.x=x;this.y=y;}};// A utility function to find square of distance// from point 'p' to point 'q'staticintdistSq(Pointp,Pointq){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 falsestaticbooleanisSquare(Pointp1,Pointp2,Pointp3,Pointp4){intd2=distSq(p1,p2);// from p1 to p2intd3=distSq(p1,p3);// from p1 to p3intd4=distSq(p1,p4);// from p1 to p4if(d2==0||d3==0||d4==0)returnfalse;// 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)){returntrue;}// The below two cases are similar to above caseif(d3==d4&&2*d3==d2&&2*distSq(p3,p2)==distSq(p3,p4)){returntrue;}if(d2==d4&&2*d2==d3&&2*distSq(p2,p3)==distSq(p2,p4)){returntrue;}returnfalse;}// Driver codepublicstaticvoidmain(String[]args){Pointp1=newPoint(20,10),p2=newPoint(10,20),p3=newPoint(20,20),p4=newPoint(10,10);System.out.println(isSquare(p1,p2,p3,p4)==true?"Yes":"No");}}
Python
# A Python3 program to check if# four given points form a square or not.classPoint:# Structure of a point in 2D spacedef__init__(self,x,y):self.x=xself.y=y# A utility function to find square of# distance from point 'p' to point 'q'defdistSq(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 falsedefisSquare(p1,p2,p3,p4):d2=distSq(p1,p2)# from p1 to p2d3=distSq(p1,p3)# from p1 to p3d4=distSq(p1,p4)# from p1 to p4ifd2==0ord3==0ord4==0:returnFalse# 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)ifd2==d3and2*d2==d4and \
2*distSq(p2,p4)==distSq(p2,p3):returnTrue# The below two cases are similar to above caseifd3==d4and2*d3==d2and \
2*distSq(p3,p2)==distSq(p3,p4):returnTrueifd2==d4and2*d2==d3and \
2*distSq(p2,p3)==distSq(p2,p4):returnTruereturnFalse# Driver Codeif__name__=="__main__":p1=Point(20,10)p2=Point(10,20)p3=Point(20,20)p4=Point(10,10)ifisSquare(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.usingSystem;classGFG{// Structure of a point in 2D spaceclassPoint{publicintx,y;publicPoint(intx,inty){this.x=x;this.y=y;}};// A utility function to find square of distance// from point 'p' to point 'q'staticintdistSq(Pointp,Pointq){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 falsestaticboolisSquare(Pointp1,Pointp2,Pointp3,Pointp4){intd2=distSq(p1,p2);// from p1 to p2intd3=distSq(p1,p3);// from p1 to p3intd4=distSq(p1,p4);// from p1 to p4if(d2==0||d3==0||d4==0)returnfalse;// 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)){returntrue;}// The below two cases are similar to above caseif(d3==d4&&2*d3==d2&&2*distSq(p3,p2)==distSq(p3,p4)){returntrue;}if(d2==d4&&2*d2==d3&&2*distSq(p2,p3)==distSq(p2,p4)){returntrue;}returnfalse;}// Driver codepublicstaticvoidMain(String[]args){Pointp1=newPoint(20,10),p2=newPoint(10,20),p3=newPoint(20,20),p4=newPoint(10,10);Console.WriteLine(isSquare(p1,p2,p3,p4)==true?"Yes":"No");}}
JavaScript
// 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'functiondistSq(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 falsefunctionisSquare(p1,p2,p3,p4){letd2=distSq(p1,p2);// from p1 to p2letd3=distSq(p1,p3);// from p1 to p3letd4=distSq(p1,p4);// from p1 to p4if(d2==0||d3==0||d4==0)returnfalse;// 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)){returntrue;}// The below two cases are similar to above caseif(d3==d4&&2*d3==d2&&2*distSq(p3,p2)==distSq(p3,p4)){returntrue;}if(d2==d4&&2*d2==d3&&2*distSq(p2,p3)==distSq(p2,p4)){returntrue;}returnfalse;}// Driver program to test above functionletp1={x:20,y:10}letp2={x:10,y:20}letp3={x:20,y:20}letp4={x:10,y:10}isSquare(p1,p2,p3,p4)?console.log("Yes"):console.log("No");
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
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.