日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

1、使用库函数计算两个向量的夹角

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1、使用库函数计算两个向量的夹角 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先需要安裝Eigen庫。安裝方法如下鏈接:https://blog.csdn.net/m0_37957160/article/details/109581546

使用到的庫函數是:

C ++ 中的acos()函數?

acos() function?is a library function of?cmath?header, it is used to find the principal value of the arc cosine of the given number, it accepts a number (x) and returns the principal value of the arc cosine of?x?in radians.

ACOS()函數CMATH報頭的庫函數,它被用于查找給定數的反余弦的主值,它接受一個數字(x)和返回x的以弧度為單位的反余弦的主要值。

Note:?Value (x) must be between -1 to +1, else it will return a domain error (nan).

注意:值( x )必須介于-1到+1之間,否則它將返回域錯誤( nan )。

Syntax of acos() function:

acos()函數的語法:

    acos(x);

Parameter(s):?x?– is the value whose arc cosine to be calculated.

參數:?x –是要計算其反余弦值的值。

Return value:?double?– it returns double type value that is the principal value of the arc cosine of the given number?x.

返回值:?double-它返回double類型值,該類型值是給定數字x的反余弦的主要值。

計算向量夾角的例子如下:

#include <iostream>
#include <Eigen/Dense>
#include <cmath>using namespace Eigen;
using namespace std;#define M_PI 3.1415926
int main()
{Eigen::Vector3d S;S(0) = 0;S(1) = 5;S(2) = 0;//把這個注釋掉就不能計算二維的叉乘????cout << "Here is the vector v:\n" << S << endl;cout << "S的模長:" << S.norm() << endl;Eigen::Vector3d Y;Y(0) = 10;Y(1) = 10;Y(2) = 0;cout << "Here is the vector Y:\n" << Y << endl;cout << "Y的模長:" << Y.norm() << endl;//向量的模//cout << "Dot product: " << v.dot(w) << endl;double dot = S.dot(Y);cout << "點乘Dot product: " << dot << endl;double delta = dot / (S.norm()*Y.norm());//兩種求delta的方法都行//double delta = dot / (sqrt(S(0)*S(0) + S(1)*S(1) + S(2)*S(2))*sqrt(Y(0)*Y(0) + Y(1)*Y(1) + Y(2)*Y(2)));//角度cos值cout << "角度值:" << delta << endl;double theta = acos(delta) * 180 / M_PI;//弧度轉化為角度cout << "兩個向量的夾角為:" << theta << endl;//cout << "向量的模: " <<norm(w)<< endl;//cout << "Cross product:\n" << v.cross(w) << endl;//向量v叉乘向量W為/*Eigen::Vector3d u;u = v.cross(w);cout << "Here is the vector u:\n" << u << endl;double m1 = sqrt(u(0)*u(0) + u(1)*u(1) + u(2)*u(2));double m2= sqrt(w(0)*w(0) + w(1)*w(1) + w(2)*w(2));double distance = m1 / m2;std::cout <<"距離是:" <<distance << std::endl;*/}

結果:

1rad=(/180)1°=1/180rad,其中rad是弧bai度的單位、du通常可以省略不寫。zhi

公式為:角度=弧度×180°÷π? ?弧度=角度×π÷180°

擴展:

(1)求兩點之間的距離:

	CPoint pt1(10, 10), pt2(5, 5);Vector2f v4(pt1.x, pt1.y), v5(pt2.x, pt2.y);float  len = (v4 - v5).norm();

(2)一點到原點的距離,pt(x,y)

Vector2f v1(x,y);
float  res1= v1.norm();              //   等于 sqrt(x^2+y^2) , 即距離
float  res2 = v1.squaredNorm();     //    (x^2+y^2)

(3)兩個向量之間的夾角

Vector2f v1,Vector2f v2;
double cosValNew=v1.dot(v2) /(v1.norm()*v2.norm()); //角度cos值
double angleNew = acos(cosValNew) * 180 / M_PI;     //弧度角

?

總結

以上是生活随笔為你收集整理的1、使用库函数计算两个向量的夹角的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。