Tienes dos planos P1 : a1 * x + b1 * y + c1 * z + d1 = 0 y P2 : a2 * x + b2 * y + c2 * z + d2 = 0 . La tarea es escribir un programa para encontrar la distancia entre estos dos Planos.
Ejemplos:
Input: a1 = 1, b1 = 2, c1 = -1, d1 = 1, a2 = 3, b2 = 6, c2 = -3, d2 = -4 Output: Distance is 0.952579344416 Input: a1 = 1, b1 = 2, c1 = -1, d1 = 1, a2 = 1, b2 = 6, c2 = -3, d2 = -4 Output: Planes are not parallel
Enfoque: Considere dos planos dados por las ecuaciones: –
P1 : a1 * x + b1 * y + c1 * z + d1 = 0, donde a1, b1 y c1, d1 son constantes reales y
P2 : a2 * x + b2 * y + c2 * z + d2 = 0, donde a2 , b2 y c2, d2 son constantes reales.
La condición para que dos planos sean paralelos es:
=> a1 / a2 = b1 / b2 = c1 / c2
Encuentre un punto en cualquier plano tal que la distancia de ese punto al otro plano sea la distancia entre esos dos planos. La distancia se puede calcular utilizando las fórmulas:
Distance = (| a*x1 + b*y1 + c*z1 + d |) / (sqrt( a*a + b*b + c*c))
Sea un punto en el Plano P1 P(x1, y1, z1),
ponga x = y = 0 en la ecuación a1 * x + b1 * y + c1 * z + d1 = 0 y encuentre z.
=> z = -d1 / c1
Ahora tenemos coordenadas de P(0, 0, z) = P(x1, y1, z1).
La distancia del punto P al plano P2 será: –
Distancia = (| a2*x1 + b2*y1 + c2*z1 + d2 |) / (raíz cuadrada( a2*a2 + b2*b2 + c2*c2))
= (| a2*0 + b2*0 + c2*z1 + d2 |) / (raíz cuadrada( a2*a2 + b2*b2 + c2*c2))
= (| c2*z1 + d2 |) / (raíz cuadrada( a2*a2 + b2*b2 + c2*c2))
A continuación se muestra la implementación de las fórmulas anteriores:
C++
// C++ program to find the Distance // between two parallel Planes in 3 D. #include <bits/stdc++.h> #include<math.h> using namespace std; // Function to find distance void distance(float a1, float b1, float c1, float d1, float a2, float b2, float c2, float d2) { float x1, y1, z1, d; if (a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2) { x1 = y1 = 0; z1 = -d1 / c1; d = fabs(( c2 * z1 + d2)) / (sqrt(a2 * a2 + b2 * b2 + c2 * c2)); cout << "Perpendicular distance is " << d << endl; } else cout << "Planes are not parallel"; return; } // Driver Code int main() { float a1 = 1; float b1 = 2; float c1 = -1; float d1 = 1; float a2 = 3; float b2 = 6; float c2 = -3; float d2 = -4; distance(a1, b1, c1, d1, a2, b2, c2, d2); // Fxn cal return 0; } // This code is contributed // by Akanksha Rai(Abby_akku)
C
// C program to find the Distance between // two parallel Planes in 3 D. #include <stdio.h> #include<math.h> // Function to find distance void distance(float a1, float b1, float c1, float d1, float a2, float b2, float c2, float d2) { float x1,y1,z1,d; if (a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2) { x1 = y1 = 0; z1 =-d1 / c1; d = fabs(( c2 * z1 + d2)) / (sqrt(a2 * a2 + b2 * b2 + c2 * c2)); printf("Perpendicular distance is %f\n", d); } else printf("Planes are not parallel"); return; } // Driver Code int main() { float a1 = 1; float b1 = 2; float c1 = -1; float d1 = 1; float a2 = 3; float b2 = 6; float c2 = -3; float d2 = -4; distance(a1, b1, c1, d1, a2, b2, c2, d2); // Fxn cal return 0; } // This code is contributed // by Amber_Saxena.
Java
// Java program to find the Distance // between two parallel Planes in 3 D. import java .io.*; import java.lang.Math; class GFG { // Function to find distance static void distance(float a1, float b1, float c1, float d1, float a2, float b2, float c2, float d2) { float x1,y1,z1,d; if (a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2) { x1 = y1 = 0; z1 =-d1 / c1; d = Math.abs(( c2 * z1 + d2)) / (float)(Math.sqrt(a2 * a2 + b2 * b2 + c2 * c2)); System.out.println("Perpendicular distance is "+ d); } else System.out.println("Planes are not parallel"); } // Driver code public static void main(String[] args) { float a1 = 1; float b1 = 2; float c1 = -1; float d1 = 1; float a2 = 3; float b2 = 6; float c2 = -3; float d2 = -4; distance(a1, b1, c1, d1, a2, b2, c2, d2);// Fxn cal } } // This code is contributed // by Amber_Saxena.
Python
# Python program to find the Distance between # two parallel Planes in 3 D. import math # Function to find distance def distance(a1, b1, c1, d1, a2, b2, c2, d2): if (a1 / a2 == b1 / b2 and b1 / b2 == c1 / c2): x1 = y1 = 0 z1 =-d1 / c1 d = abs(( c2 * z1 + d2)) / (math.sqrt(a2 * a2 + b2 * b2 + c2 * c2)) print("Perpendicular distance is"), d else: print("Planes are not parallel") # Driver Code a1 = 1 b1 = 2 c1 = -1 d1 = 1 a2 = 3 b2 = 6 c2 = -3 d2 = -4 distance(a1, b1, c1, d1, a2, b2, c2, d2) # Fxn cal
C#
// C# program to find the Distance // between two parallel Planes in 3 D. using System; class GFG { // Function to find distance static void distance(float a1, float b1, float c1, float d1, float a2, float b2, float c2, float d2) { float z1, d; if (a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2) { z1 =-d1 / c1; d = Math.Abs((c2 * z1 + d2)) / (float)(Math.Sqrt(a2 * a2 + b2 * b2 + c2 * c2)); Console.Write("Perpendicular distance is " + d); } else Console.Write("Planes are not parallel"); } // Driver code public static void Main() { float a1 = 1; float b1 = 2; float c1 = -1; float d1 = 1; float a2 = 3; float b2 = 6; float c2 = -3; float d2 = -4; distance(a1, b1, c1, d1, a2, b2, c2, d2);// Fxn cal } } // This code is contributed // by ChitraNayal
PHP
<?php // PHP program to find the Distance // between two parallel Planes in 3 D // Function to find distance function distance($a1, $b1, $c1, $d1, $a2, $b2, $c2, $d2) { if ($a1 / $a2 == $b1 / $b2 && $b1 / $b2 == $c1 / $c2) { $x1 = $y1 = 0; $z1 =- $d1 / $c1; $d = abs(($c2 * $z1 + $d2)) / (sqrt($a2 * $a2 + $b2 * $b2 + $c2 * $c2)); echo "Perpendicular distance is ", $d; } else echo "Planes are not parallel"; } // Driver Code $a1 = 1; $b1 = 2; $c1 = -1; $d1 = 1; $a2 = 3; $b2 = 6; $c2 = -3; $d2 = -4; distance($a1, $b1, $c1, $d1, $a2, $b2, $c2, $d2); // This code is contributed // by Amber_Saxena. ?>
Javascript
<script> // Javascript program to find the Distance // between two parallel Planes in 3 D. // Function to find distance function distance(a1, b1, c1, d1, a2, b2, c2, d2) { let x1,y1,z1,d; if (a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2) { x1 = y1 = 0; z1 =-d1 / c1; d = Math.abs(( c2 * z1 + d2)) / (Math.sqrt(a2 * a2 + b2 * b2 + c2 * c2)); document.write("Perpendicular distance is "+ d); } else document.write("Planes are not parallel"); } // Driver Code let a1 = 1; let b1 = 2; let c1 = -1; let d1 = 1; let a2 = 3; let b2 = 6; let c2 = -3; let d2 = -4; distance(a1, b1, c1, d1, a2, b2, c2, d2);// Fxn cal </script>
Perpendicular distance is 0.952579
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por anamika9988 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA