1 计算两个二维向量夹角

#include <iostream>
#include <cmath>

struct PoseInfo {
    float x;
    float y;
};

typedef PoseInfo Point2D;
typedef PoseInfo Vector2D;


float Vector2DAngle(const Vector2D& vec1, const Vector2D& vec2)
{
    double PI = 3.141592653;
    float t = (vec1.x * vec2.x + vec1.y * vec2.y) / (sqrt(pow(vec1.x, 2) + pow(vec1.y, 2)) * sqrt(pow(vec2.x, 2) + pow(vec2.y, 2)));
    float angle = acos(t) * (180 / PI);
    return angle;
}

int main()
{
    Point2D vecA_start_point;
    vecA_start_point.x = 0.0;
    vecA_start_point.y = 0.0;

    Point2D vecA_end_point;
    vecA_end_point.x = 1.0;
    vecA_end_point.y = 0.0;

    Point2D vecB_end_point;
    vecB_end_point.x = 1.0;
    vecB_end_point.y = 5.0;

    Vector2D vecA;
    vecA.x = vecA_end_point.x - vecA_start_point.x;
    vecA.y = vecA_end_point.y - vecA_start_point.y;

    Vector2D vecB;
    vecB.x = vecB_end_point.x - vecA_start_point.x;
    vecB.y = vecB_end_point.y - vecA_start_point.y;

    float angle = Vector2DAngle(vecA, vecB);

    std::cout << "angle = " << angle << std::endl;

    getchar();

    return 0;
}

计算结果:

angle = 78.6901

如果您觉得对您有帮助,可以请站长喝一杯咖啡哦!

记得在赞赏备注里写上您的昵称

您可在本站资助名单中查看你的打赏记录哦!

支付宝扫一扫

微信扫一扫

金额随意,礼清义重

当前分类随机文章推荐

全站随机文章推荐

关于本站站长 StubbornHuang
计算几何 – C++计算两个二维向量的夹角-StubbornHuang Blog纵使晴明无雨色,入云深处亦沾衣。