Given three direction cosines l, m and n of a 3-D plane, the task is to check if it is possible to draw a straight line with them or not. Print Yes if possible else print No.
Examples:
Input: l = 0.258, m = 0.80, n = 0.23
Output: No
Input: l = 0.70710678, m = 0.5, n = 0.5
Output: Yes
Approach: If a straight line forms angle a with positive X-axis, angle b with positive Y-axis and angle c with positive Z-axis then its direction cosines are cos(a), cos(b) and cos(c).
For a straight line, cos2(a) + cos2(b) + cos2(c) = 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( float x, float y, float z)
{
float a = x * x + y * y + z * z;
if ( ceil (a) == 1 && floor (a) == 1)
return true ;
return false ;
}
int main()
{
float l = 0.70710678, m = 0.5, n = 0.5;
if (isPossible(l, m, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPossible( float x, float y, float z)
{
float a = x * x + y * y + z * z;
if (Math.ceil(a) == 1 && Math.floor(a) == 1 )
return true ;
return false ;
}
public static void main(String args[])
{
float l = 0 .70710678f, m = 0 .5f, n = 0 .5f;
if (isPossible(l, m, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import ceil, floor
def isPossible(x, y, z) :
a = x * x + y * y + z * z
a = round (a, 8 )
if (ceil(a) = = 1 & floor(a) = = 1 ) :
return True
return False
if __name__ = = "__main__" :
l = 0.70710678
m = 0.5
n = 0.5
if (isPossible(l, m, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPossible( float x, float y, float z)
{
float a = x * x + y * y + z * z;
if (Math.Ceiling(a) == 1 && Math.Floor(a) == 1)
return true ;
return false ;
}
public static void Main()
{
float l = 0.70710678f, m = 0.5f, n = 0.5f;
if (isPossible(l, m, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isPossible( $x , $y , $z )
{
$a = round ( $x * $x + $y * $y + $z * $z );
if ( ceil ( $a ) == 1 && floor ( $a ) == 1)
return true;
return false;
}
$l = 0.70710678; $m = 0.5; $n = 0.5;
if (isPossible( $l , $m , $n ))
echo ( "Yes" );
else
echo ( "No" );
|
Javascript
<script>
function isPossible(x,y,z)
{
let a = Math.round(x * x + y * y + z * z);
if (Math.ceil(a) == 1 && Math.floor(a) == 1)
return true ;
return false ;
}
let l = 0.70710678, m = 0.5, n = 0.5;
if (isPossible(l, m, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)