Check if given four points form a square
PROBLEM :
Given coordinates of four points in a plane, find if the four points form a square or not.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains 4 space separated integer points a , b , c , d
Output:
For each test case print 1 if the four points form a square else print 0.
Remember to output the answer of each test case in a new line.
Constraints:
1<=T<=100
1<=a,b,c,d<=100
Example:
Input:
2
20 20 20 10 10 20 10 10
10 10 10 10 20 10 20 30
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<utility>
int distance(int ,int ,int , int ) ;
int main()
{
int t ;
pair<int,int> p1 ;
pair<int,int> p2 ;
pair<int,int> p3 ;
pair<int,int> p4 ;
int d1,d2,d3,d4,d5 ;
bool flag ;
cin>>t ;
while(t--)
{
flag=false ;
cin>>p1.first ;
cin>>p1.second ;
cin>>p2.first ;
cin>>p2.second ;
cin>>p3.first ;
cin>>p3.second ;
cin>>p4.first ;
cin>>p4.second ;
d1=distance(p1.first,p1.second,p2.first,p2.second) ;
d2=distance(p1.first,p1.second,p3.first,p3.second) ;
d3=distance(p1.first,p1.second,p4.first,p4.second) ;
if(d1==d2&&2*d1==d3)
{
d4=distance(p2.first,p2.second,p4.first,p4.second) ;
d5=distance(p3.first,p3.second,p4.first,p4.second) ;
if(d4==d1&&d5==d1)
flag=true ;
}
if(d1==d3&&2*d1==d2)
{
d4=distance(p2.first,p2.second,p3.first,p3.second) ;
d5=distance(p3.first,p3.second,p4.first,p4.second) ;
if(d4==d1&&d5==d1)
flag=true ;
}
if(d3==d2&&2*d2==d1)
{
d4=distance(p2.first,p2.second,p3.first,p3.second) ;
d5=distance(p2.first,p2.second,p4.first,p4.second) ;
if(d4==d2&&d5==d2)
flag=true ;
}
cout<<flag<<endl ;
}
return 0;
}
int distance(int x1,int y1,int x2, int y2)
{
return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) ;
}
---------------------------------------------------------------------------------
Given coordinates of four points in a plane, find if the four points form a square or not.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains 4 space separated integer points a , b , c , d
Output:
For each test case print 1 if the four points form a square else print 0.
Remember to output the answer of each test case in a new line.
Constraints:
1<=T<=100
1<=a,b,c,d<=100
Example:
Input:
2
20 20 20 10 10 20 10 10
10 10 10 10 20 10 20 30
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<utility>
int distance(int ,int ,int , int ) ;
int main()
{
int t ;
pair<int,int> p1 ;
pair<int,int> p2 ;
pair<int,int> p3 ;
pair<int,int> p4 ;
int d1,d2,d3,d4,d5 ;
bool flag ;
cin>>t ;
while(t--)
{
flag=false ;
cin>>p1.first ;
cin>>p1.second ;
cin>>p2.first ;
cin>>p2.second ;
cin>>p3.first ;
cin>>p3.second ;
cin>>p4.first ;
cin>>p4.second ;
d1=distance(p1.first,p1.second,p2.first,p2.second) ;
d2=distance(p1.first,p1.second,p3.first,p3.second) ;
d3=distance(p1.first,p1.second,p4.first,p4.second) ;
if(d1==d2&&2*d1==d3)
{
d4=distance(p2.first,p2.second,p4.first,p4.second) ;
d5=distance(p3.first,p3.second,p4.first,p4.second) ;
if(d4==d1&&d5==d1)
flag=true ;
}
if(d1==d3&&2*d1==d2)
{
d4=distance(p2.first,p2.second,p3.first,p3.second) ;
d5=distance(p3.first,p3.second,p4.first,p4.second) ;
if(d4==d1&&d5==d1)
flag=true ;
}
if(d3==d2&&2*d2==d1)
{
d4=distance(p2.first,p2.second,p3.first,p3.second) ;
d5=distance(p2.first,p2.second,p4.first,p4.second) ;
if(d4==d2&&d5==d2)
flag=true ;
}
cout<<flag<<endl ;
}
return 0;
}
int distance(int x1,int y1,int x2, int y2)
{
return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment