Distance between two parallel Planes in 3-D

You are given two planes P1: a1 * x + b1 * y + c1 * z + d1 = 0 and P2: a2 * x + b2 * y + c2 * z + d2 = 0. The task is to write a program to find distance between these two Planes.


Examples :

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 

Approach :Consider two planes are given by the equations:-



P1 : a1 * x + b1 * y + c1 * z + d1 = 0, where a1, b1 and c1, d1 are real constants and
P2 : a2 * x + b2 * y + c2 * z + d2 = 0, where a2, b2 and c2, d2 are real constants.

The condition for two planes to be parallel is:-

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

Find a point in any one plane such that the distance from that point to the other plane that will be the distance between those two planes. The distance can be calculated by using the formulae:

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

Let a point in Plane P1 be P(x1, y1, z1),
put x = y = 0 in equation a1 * x + b1 * y + c1 * z + d1 = 0 and find z.
=> z = -d1 / c1
Now we have coordinates of P(0, 0, z) = P(x1, y1, z1).
Distance of point P to Plane P2 will be:-

Distance = (| a2*x1 + b2*y1 + c2*z1 + d2 |) / (sqrt( a2*a2 + b2*b2 + c2*c2))
= (| a2*0 + b2*0 + c2*z1 + d2 |) / (sqrt( a2*a2 + b2*b2 + c2*c2))
= (| c2*z1 + d2 |) / (sqrt( a2*a2 + b2*b2 + c2*c2))

Below is the implementation of the above formulae:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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. 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?> 

chevron_right


Output:

Perpendicular distance is 0.952579344416


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.