📜  3-D中两个平行平面之间的距离

📅  最后修改于: 2021-04-26 17:52:47             🧑  作者: Mango

给您两个平面P1a1 * x + b1 * y + c1 * z + d1 = 0P2a2 * x + b2 * y + c2 * z + d2 = 0 。任务是编写一个程序来查找这两个平面之间的距离。


例子 :

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 

方法:考虑两个平面由等式给出:

两个平面平行的条件是:

=> a1 / a2 = b1 / b2 = c1 / c2

在任一平面中找到一个点,以使从该点到另一平面的距离成为这两个平面之间的距离。可以使用以下公式计算距离:

Distance = (| a*x1 + b*y1 + c*z1 + d |) / (sqrt( a*a + b*b + c*c))

设平面P1中的一个点为P(x1,y1,z1),
将x = y = 0放在等式a1 * x + b1 * y + c1 * z + d1 = 0中并找到z。
=> z = -d1 / c1
现在我们有了P(0,0,z)= P(x1,y1,z1)的坐标。
点P到平面P2的距离将是:

下面是上述公式的实现:

C++
// C++ program to find the Distance 
// between two parallel Planes in 3 D.
#include  
#include
  
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  
#include
   
// 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


输出:
Perpendicular distance is 0.952579344416