计算两个向量的叉积的 C++ 程序
这是一个计算两个向量的叉积的 C++ 程序。
我们假设,
M = m1 * i + m2 * j + m3 * k
N = n1 * i + n2 * j + n3 * k.
所以,叉积 = (m2 * n3 – m3 * n2) * i + (m1 * n3 – m3 * n1) * j + (m1 * n1 – m2 * n1) * k
其中 m2 * n3 – m3 * n2、m1 * n3 – m3 * n1 和 m1 * n1 – m2 * n1 是沿 i、j 和 k 方向的单位向量的系数。
算法
Begin Declare a function cProduct(). Declare three vectors v_A[], v_B[], c_P[] of the integer datatype. c_P[0] = v_A[1] * v_B[2] - v_A[2] * v_B[1]. c_P[1] = -(v_A[0] * v_B[2] - v_A[2] * v_B[0]). c_P[2] = v_A[0] * v_B[1] - v_A[1] * v_B[0]. Initialize values in v_A[] vector. Initialize values in v_B[] vector. Initialize c_P[] vector with an integer variable n. Print “Cross product:”. Call the function cProduct() to perform cross product within v_A[] and v_B[]. for (int i = 0; i < n; i++) print the value of c_P[] vector. End.
示例代码
#include
#define n 3
using namespace std;
void crossProduct(int v_A[], int v_B[], int c_P[]) {
c_P[0] = v_A[1] * v_B[2] - v_A[2] * v_B[1];
c_P[1] = -(v_A[0] * v_B[2] - v_A[2] * v_B[0]);
c_P[2] = v_A[0] * v_B[1] - v_A[1] * v_B[0];
}
int main() {
int v_A[] = { 7, 6, 4 };
int v_B[] = { 2, 1, 3 };
int c_P[n];
cout << "Cross product:";
crossProduct(v_A, v_B, c_P);
for (int i = 0; i < n; i++)
cout << c_P[i] << " ";
return 0;
}输出
Cross product: 14 13 -5
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP