Dados cuatro enteros a , b , c y d , que representa el coeficiente de la ecuación del plano ax + by + cz + d = 0 y dos coordenadas enteras (x1, y1, z1) y (x2, y2, z2) , la tarea es encontrar si ambos puntos se encuentran en el mismo lado, en lados diferentes o en la superficie del plano.
Ejemplos:
Entrada: a = 1, b = 2, c = 3, d = 4, x1 = -2, y1 = -2, z1 = 1, x2 = -4, y2 = 11, z2 = -1
Salida: Del mismo lado
Explicación: Al aplicar (x1, y1, z1) y (x2, y2, z2) en ax+by+cz+d=0 da 1 y 19 respectivamente, los cuales tienen el mismo signo, por lo que ambos puntos se encuentran en el mismo lado del avión.Entrada: a = 4, b = 2, c = 1, d = 3, x1 = -2, y1 = -2, z1 = 1, x2 = -4, y2 = 11, z2 = -1
Salida: En diferentes lados
Planteamiento: La idea se basa en que si los dos puntos aplicados a la ecuación tienen la misma paridad (signo), entonces estarán en el mismo lado del plano, y si tienen diferente paridad entonces estarán en el mismo lado. diferentes lados del plano. Siga los pasos a continuación para resolver el problema:
- Coloca las coordenadas de los puntos dados en la ecuación del plano y almacena los valores en las variables P1 y P2 .
- Comprobar el signo de los valores obtenidos:
- Si P1 y P2 tienen la misma paridad, entonces están en el mismo lado del plano.
- Si P1 y P2 tienen diferente paridad, entonces se encuentran en lados opuestos del plano.
- Si P1 y P2 son cero, entonces se encuentran en el plano.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to check position of two // points with respect to a plane in 3D void check_position(int a, int b, int c, int d, int x1, int y1, int z1, int x2, int y2, int z2) { // Put coordinates in plane equation int value_1 = a * x1 + b * y1 + c * z1 + d; int value_2 = a * x2 + b * y2 + c * z2 + d; // If both values have same sign if ((value_1 > 0 && value_2 > 0) || (value_1 < 0 && value_2 < 0)) cout << "On same side"; // If both values have different sign if ((value_1 > 0 && value_2 < 0) || (value_1 < 0 && value_2 > 0)) cout << "On different sides"; // If both values are zero if (value_1 == 0 && value_2 == 0) cout << "Both on the plane"; // If either of the two values is zero if (value_1 == 0 && value_2 != 0) cout << "Point 1 on the plane"; if (value_1 != 0 && value_2 == 0) cout << "Point 2 on the plane"; } // Driver Code int main() { // Given Input int a = 1, b = 2, c = 3, d = 4; // Coordinates of points int x1 = -2, y1 = -2, z1 = 1; int x2 = -4, y2 = 11, z2 = -1; // Function Call check_position(a, b, c, d, x1, y1, z1, x2, y2, z2); return 0; }
Java
// Java program for the above approach public class GFG { // Function to check position of two // points with respect to a plane in 3D static void check_position(int a, int b, int c, int d, int x1, int y1, int z1, int x2, int y2, int z2) { // Put coordinates in plane equation int value_1 = a * x1 + b * y1 + c * z1 + d; int value_2 = a * x2 + b * y2 + c * z2 + d; // If both values have same sign if ((value_1 > 0 && value_2 > 0) || (value_1 < 0 && value_2 < 0)) System.out.print("On same side"); // If both values have different sign if ((value_1 > 0 && value_2 < 0) || (value_1 < 0 && value_2 > 0)) System.out.print("On different sides"); // If both values are zero if (value_1 == 0 && value_2 == 0) System.out.print("Both on the plane"); // If either of the two values is zero if (value_1 == 0 && value_2 != 0) System.out.print("Point 1 on the plane"); if (value_1 != 0 && value_2 == 0) System.out.print("Point 2 on the plane"); } // Driver code public static void main(String[] args) { // Given Input int a = 1, b = 2, c = 3, d = 4; // Coordinates of points int x1 = -2, y1 = -2, z1 = 1; int x2 = -4, y2 = 11, z2 = -1; // Function Call check_position(a, b, c, d, x1, y1, z1, x2, y2, z2); } } // This code is contributed by sk944795.
Python3
# Python3 program for the above approach # Function to check position of two # points with respect to a plane in 3D def check_position(a, b, c, d, x1, y1, z1, x2, y2, z2): # Put coordinates in plane equation value_1 = a * x1 + b * y1 + c * z1 + d value_2 = a * x2 + b * y2 + c * z2 + d # If both values have same sign if ((value_1 > 0 and value_2 > 0) or (value_1 < 0 and value_2 < 0)): print("On same side") # If both values have different sign if ((value_1 > 0 and value_2 < 0) or (value_1 < 0 and value_2 > 0)): print("On different sides") # If both values are zero if (value_1 == 0 and value_2 == 0): print("Both on the plane") # If either of the two values is zero if (value_1 == 0 and value_2 != 0): print("Point 1 on the plane") if (value_1 != 0 and value_2 == 0): print("Point 2 on the plane") # Driver Code if __name__ == '__main__': # Given Input a, b, c, d = 1, 2, 3, 4 # Coordinates of points x1, y1, z1 = -2, -2, 1 x2, y2, z2 = -4, 11, -1 # Function Call check_position(a, b, c, d, x1, y1, z1, x2, y2, z2) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG{ // Function to check position of two // points with respect to a plane in 3D static void check_position(int a, int b, int c, int d, int x1, int y1, int z1, int x2, int y2, int z2) { // Put coordinates in plane equation int value_1 = a * x1 + b * y1 + c * z1 + d; int value_2 = a * x2 + b * y2 + c * z2 + d; // If both values have same sign if ((value_1 > 0 && value_2 > 0) || (value_1 < 0 && value_2 < 0)) Console.Write("On same side"); // If both values have different sign if ((value_1 > 0 && value_2 < 0) || (value_1 < 0 && value_2 > 0)) Console.Write("On different sides"); // If both values are zero if (value_1 == 0 && value_2 == 0) Console.Write("Both on the plane"); // If either of the two values is zero if (value_1 == 0 && value_2 != 0) Console.Write("Point 1 on the plane"); if (value_1 != 0 && value_2 == 0) Console.Write("Point 2 on the plane"); } // Driver code static void Main() { // Given Input int a = 1, b = 2, c = 3, d = 4; // Coordinates of points int x1 = -2, y1 = -2, z1 = 1; int x2 = -4, y2 = 11, z2 = -1; // Function Call check_position(a, b, c, d, x1, y1, z1, x2, y2, z2); } } // This code is contributed by sanjoy_62.
Javascript
<script> // JavaScript program for the above approach // Function to check position of two // points with respect to a plane in 3D function check_position(a , b , c , d, x1 , y1 , z1, x2 , y2 , z2) { // Put coordinates in plane equation var value_1 = a * x1 + b * y1 + c * z1 + d; var value_2 = a * x2 + b * y2 + c * z2 + d; // If both values have same sign if ((value_1 > 0 && value_2 > 0) || (value_1 < 0 && value_2 < 0)) document.write("On same side"); // If both values have different sign if ((value_1 > 0 && value_2 < 0) || (value_1 < 0 && value_2 > 0)) document.write("On different sides"); // If both values are zero if (value_1 == 0 && value_2 == 0) document.write("Both on the plane"); // If either of the two values is zero if (value_1 == 0 && value_2 != 0) document.write("Point 1 on the plane"); if (value_1 != 0 && value_2 == 0) document.write("Point 2 on the plane"); } // Driver code // Given Input var a = 1, b = 2, c = 3, d = 4; // Coordinates of points var x1 = -2, y1 = -2, z1 = 1; var x2 = -4, y2 = 11, z2 = -1; // Function Call check_position(a, b, c, d, x1, y1, z1, x2, y2, z2); // This code is contributed by 29AjayKumar </script>
On same side
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por arjundevmishra6 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA