Dados tres cosenos directores l , m y n de un plano tridimensional, la tarea es verificar si es posible dibujar una línea recta con ellos o no. Escriba Sí si es posible, de lo contrario, escriba No.
Ejemplos:
Entrada: l = 0,258, m = 0,80, n = 0,23
Salida: No
Entrada: l = 0,70710678, m = 0,5, n = 0,5
Salida: Sí
Enfoque: si una línea recta forma un ángulo a con eje X positivo , un ángulo b con un eje Y positivo y un ángulo c con un eje Z positivo , entonces sus cosenos directores son cos(a) , cos(b) y cos(c) .
Para una línea recta, cos 2 (a) + cos 2 (b) + cos 2 (c) = 1 .
A continuación se muestra la implementación del enfoque anterior:
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; }
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
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
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.
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.
Javascript
<script> // Javascript implementation of the approach // Function that returns true // if a straight line is possible 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; } // Driver code let l = 0.70710678, m = 0.5, n = 0.5; if (isPossible(l, m, n)) document.write("Yes"); else document.write("No"); // This code is contributed by rag2127 </script>
Producción:
Yes