如果使用C ++给出两个相邻边的矢量,则求出平行四边形的面积。
假设对于平行四边形的两个相邻边,我们有两个向量,形式为$x\hat{i}+y\hat{j}+z\hat{k}$。我们的任务是找到平行四边形的面积。平行四边形的面积是两个向量的叉积的大小。(|A×B|)
$$\rvert\vec{A}\times\vec{B}\rvert=\sqrt{\lgroupy_{1}*z_{2}-y_{2}*z_{1}\rgroup^{2}+\lgroupx_{1}*z_{2}-x_{2}*z_{1}\rgroup^{2}+\lgroupx_{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;
}
int main() {
float A[] = {3, 1, -2};
float B[] = {1, -3, 4};
float a = area(A, B);
cout << "Area = " << a;
}输出结果
Area = 17.3205