Check if it is possible to draw a straight line with the given direction cosines in Python


Suppose we have three direction cosines l, m and n in 3-D space, we have to check whether it is possible to draw a straight line with these direction cosines or not.

So, if the input is like l = 0.42426 m = 0.56568 n = 0.7071, then the output will be True as this the direction cosines of a vector {3, 4, 5}.

To solve this, we will follow some rule as

  • l = cos(a), where a is angle between the straight line and x-axis
  • m = cos(b), where b is angle between the straight line and y-axis
  • n = cos(c), where c is angle between the straight line and z-axis
  • l^2 + m^2 + n^2 = 1

To solve this, we will follow these steps −

  • angle := l * l + m * m + n * n
  • angle := round of the value of angle up to 8 decimal places
  • if |1 - angle| < 0.0001, then
    • return True
  • return False

Example

Let us see the following implementation to get better understanding −

 Live Demo

def solve(l, m, n) :
   angle = l * l + m * m + n * n
   angle = round(angle, 8)
   if abs(1 - angle) < 0.0001:
      return True
   return False
l = 0.42426
m = 0.56568
n = 0.7071
print (solve(l, m, n))

Input

0.42426, 0.56568, 0.7071

Output

True

Updated on: 18-Jan-2021

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements