CONCEPTS USED:
Basic Mathematics, Co-ordinate Geometry
DIFFICULTY LEVEL:
Easy
PROBLEM STATEMENT(SIMPLIFIED):
Given 4 coordinate points, check if the given quadrilateral formed from given coordinates forms a Square or not, print
YesorNoaccordingly.
EXAMPLE:
Input: A(20, 10), B(10, 20), C(20, 20), D(10, 10)
Output: Yes
Explanation: Diagonals are of same length and bisect at same coordinates.
d1 = (20 - 10)*(20 - 10) + (10 - 20)*(10 - 20) = 200
d1 = (20 - 10)*(20 - 10) + (20 - 10)*(20 - 10) = 200
mid1 = ( (20 + 10)/2 , (10 + 20)/2 ) = (15, 15)
mid2 = ( (20 + 10)/2 , (20 + 10)/2 ) = (15, 15)
OBSERVATION:
Proving that the given Quadrilateral is a
Squaresolves our problem as it has all its sides equal.Properties of
Square:
Both diagonals are of the same length.
Both diagonals bisect at the same coordinates.
SOLVING APPROACH:
If the length of both diagonals are equal and they bisect each other at the same coordinates, then return
YeselseNo.Find the length of both the diagonals using the given formula :
√(X{2} − X{1})2 + `(Y{2} − Y{1})2
Find the mid point of both the diagonals using the following mid-point theorem :
X=(X_{1} + X_{2})/2
Y=(Y_{1} + Y_{2})/2
SOLUTIONS:
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int arr_x[4];
int arr_y[4];
for(int i=0;i<4;i++)
{
scanf("%d",&arr_x[i]);
scanf("%d",&arr_y[i]);
}
int dist_12 = (arr_x[0]-arr_x[1])*(arr_x[0]-arr_x[1]) + (arr_y[0]-arr_y[1])*(arr_y[0]-arr_y[1]);
int dist_34 = (arr_x[2]-arr_x[3])*(arr_x[2]-arr_x[3]) + (arr_y[2]-arr_y[3])*(arr_y[2]-arr_y[3]);
int midx_12 = (arr_x[0]+arr_x[1])/2;
int midy_12 = (arr_y[0]+arr_y[1])/2;
int midx_34 = (arr_x[2]+arr_x[3])/2;
int midy_34 = (arr_y[2]+arr_y[3])/2;
if(dist_12 == dist_34 && midx_12==midx_34 && midy_12==midy_34)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t--)
{
int arr_x[4];
int arr_y[4];
for(int i=0;i<4;i++)
{
cin>>arr_x[i];
cin>>arr_y[i];
}
int dist_12 = (arr_x[0]-arr_x[1])*(arr_x[0]-arr_x[1]) + (arr_y[0]-arr_y[1])*(arr_y[0]-arr_y[1]);
int dist_34 = (arr_x[2]-arr_x[3])*(arr_x[2]-arr_x[3]) + (arr_y[2]-arr_y[3])*(arr_y[2]-arr_y[3]);
int midx_12 = (arr_x[0]+arr_x[1])/2;
int midy_12 = (arr_y[0]+arr_y[1])/2;
int midx_34 = (arr_x[2]+arr_x[3])/2;
int midy_34 = (arr_y[2]+arr_y[3])/2;
if(dist_12 == dist_34 && midx_12==midx_34 && midy_12==midy_34)
{
cout<<"Yes"<<"\n";
}
else
{
cout<<"No"<<"\n";
}
}
return 0;
}
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t!=0)
{
int arr_x[] = new int[4];
int arr_y[] = new int[4];
for(int i=0;i<4;i++)
{
arr_x[i] = sc.nextInt();
arr_y[i] = sc.nextInt();;
}
int dist_12 = (arr_x[0]-arr_x[1])*(arr_x[0]-arr_x[1]) + (arr_y[0]-arr_y[1])*(arr_y[0]-arr_y[1]);
int dist_34 = (arr_x[2]-arr_x[3])*(arr_x[2]-arr_x[3]) + (arr_y[2]-arr_y[3])*(arr_y[2]-arr_y[3]);
int midx_12 = (arr_x[0]+arr_x[1])/2;
int midy_12 = (arr_y[0]+arr_y[1])/2;
int midx_34 = (arr_x[2]+arr_x[3])/2;
int midy_34 = (arr_y[2]+arr_y[3])/2;
if(dist_12 == dist_34 && midx_12==midx_34 && midy_12==midy_34)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
t--;
}
}
}
Space Complexity:
O(1)
[forminator_quiz id="442"]
This article tried to discuss the concept of Mathematics. Hope this blog helps you understand and solve the problem. To practice more problems on Mathematics you can check out .
