Find if it’s possible to rotate the page by an angle or not.
Last Updated :
22 Jun, 2022
You are given three points a, b, c on a page. Find if it’s possible to rotate the page around the point by an angle, such that the new position of ‘a’ is same as the old position of ‘b’, and the new position of ‘b’ is same as the old position of ‘c’. If such angle exists print “Yes”, else “No”.
Examples:
Input : a1 = 0, a2 = 1, b1 = 1, b2 = 1,
c1 = 1, c2 = 0
Output : Yes
Explanation : Rotate the page by 90 degree.
Input : a1 = 1, a2 = 1, b1 = 0, b2 = 0,
c1 = 1000, c2 = 1000
Output : No
Rotation of page by some angle is only possible if the distance between points ‘a’ and ‘b’ is equal to distance between points ‘b’ and ‘c’. But if the points are on same line, there is no rotation at point ‘b’. The problem has no solution when ‘a’, ‘b’, ‘c’ are in the same line or dis(a, b) != dis(b, c)
C++
#include<bits/stdc++.h>
using namespace std;
void possibleOrNot( long long a1, long long a2,
long long b1, long long b2,
long long c1, long long c2){
long long dis1 = pow (b1 - a1, 2) +
pow (b2 - a2, 2);
long long dis2 = pow (c1 - b1, 2) +
pow (c2 - b2, 2);
if (dis1 != dis2)
cout << "No" ;
else if (b1 == ((a1 + c1) / 2.0) && b2 ==
((a2 + c2) / 2.0))
cout << "No" ;
else
cout << "Yes" ;
}
int main()
{
ios_base::sync_with_stdio( false );
cin.tie(NULL);
long long a1 = 1, a2 = 0, b1 = 2,
b2 = 0, c1 = 3, c2 = 0;
possibleOrNot(a1, a2, b1, b2, c1, c2);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void possibleOrNot( long a1, long a2,
long b1, long b2,
long c1, long c2)
{
long dis1 = ( long )Math.pow(b1 - a1, 2 ) +
( long ) Math.pow(b2 - a2, 2 );
long dis2 = ( long )Math.pow(c1 - b1, 2 ) +
( long )Math.pow(c2 - b2, 2 );
if (dis1 != dis2)
System.out.print( "No" );
else if (b1 == ((a1 + c1) / 2.0 ) &&
b2 == ((a2 + c2) / 2.0 ))
System.out.print( "No" );
else
System.out.print( "Yes" );
}
public static void main(String[] args)
{
long a1 = 1 , a2 = 0 , b1 = 2 ,
b2 = 0 , c1 = 3 , c2 = 0 ;
possibleOrNot(a1, a2, b1, b2, c1, c2);
}
}
|
Python3
def possibleOrNot(a1, a2, b1, b2, c1, c2):
dis1 = ( pow (b1 - a1, 2 ) +
pow (b2 - a2, 2 ))
dis2 = ( pow (c1 - b1, 2 ) +
pow (c2 - b2, 2 ))
if (dis1 ! = dis2):
print ( "No" )
else if (b1 = = ((a1 + c1) / / 2.0 ) and
b2 = = ((a2 + c2) / / 2.0 )):
print ( "No" )
else :
print ( "Yes" )
a1, b1, c1 = 1 , 2 , 3
a2 = b2 = c2 = 0
possibleOrNot(a1, a2, b1, b2, c1, c2)
|
C#
using System;
class GFG
{
static void possibleOrNot( long a1, long a2,
long b1, long b2,
long c1, long c2)
{
long dis1 = ( long )Math.Pow(b1 - a1, 2) +
( long ) Math.Pow(b2 - a2, 2);
long dis2 = ( long )Math.Pow(c1 - b1, 2) +
( long )Math.Pow(c2 - b2, 2);
if (dis1 != dis2)
Console.Write( "No" );
else if (b1 == ((a1 + c1) / 2.0) && b2 ==
((a2 + c2) / 2.0))
Console.Write( "No" );
else
Console.Write( "Yes" );
}
public static void Main()
{
long a1 = 1, a2 = 0, b1 = 2,
b2 = 0, c1 = 3, c2 = 0;
possibleOrNot(a1, a2, b1, b2, c1, c2);
}
}
|
PHP
<?php
function possibleOrNot( $a1 , $a2 , $b1 ,
$b2 , $c1 , $c2 )
{
$dis1 = pow( $b1 - $a1 , 2) +
pow( $b2 - $a2 , 2);
$dis2 = pow( $c1 - $b1 , 2) +
pow( $c2 - $b2 , 2);
if ( $dis1 != $dis2 )
echo "No" ;
else if ( $b1 == (( $a1 + $c1 ) / 2.0) &&
$b2 == (( $a2 + $c2 ) / 2.0))
echo "No" ;
else
echo "Yes" ;
}
$a1 = 1;
$a2 = 0;
$b1 = 2;
$b2 = 0;
$c1 = 3;
$c2 = 0;
possibleOrNot( $a1 , $a2 , $b1 ,
$b2 , $c1 , $c2 );
?>
|
Javascript
<script>
function possibleOrNot( a1, a2, b1, b2, c1, c2)
{
var dis1 = Math.pow(b1 - a1, 2) + Math.pow(b2 - a2, 2);
var dis2 = Math.pow(c1 - b1, 2) + Math.pow(c2 - b2, 2);
if (dis1 != dis2)
document.write( "No" );
else if (b1 == ((a1 + c1) / 2.0) && b2 == ((a2 + c2) / 2.0))
document.write( "No" );
else
document.write( "Yes" );
}
var a1 = 1, a2 = 0, b1 = 2, b2 = 0, c1 = 3, c2 = 0;
possibleOrNot(a1, a2, b1, b2, c1, c2);
</script>
|
Output:
No
Time Complexity: O(logn)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...