Find if it's possible to rotate the page by an angle or not in C++

C++Server Side ProgrammingProgramming

In this problem, we are given coordinates of three points that lie on a page. Our task is to find if it’s possible to rotate the page by an angle or not. 

 

The rotation of the page is made in such a way that the new position of ‘x’ is the old position of ‘y’, the new position of ‘y’ is the old position of ‘z’. And print “Yes” or “No” based on the rotation.

Let’s take an example to understand the problem,

Input: x = (0, 1), y = (1, 0), z = (0, -1)

Output: Yes

Explanation:


We can rotate the page by 90o.

Solution Approach: 

We can rotate the page by some angle if some conditions are possible.

This can be done if the distance between x and y is the same as the distance between y and z. Also, if all the points lie on the same line rotation cannot be possible.

Program to illustrate the working of our solution,

Example

Live Demo

#include<bits/stdc++.h>
using namespace std;

int possibleOrNot(int coordinates[3][2]){
   
   long long dis1 = pow(coordinates[1][0] - coordinates[0][0], 2) + pow(coordinates[1][1] - coordinates[0][1], 2);
   long long dis2 = pow(coordinates[2][0] - coordinates[1][0], 2) + pow(coordinates[2][1] - coordinates[1][1], 2);

   if(dis1 != dis2)
      return 0;
   else if (coordinates[1][0] == ((coordinates[0][0] + coordinates[2][0]) / 2.0) &amp;&amp; coordinates[1][1] == ((coordinates[0][1] + coordinates[2][1]) / 2.0))
      return 0;
   else
      return 1;
}

int main() {
   
   int coordinates[3][2] = {{0 , 1}, {1 , 0}, {0, -1} } ;
   if ( possibleOrNot(coordinates))
      cout<<"The rotation of page is possible";
   else
      cout<<"The rotation of page is not possible";
   
   return 0;
}

Output

The rotation of page is possible
raja
Published on 22-Jan-2021 13:57:06
Advertisements