📜  查找通过3点的平面方程的程序

📅  最后修改于: 2021-04-24 19:57:23             🧑  作者: Mango

给定三个点(x1,y1,z1),(x2,y2,z2),(x3,y3,z3)。任务是找到通过这三个点的平面方程。

例子:

方法:设P,Q和R为分别具有坐标(x1,y1,z1),(x2,y2,z2),(x3,y3,z3)的三个点。则平面的等式为a *(x-x0)+ b *(y-y0)+ c *(z-z0)= 0 ,其中a,b,c是法线与平面的方向比,而(x0, y0,z0)是通过平面的任何点(即P,Q或R)的坐标。为了找到法线与平面的方向比,取平面中的任意两个向量,使其为向量PQ,向量PR。

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

到该平面的法线向量将是向量PQ x向量PR。

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

法向矢量的方向比将为a,b,c。从P,Q或R中取任意一点,使其坐标为(x0,y0,z0)。则通过点(x0,y0,z0)且方向比为a,b,c的平面方程为

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

下面是上述方法的实现:

C++
// C++ program to find equation of a plane
// passing through given 3 points.
#include 
#include
#include 
#include 
 
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.


C
// C program to find equation of a plane
// passing through given 3 points.
 
#include
 
// 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.


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


Python
# 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)


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


PHP


Javascript


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