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