สมมติว่าเรามีเวกเตอร์สองตัวสำหรับด้านประชิดสองด้านของสามเหลี่ยมในรูปแบบ $x\hat{i}+y\hat{j}+z\hat{k}$ งานของเราคือการหาพื้นที่ของสามเหลี่ยม พื้นที่ของสามเหลี่ยมคือขนาดของผลคูณของเวกเตอร์สองตัว (|A x B|)
$$\frac{1}{2}\rvert \vec{A}\times\vec{B}\rvert=\frac{1}{2}\sqrt{\lgroup y_{1}*z_{2}- y_{2}*z_{1}\rgroup^{2}+\lgroup x_{1}*z_{2}-x_{2}*z_{1}\rgroup^{2}+\lgroup x_{1} *y_{2}-x_{2}*y_{1}\rgroup^{2}}$$
ตัวอย่าง
#include<iostream>
#include<cmath>
using namespace std;
float area(float A[], float B[]) {
float area = sqrt(pow((A[1] * B[2] - B[1] * A[2]),2) + pow((A[0] * B[2] - B[0] * A[2]),2) + pow((A[0] * B[1] - B[0] * A[1]),2));
return area*0.5;
}
int main() {
float A[] = {3, 1, -2};
float B[] = {1, -3, 4};
float a = area(A, B);
cout << "Area = " << a;
} ผลลัพธ์
Area = 8.66025