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

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, **cos ^{2}(a) + cos^{2}(b) + cos^{2}(c) = 1**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true ` `// if a straight line is possible ` `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` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `l = 0.70710678, m = 0.5, n = 0.5; ` ` ` ` ` `if` `(isPossible(l, m, n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true ` `// if a straight line is possible ` `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` `; ` `} ` ` ` `// Driver code ` `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"` `); ` `} ` `} ` ` ` `// This code is contributed by ` `// Shashank_Sharma ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `ceil, floor ` ` ` `# Function that returns true ` `# if a straight line is possible ` `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` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l ` `=` `0.70710678` ` ` `m ` `=` `0.5` ` ` `n ` `=` `0.5` ` ` ` ` `if` `(isPossible(l, m, n)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true ` `// if a straight line is possible ` `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` `; ` `} ` ` ` `// Driver code ` `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"` `); ` `} ` `} ` ` ` `// This code is contributed by Ita_c. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function that returns true ` `// if a straight line is possible ` `function` `isPossible(` `$x` `, ` `$y` `, ` `$z` `) ` `{ ` ` ` `$a` `= ` `round` `(` `$x` `* ` `$x` `+ ` `$y` `* ` `$y` `+ ` `$z` `* ` `$z` `); ` ` ` `if` `(` `ceil` `(` `$a` `) == 1 && ` `floor` `(` `$a` `) == 1) ` ` ` `return` `true; ` ` ` `return` `false; ` `} ` ` ` `// Driver code ` `$l` `= 0.70710678; ` `$m` `= 0.5; ` `$n` `= 0.5; ` ` ` `if` `(isPossible(` `$l` `, ` `$m` `, ` `$n` `)) ` ` ` `echo` `(` `"Yes"` `); ` `else` ` ` `echo` `(` `"No"` `); ` `// This code is contributed by mukul singh. ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Represent a given set of points by the best possible straight line
- Length of the normal from origin on a straight line whose intercepts are given
- Direction of a Point from a Line Segment
- Check whether two straight lines are orthogonal or not
- Check if three straight lines are concurrent or not
- Check whether the point (x, y) lies on a given line
- How to check if two given line segments intersect?
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check if a line passes through the origin
- Program to find third side of triangle using law of cosines
- Check if a line touches or intersects a circle
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Count of different straight lines with total n points with m collinear
- Time until distance gets equal to X between two objects moving in opposite direction
- Draw geometric shapes on images using OpenCV

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.