Program to find equation of a plane passing through 3 points

Given three points (x1, y1, z1), (x2, y2, z2), (x3, y3, z3). The task is to find the equation of the plane passing through these 3 points.

Examples:

Input: x1 = -1 y1 = w z1 = 1
x2 = 0 y2 = -3 z2 = 2
x3 = 1 y3 = 1 z3 = -4
Output: equation of plane is 26 x + 7 y + 9 z + 3 = 0.

Input: x1 = 2, y1 = 1, z1 = -1, 1
x2 = 0, y2 = -2, z2 = 0
x3 = 1, y3 = -1, z3 = 2
Output: equation of plane is -7 x + 5 y + 1 z + 10 = 0.

Approach: Let P, Q and R be the three points with coordinates (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) respectively. Then the equation of plane is a * (x – x0) + b * (y – y0) + c * (z – z0) = 0, where a, b, c are direction ratios of normal to the plane and (x0, y0, z0) are co-ordinates of any point(i.e P, Q, or R) passing through the plane. For finding direction ratios of normal to the plane, take any two vectors in plane, let it be vector PQ, vector PR.

=> Vector PQ = (x2 - x1, y2 - y1, z2 - z1) = (a1, b1, c1).
=> Vector PR = (x3 - x1, y3 - y1, z3 - z1) = (a2, b2, c2).

Normal vector to this plane will be vector PQ x vector PR.

=> PQ X PR = (b1 * c2 - b2 * c1) i 
              + (a2 * c1 - a1 * c2) j 
              + (a1 * b2 - b1 *a2) k = ai + bj + ck.

Direction ratios of normal vector will be a, b, c. Taking any one point from P, Q, or R, let its co-ordinate be (x0, y0, z0). Then the equation of plane passing through a point(x0, y0, z0) and having direction ratios a, b, c will be

=> a * (x - x0) + b * (y - y0) + c * (z - z0) = 0.
=> a * x - a * x0 + b * y - b * y0 + c * z - c * z0 = 0.
=> a * x + b * y + c * z + (- a * x0 - b * y0 - c * z0) = 0.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find equation of a plane 
// passing through given 3 points.
#include <bits/stdc++.h> 
#include<math.h>
#include <iostream>
#include <iomanip>
  
using namespace std;
  
// Function to find equation of plane.
void equation_plane(float x1, float y1, 
                    float z1, float x2,
                    float y2, float z2, 
                    float x3, float y3, float z3)
{
    float a1 = x2 - x1;
    float b1 = y2 - y1;
    float c1 = z2 - z1;
    float a2 = x3 - x1;
    float b2 = y3 - y1;
    float c2 = z3 - z1;
    float a = b1 * c2 - b2 * c1;
    float b = a2 * c1 - a1 * c2;
    float c = a1 * b2 - b1 * a2;
    float d = (- a * x1 - b * y1 - c * z1);
    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    cout << "equation of plane is " << a << " x + " << b
        << " y + " << c << " z + " << d << " = 0.";
}
  
// Driver Code 
int main()
{
      
    float x1 =-1;
    float y1 = 2;
    float z1 = 1;
    float x2 = 0;
    float y2 =-3;
    float z2 = 2;
    float x3 = 1;
    float y3 = 1;
    float z3 =-4;
    equation_plane(x1, y1, z1, x2, y2, z2, x3, y3, z3);
    return 0;
}
// This code is contributed 
// by Amber_Saxena.

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find equation of a plane 
// passing through given 3 points.
  
#include<stdio.h>
  
// Function to find equation of plane.
void equation_plane(float x1, float y1, 
                    float z1, float x2,
                    float y2, float z2, 
                    float x3, float y3, float z3)
{
    float a1 = x2 - x1;
    float b1 = y2 - y1;
    float c1 = z2 - z1;
    float a2 = x3 - x1;
    float b2 = y3 - y1;
    float c2 = z3 - z1;
    float a = b1 * c2 - b2 * c1;
    float b = a2 * c1 - a1 * c2;
    float c = a1 * b2 - b1 * a2;
    float d = (- a * x1 - b * y1 - c * z1);
    printf("equation of plane is %.2f x + %.2f"
        " y + %.2f z + %.2f = 0.",a,b,c,d);
    return;
}
  
// Driver Code
int main()
{
    float x1 =-1;
    float y1 = 2;
    float z1 = 1;
    float x2 = 0;
    float y2 =-3;
    float z2 = 2;
    float x3 = 1;
    float y3 = 1;
    float z3 =-4;
    equation_plane(x1, y1, z1, x2, y2, z2, x3, y3, z3);
    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 equation 
// of a plane passing through 
// given 3 points.
import java .io.*;
  
class GFG
{
      
// Function to find equation of plane.
static void equation_plane(float x1, float y1, 
                           float z1, float x2,
                           float y2, float z2,
                           float x3, float y3,
                           float z3)
{
    float a1 = x2 - x1;
    float b1 = y2 - y1;
    float c1 = z2 - z1;
    float a2 = x3 - x1;
    float b2 = y3 - y1;
    float c2 = z3 - z1;
    float a = b1 * c2 - b2 * c1;
    float b = a2 * c1 - a1 * c2;
    float c = a1 * b2 - b1 * a2;
    float d = (- a * x1 - b * y1 - c * z1);
    System.out.println("equation of plane is " + a +
                       " x + " + b + " y + " + c + 
                       " z + " + d + " = 0.");
}
  
// Driver code
public static void main(String[] args)
{
    float x1 =-1;
    float y1 = 2;
    float z1 = 1;
    float x2 = 0;
    float y2 =-3;
    float z2 = 2;
    float x3 = 1;
    float y3 = 1;
    float z3 =-4;
    equation_plane(x1, y1, z1, x2, 
                   y2, z2, x3, y3, z3);
}
}
  
// This code is contributed 
// by Amber_Saxena. 

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find equation of a plane 
# passing through given 3 points.
  
# Function to find equation of plane.
def equation_plane(x1, y1, z1, x2, y2, z2, x3, y3, z3): 
      
    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 "equation of plane is ",
    print a, "x +",
    print b, "y +",
    print c, "z +",
    print d, "= 0."
  
# Driver Code 
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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find equation 
// of a plane passing through 
// given 3 points.
using System; 
  
class GFG
{
      
// Function to find equation of plane.
static void equation_plane(float x1, float y1, 
                           float z1, float x2,
                           float y2, float z2,
                           float x3, float y3,
                           float z3)
{
    float a1 = x2 - x1;
    float b1 = y2 - y1;
    float c1 = z2 - z1;
    float a2 = x3 - x1;
    float b2 = y3 - y1;
    float c2 = z3 - z1;
    float a = b1 * c2 - b2 * c1;
    float b = a2 * c1 - a1 * c2;
    float c = a1 * b2 - b1 * a2;
    float d = (- a * x1 - b * y1 - c * z1);
    Console.Write("equation of plane is " + a +
                      "x + " + b + "y + " + c + 
                          "z + " + d + " = 0");
}
  
// Driver code
public static void Main()
{
    float x1 =-1;
    float y1 = 2;
    float z1 = 1;
    float x2 = 0;
    float y2 =-3;
    float z2 = 2;
    float x3 = 1;
    float y3 = 1;
    float z3 =-4;
    equation_plane(x1, y1, z1,
                   x2, y2, z2, 
                   x3, y3, z3);
}
}
  
// This code is contributed
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find equation 
// of a plane passing through
// given 3 points.
  
// Function to find equation of plane.
function equation_plane($x1, $y1, $z1
                        $x2, $y2, $z2
                        $x3, $y3, $z3
    $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);
    echo sprintf("equation of the plane is %.2fx"
                     " + %.2fy + %.2fz + %.2f = 0"
                                   $a, $b, $c, $d);
  
// Driver Code 
$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);
  
// This code is contributed 
// by Amber_Saxena.
?> 

chevron_right


Output:

equation of plane is  26 x + 7 y + 9 z + 3 = 0.


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.



Improved By : Amber_Saxena, Ita_c