已知两个非零向量,作
在空间任取一点O,作
,则
称为向量
的夹角,如下图当
,两向量同向,否则等于180度两向量反向。
求两向量的公式为:


C++代码如下:
#include
#include
using namespace std; #define PI 3. int main() {
float a[4];// 存放第一个向量的起点和重点 float b[4];// 存放第二个向量的起点和重点 cout << "请输入第一个向量的起点坐标:" << endl; cin >> a[0] >> a[1]; cout << "请输入第一个向量的终点坐标:" << endl; cin >> a[2] >> a[3]; cout << "请输入第二个向量的起点坐标:" << endl; cin >> b[0] >> b[1]; cout << "请输入第二个向量的终点坐标:" << endl; cin >> b[2] >> b[3]; float vector1x = a[0] - a[2]; float vector1y = a[1] - a[3]; float vector2x = b[0] - b[2]; float vector2y = b[1] - b[3]; float t = ((vector1x)*(vector2x) + (vector1y)*(vector2y))/ (sqrt(pow(vector1x, 2) + pow(vector1y, 2))*sqrt(pow(vector2x, 2) + pow(vector2y, 2))); cout << "这两个向量的夹角为:" << acos(t)*(180 / PI) << "度" << endl; system("pause"); return 0; }

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177321.html原文链接:https://javaforall.net
