Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chương trình tìm phương trình mặt phẳng đi qua 3 điểm trong C++


Cho 3 điểm không thẳng hàng. Tìm phương trình mặt phẳng: ax + by + cz + d = 0.

Thuật toán

  • Tạo 2 vectơ: AB = (x2-x1, y2-y1, z2-z1), AC = (x3-x1, y3-y1, z3-z1).
  • Vector pháp tuyến n = AB × AC (tích có hướng).
  • a = n.x, b = n.y, c = n.z.
  • d = -(a*x1 + b*y1 + c*z1).

Ví dụ triển khai (C++)

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;

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, b1 = y2 - y1, c1 = z2 - z1;
    float a2 = x3 - x1, b2 = y3 - y1, 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);
    cout << fixed << setprecision(2);
    cout << "Equation of plane is " << a << " x + " << b << " y + " << c << " z + " << d << " = 0";
}

int main() {
    equation_plane(-1, 2, 1, 0, -3, 2, 1, 1, -4);
    return 0;
}

Đầu ra

Equation of plane is 26.00 x + 7.00 y + 9.00 z + 3.00 = 0