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++
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
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 ;
}
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);
return 0;
}
|
C
#include <stdio.h>
#include<math.h>
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 ;
}
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);
return 0;
}
|
Java
import java .io.*;
import java.lang.Math;
class GFG
{
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" );
}
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);
}
}
|
Python
import math
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" )
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)
|
C#
using System;
class GFG
{
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" );
}
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);
}
}
|
PHP
<?php
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" ;
}
$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 );
?>
|
Javascript
<script>
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" );
}
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);
</script>
|
Output
Perpendicular distance is 0.952579
Time Complexity: O(logn) as it is using inbuilt sqrt function
Auxiliary Space: O(1)
Last Updated :
13 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...