Given an 2D array points[] which represents coordinates of four points in a plane. Find if the four points can form a square or not. Return true if they form a square else return false.
Examples:
Input: points[] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}} Output: true Explanation: These points form a square which can be clearly seen in the below image.
Input: points[] = {{0, 0}, {1, 1}, {1, 0}, {0, 2}} Output: false Explanation: These four points do not form a square.
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++
#include<iostream>#include<vector>usingnamespacestd;// Returns the squared Euclidean distance between two points.// Squared distance is sufficient because we only need comparisons// and it avoids using sqrt().intdistanceSq(inta,intb,intp,intq){return(p-a)*(p-a)+(q-b)*(q-b);}// Checks whether the given four points form a valid square.boolisSquare(vector<vector<int>>points){// A valid square cannot have overlapping points.for(inti=0;i<4;i++){for(intj=i+1;j<4;j++){if(points[i][0]==points[j][0]&&points[i][1]==points[j][1]){returnfalse;}}}// Compute distances from the first point to the other three points.intd2=distanceSq(points[0][0],points[0][1],points[1][0],points[1][1]);intd3=distanceSq(points[0][0],points[0][1],points[2][0],points[2][1]);intd4=distanceSq(points[0][0],points[0][1],points[3][0],points[3][1]);// Case 1:// point[1] and point[2] are adjacent vertices,// point[3] is diagonally opposite to point[0].if(d2==d3&&2*d2==d4){// Remaining three sides must also be equal.intd=distanceSq(points[1][0],points[1][1],points[3][0],points[3][1]);if((d==distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2)==true){returntrue;}else{returnfalse;}}// Case 2:// point[2] and point[3] are adjacent vertices,// point[1] is diagonally opposite to point[0].if(d3==d4&&2*d3==d2){intd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);if((d==distanceSq(points[1][0],points[1][1],points[3][0],points[3][1])&&d==d3)==true){returntrue;}else{returnfalse;}}// Case 3:// point[1] and point[3] are adjacent vertices,// point[2] is diagonally opposite to point[0].if(d2==d4&&2*d2==d3){intd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);if((d==distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2)==true){returntrue;}else{returnfalse;}}// None of the three configurations satisfied square properties.returnfalse;}intmain(){vector<vector<int>>points={{0,0},{0,1},{1,0},{1,1}};if(isSquare(points))cout<<"true"<<endl;elsecout<<"false"<<endl;return0;}
Java
classGFG{// Returns the squared Euclidean distance between two// points. Squared distance is sufficient because we// only need comparisons and it avoids using sqrt().staticintdistanceSq(inta,intb,intp,intq){return(p-a)*(p-a)+(q-b)*(q-b);}// Checks whether the given four points form a valid// square.staticbooleanisSquare(int[][]points){// A valid square cannot have overlapping points.for(inti=0;i<4;i++){for(intj=i+1;j<4;j++){if(points[i][0]==points[j][0]&&points[i][1]==points[j][1]){returnfalse;}}}// Compute distances from the first point to the// other three points.intd2=distanceSq(points[0][0],points[0][1],points[1][0],points[1][1]);intd3=distanceSq(points[0][0],points[0][1],points[2][0],points[2][1]);intd4=distanceSq(points[0][0],points[0][1],points[3][0],points[3][1]);// Case 1:// point[1] and point[2] are adjacent vertices,// point[3] is diagonally opposite to point[0].if(d2==d3&&2*d2==d4){// Remaining three sides must also be equal.intd=distanceSq(points[1][0],points[1][1],points[3][0],points[3][1]);returnd==distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2;}// Case 2:// point[2] and point[3] are adjacent vertices,// point[1] is diagonally opposite to point[0].if(d3==d4&&2*d3==d2){intd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd==distanceSq(points[1][0],points[1][1],points[3][0],points[3][1])&&d==d3;}// Case 3:// point[1] and point[3] are adjacent vertices,// point[2] is diagonally opposite to point[0].if(d2==d4&&2*d2==d3){intd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd==distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2;}// None of the three configurations satisfied square// properties.returnfalse;}publicstaticvoidmain(String[]args){int[][]points={{0,0},{0,1},{1,0},{1,1}};System.out.println(isSquare(points)?"true":"false");}}
Python
# Returns the squared Euclidean distance between two points.# Squared distance is sufficient because we only need comparisons# and it avoids using sqrt().defdistance_sq(a,b,p,q):return(p-a)**2+(q-b)**2# Checks whether the given four points form a valid square.defisSquare(points):# A valid square cannot have overlapping points.foriinrange(4):forjinrange(i+1,4):ifpoints[i][0]==points[j][0]andpoints[i][1]==points[j][1]:returnFalse# Compute distances from the first point to the other three points.d2=distance_sq(points[0][0],points[0][1],points[1][0],points[1][1])d3=distance_sq(points[0][0],points[0][1],points[2][0],points[2][1])d4=distance_sq(points[0][0],points[0][1],points[3][0],points[3][1])# Case 1:# point[1] and point[2] are adjacent vertices,# point[3] is diagonally opposite to point[0].ifd2==d3and2*d2==d4:# Remaining three sides must also be equal.d=distance_sq(points[1][0],points[1][1],points[3][0],points[3][1])return(d==distance_sq(points[2][0],points[2][1],points[3][0],points[3][1])andd==d2)# Case 2:# point[2] and point[3] are adjacent vertices,# point[1] is diagonally opposite to point[0].ifd3==d4and2*d3==d2:d=distance_sq(points[1][0],points[1][1],points[2][0],points[2][1])return(d==distance_sq(points[1][0],points[1][1],points[3][0],points[3][1])andd==d3)# Case 3:# point[1] and point[3] are adjacent vertices,# point[2] is diagonally opposite to point[0].ifd2==d4and2*d2==d3:d=distance_sq(points[1][0],points[1][1],points[2][0],points[2][1])return(d==distance_sq(points[2][0],points[2][1],points[3][0],points[3][1])andd==d2)# None of the three configurations satisfied square properties.returnFalse# Driver Codeif__name__=="__main__":points=[[0,0],[0,1],[1,0],[1,1]]print("true"ifisSquare(points)else"false")
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// Returns the squared Euclidean distance between two// points. Squared distance is sufficient because we// only need comparisons and it avoids using sqrt().staticintDistanceSq(inta,intb,intp,intq){return(p-a)*(p-a)+(q-b)*(q-b);}// Checks whether the given four points form a valid// square.staticboolisSquare(List<List<int>>points){// A valid square cannot have overlapping points.for(inti=0;i<4;i++){for(intj=i+1;j<4;j++){if(points[i][0]==points[j][0]&&points[i][1]==points[j][1]){returnfalse;}}}// Compute distances from the first point to the// other three points.intd2=DistanceSq(points[0][0],points[0][1],points[1][0],points[1][1]);intd3=DistanceSq(points[0][0],points[0][1],points[2][0],points[2][1]);intd4=DistanceSq(points[0][0],points[0][1],points[3][0],points[3][1]);// Case 1:// point[1] and point[2] are adjacent vertices,// point[3] is diagonally opposite to point[0].if(d2==d3&&2*d2==d4){intd=DistanceSq(points[1][0],points[1][1],points[3][0],points[3][1]);returnd==DistanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2;}// Case 2:// point[2] and point[3] are adjacent vertices,// point[1] is diagonally opposite to point[0].if(d3==d4&&2*d3==d2){intd=DistanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd==DistanceSq(points[1][0],points[1][1],points[3][0],points[3][1])&&d==d3;}// Case 3:// point[1] and point[3] are adjacent vertices,// point[2] is diagonally opposite to point[0].if(d2==d4&&2*d2==d3){intd=DistanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd==DistanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d==d2;}// None of the three configurations satisfied square// properties.returnfalse;}staticvoidMain(){List<List<int>>points=newList<List<int>>{newList<int>{0,0},newList<int>{0,1},newList<int>{1,0},newList<int>{1,1}};Console.WriteLine(isSquare(points)?"true":"false");}}
JavaScript
// Returns the squared Euclidean distance between two// points. Squared distance is sufficient because we only// need comparisons and it avoids using Math.sqrt().functiondistanceSq(a,b,p,q){return(p-a)*(p-a)+(q-b)*(q-b);}// Checks whether the given four points form a valid square.functionisSquare(points){// A valid square cannot have overlapping points.for(leti=0;i<4;i++){for(letj=i+1;j<4;j++){if(points[i][0]===points[j][0]&&points[i][1]===points[j][1]){returnfalse;}}}// Compute distances from the first point to the other// three points.letd2=distanceSq(points[0][0],points[0][1],points[1][0],points[1][1]);letd3=distanceSq(points[0][0],points[0][1],points[2][0],points[2][1]);letd4=distanceSq(points[0][0],points[0][1],points[3][0],points[3][1]);// Case 1:// point[1] and point[2] are adjacent vertices,// point[3] is diagonally opposite to point[0].if(d2===d3&&2*d2===d4){letd=distanceSq(points[1][0],points[1][1],points[3][0],points[3][1]);returnd===distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d===d2;}// Case 2:// point[2] and point[3] are adjacent vertices,// point[1] is diagonally opposite to point[0].if(d3===d4&&2*d3===d2){letd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd===distanceSq(points[1][0],points[1][1],points[3][0],points[3][1])&&d===d3;}// Case 3:// point[1] and point[3] are adjacent vertices,// point[2] is diagonally opposite to point[0].if(d2===d4&&2*d2===d3){letd=distanceSq(points[1][0],points[1][1],points[2][0],points[2][1]);returnd===distanceSq(points[2][0],points[2][1],points[3][0],points[3][1])&&d===d2;}// None of the three configurations satisfied square// properties.returnfalse;}// Driver Codeconstpoints=[[0,0],[0,1],[1,0],[1,1]];console.log(isSquare(points)?"true":"false");
Output
true
Time Complexity: O(1), all operations are being carried out in O(1) constant time. Auxiliary Space: O(1), no extra space required