Program to find X, Y and Z intercepts of a plane
Last Updated :
28 Jul, 2022
In this article, it is explained how to find the X, Y, and Z intercepts of a plane. There are two ways to find the intercepts:
Case 1: When the general equation of the plane is given.
Examples:
Input: A = -6, B = 5, C = -3, D = 9
Output:
1.5
-1.8
3.0
Input: A = 7, B = 4, C = 5, D = -7
Output:
1.0
1.75
1.4
Approach:
The result can be computed by following the below steps:
- Convert general equation of a plane Ax + By + Cz + D = 0 to Ax + By + Cz = – D
- Divide by -D into both sides of the equation
- So, the equation becomes x/(-D/A) + y/(-D/B) + z(-D/C) = 1
- The above equation is in the intercept form of a plane. Therefore,
X intercepts will be = -D/A
Y intercepts will be = -D/B
Z intercepts will be = -D/C
Below is the implementation of the above approach:
C++
#include<iostream>
using namespace std;
float * XandYandZintercept( float A, float B,
float C, float D)
{
static float rslt[3];
float x = -D / A ;
float y = -D / B ;
float z = -D / C ;
rslt[0] = x;
rslt[1] = y;
rslt[2] = z;
return rslt;
}
int main ()
{
int A = 2 ;
int B = 5 ;
int C = 7;
int D = 8 ;
float *rslt = XandYandZintercept(A, B, C, D);
for ( int i = 0; i < 3 ; i++)
{
cout << rslt[i] << " " ;
}
}
|
Java
class GFG
{
static float [] XandYandZintercept( float A,
float B, float C, float D)
{
float rslt[] = new float [ 3 ];
float x = -D / A ;
float y = -D / B ;
float z = -D / C ;
rslt[ 0 ] = x;
rslt[ 1 ] = y;
rslt[ 2 ] = z;
return rslt;
}
public static void main (String[] args)
{
int A = 2 ;
int B = 5 ;
int C = 7 ;
int D = 8 ;
float rslt[] = XandYandZintercept(A, B, C, D);
for ( int i = 0 ; i < 3 ; i++)
{
System.out.print(rslt[i] + " " );
}
}
}
|
Python3
def XandYandZintercept(A, B, C, D):
x = - D / A
y = - D / B
z = - D / C
return [x, y, z]
A = 2
B = 5
C = 7
D = 8
print (XandYandZintercept(A, B, C, D))
|
C#
using System;
class GFG
{
static float [] XandYandZintercept( float A,
float B, float C, float D)
{
float []rslt = new float [3];
float x = -D / A ;
float y = -D / B ;
float z = -D / C ;
rslt[0] = x;
rslt[1] = y;
rslt[2] = z;
return rslt;
}
public static void Main()
{
int A = 2 ;
int B = 5 ;
int C = 7;
int D = 8 ;
float []rslt = XandYandZintercept(A, B, C, D);
for ( int i = 0; i < 3 ; i++)
{
Console.Write(rslt[i] + " " );
}
}
}
|
Javascript
<script>
function XandYandZintercept(A, B, C, D) {
var x = -D / A;
var y = -D / B;
var z = -D / C;
return [x, y, z];
}
function equation_plane(p, q, r) {
var x1 = p[0];
var y1 = p[1];
var z1 = p[2];
var x2 = q[0];
var y2 = q[1];
var z2 = q[2];
var x3 = r[0];
var y3 = r[1];
var z3 = r[2];
var a1 = x2 - x1;
var b1 = y2 - y1;
var c1 = z2 - z1;
var a2 = x3 - x1;
var b2 = y3 - y1;
var c2 = z3 - z1;
var A = b1 * c2 - b2 * c1;
var B = a2 * c1 - a1 * c2;
var C = a1 * b2 - b1 * a2;
var D = -A * x1 - B * y1 - C * z1;
var [x, y, z] = XandYandZintercept(A, B, C, D);
document.write(x + " " + y + " " + z);
}
var x1 = -1;
var y1 = 2;
var z1 = 1;
var x2 = 0;
var y2 = -3;
var z2 = 2;
var x3 = 1;
var y3 = 1;
var z3 = -4;
var p = [x1, y1, z1];
var q = [x2, y2, z2];
var r = [x3, y3, z3];
equation_plane(p, q, r);
</script>
|
Output:
[-4.0, -1.6, -1.1428571428571428]
Time Complexity: O(1)
Auxiliary Space: O(1)
Case 2: When 3 non-collinear points are given.
Examples:
Input: A = (3, 17, 2), B = (4, 8, 5), C = (1, 8, 3)
Output:
1.5
-1.8
3.0
Input: A = (2, 11, 4), B = (7, 8, 3), C = (9, 18, 23)
Output:
1.0
1.75
1.4
Approach:
The idea is to find the Cartesian form of the equation using three points.
- When three points (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) are given, the determinant value of the following matrix gives the Cartesian form.
- | (x – x1) (y – y1) (z – z1) |
| (x2 – x1) (y2 – y1) (z2 – z1)| = 0
| (x3 – x1) (y3 – y1) (z3 – z1)|
- Once the above determinant is found, then the intercepts can be found using the first mentioned approach.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
float * XandYandZintercept( float A, float B,
float C, float D)
{
static float rslt[3];
float x = -D / A;
float y = -D / B ;
float z = -D / C;
rslt[0] = x;
rslt[1] = y;
rslt[2] = z;
return rslt;
}
void equation_plane( int p[], int q[], int r[])
{
int x1 = p[0];
int y1 = p[1];
int z1 = p[2];
int x2 = q[0];
int y2 = q[1];
int z2 = q[2];
int x3 = r[0];
int y3 = r[1];
int z3 = r[2];
int a1 = x2 - x1;
int b1 = y2 - y1;
int c1 = z2 - z1;
int a2 = x3 - x1;
int b2 = y3 - y1;
int c2 = z3 - z1;
int A = b1 * c2 - b2 * c1;
int B = a2 * c1 - a1 * c2;
int C = a1 * b2 - b1 * a2;
int D = (- A * x1 - B * y1 - C * z1);
float * rslt=XandYandZintercept(A, B, C, D);
for ( int i = 0; i < 3; i++)
{
cout << rslt[i] << " " ;
}
}
int main()
{
int x1 =-1;
int y1 = 2;
int z1 = 1;
int x2 = 0;
int y2 =-3;
int z2 = 2;
int x3 = 1;
int y3 = 1;
int z3 =-4;
int p[3] = {x1, y1, z1};
int q[3] = {x2, y2, z2};
int r[3] = {x3, y3, z3};
equation_plane(p, q, r);
}
|
Java
import java.util.*;
class solution{
static double [] XandYandZintercept( double A, double B,
double C, double D)
{
double []rslt = new double [ 3 ];
double x = -D / A;
double y = -D / B ;
double z = -D / C;
rslt[ 0 ] = x;
rslt[ 1 ] = y;
rslt[ 2 ] = z;
return rslt;
}
static void equation_plane( int []p, int []q, int []r)
{
int x1 = p[ 0 ];
int y1 = p[ 1 ];
int z1 = p[ 2 ];
int x2 = q[ 0 ];
int y2 = q[ 1 ];
int z2 = q[ 2 ];
int x3 = r[ 0 ];
int y3 = r[ 1 ];
int z3 = r[ 2 ];
int a1 = x2 - x1;
int b1 = y2 - y1;
int c1 = z2 - z1;
int a2 = x3 - x1;
int b2 = y3 - y1;
int c2 = z3 - z1;
int A = b1 * c2 - b2 * c1;
int B = a2 * c1 - a1 * c2;
int C = a1 * b2 - b1 * a2;
int D = (- A * x1 - B * y1 - C * z1);
double []rslt = XandYandZintercept(A, B, C, D);
for ( int i = 0 ; i < 3 ; i++)
{
System.out.printf(rslt[i]+ " " );
}
}
public static void main(String args[])
{
int x1 =- 1 ;
int y1 = 2 ;
int z1 = 1 ;
int x2 = 0 ;
int y2 =- 3 ;
int z2 = 2 ;
int x3 = 1 ;
int y3 = 1 ;
int z3 =- 4 ;
int []p = {x1, y1, z1};
int []q = {x2, y2, z2};
int []r = {x3, y3, z3};
equation_plane(p, q, r);
}
}
|
Python3
def XandYandZintercept(A, B, C, D):
x = - D / A
y = - D / B
z = - D / C
return [x, y, z]
def equation_plane(p, q, r):
x1 = p[ 0 ]
y1 = p[ 1 ]
z1 = p[ 2 ]
x2 = q[ 0 ]
y2 = q[ 1 ]
z2 = q[ 2 ]
x3 = r[ 0 ]
y3 = r[ 1 ]
z3 = r[ 2 ]
a1 = x2 - x1
b1 = y2 - y1
c1 = z2 - z1
a2 = x3 - x1
b2 = y3 - y1
c2 = z3 - z1
A = b1 * c2 - b2 * c1
B = a2 * c1 - a1 * c2
C = a1 * b2 - b1 * a2
D = ( - A * x1 - B * y1 - C * z1)
print (XandYandZintercept(A, B, C, D))
x1 = - 1
y1 = 2
z1 = 1
x2 = 0
y2 = - 3
z2 = 2
x3 = 1
y3 = 1
z3 = - 4
equation_plane((x1, y1, z1), (x2, y2, z2), (x3, y3, z3))
|
C#
using System;
class GFG{
static double [] XandYandZintercept( double A,
double B,
double C,
double D)
{
double [] rslt = new double [3];
double x = -D / A;
double y = -D / B ;
double z = -D / C;
rslt[0] = x;
rslt[1] = y;
rslt[2] = z;
return rslt;
}
static void equation_plane( int [] p,
int [] q,
int [] r)
{
int x1 = p[0];
int y1 = p[1];
int z1 = p[2];
int x2 = q[0];
int y2 = q[1];
int z2 = q[2];
int x3 = r[0];
int y3 = r[1];
int z3 = r[2];
int a1 = x2 - x1;
int b1 = y2 - y1;
int c1 = z2 - z1;
int a2 = x3 - x1;
int b2 = y3 - y1;
int c2 = z3 - z1;
int A = (b1 * c2 -
b2 * c1);
int B = (a2 * c1 -
a1 * c2);
int C = (a1 * b2 -
b1 * a2);
int D = (- A * x1 -
B * y1 -
C * z1);
double [] rslt = XandYandZintercept(A, B,
C, D);
for ( int i = 0; i < 3; i++)
{
Console.Write(rslt[i] + " " );
}
}
static void Main()
{
int x1 =-1;
int y1 = 2;
int z1 = 1;
int x2 = 0;
int y2 =-3;
int z2 = 2;
int x3 = 1;
int y3 = 1;
int z3 =-4;
int [] p = {x1, y1, z1};
int [] q = {x2, y2, z2};
int [] r = {x3, y3, z3};
equation_plane(p, q, r);
}
}
|
Javascript
<script>
function XandYandZintercept(A, B, C, D)
{
let rslt = new Array(3);
let x = -D / A;
let y = -D / B ;
let z = -D / C;
rslt[0] = x;
rslt[1] = y;
rslt[2] = z;
return rslt;
}
function equation_plane(p, q, r)
{
let x1 = p[0];
let y1 = p[1];
let z1 = p[2];
let x2 = q[0];
let y2 = q[1];
let z2 = q[2];
let x3 = r[0];
let y3 = r[1];
let z3 = r[2];
let a1 = x2 - x1;
let b1 = y2 - y1;
let c1 = z2 - z1;
let a2 = x3 - x1;
let b2 = y3 - y1;
let c2 = z3 - z1;
let A = b1 * c2 - b2 * c1;
let B = a2 * c1 - a1 * c2;
let C = a1 * b2 - b1 * a2;
let D = (- A * x1 - B * y1 - C * z1);
let rslt = XandYandZintercept(A, B, C, D);
for (let i = 0; i < 3; i++)
{
document.write(rslt[i] + " " );
}
}
let x1 = -1;
let y1 = 2;
let z1 = 1;
let x2 = 0;
let y2 = -3;
let z2 = 2;
let x3 = 1;
let y3 = 1;
let z3 = -4;
let p = [ x1, y1, z1 ];
let q = [ x2, y2, z2 ];
let r = [ x3, y3, z3 ];
equation_plane(p, q, r);
</script>
|
Output:
[-0.11538461538461539, -0.42857142857142855, -0.3333333333333333]
Time Complexity: O(1), Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...