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 Yes or No accordingly.

See original problem statement here

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 Square solves our problem as it has all its sides equal.

Properties of Square:

  1. Both diagonals are of the same length.

  2. Both diagonals bisect at the same coordinates.

SOLVING APPROACH:

  1. If the length of both diagonals are equal and they bisect each other at the same coordinates, then return Yes else No.

  2. Find the length of both the diagonals using the given formula by referring c programming language online course.

    \sqrt{}(X_{2} − X_{1})^2 + (Y_{2} − Y_{1})^2

  3. 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)
Previous post Saitama’s Punch
Next post N Digit Sum Even Odd

Leave a Reply

Your email address will not be published. Required fields are marked *