|
- #include <cmath> // 包含acos等函数
- double angleBetweenVectors(const AcGeVector2d& v1, const AcGeVector2d& v2) {
- // 计算点积
- double dotProduct = v1.dotProduct(v2);
-
- // 计算各自的模长
- double lengthV1 = v1.length();
- double lengthV2 = v2.length();
-
- // 防止除以零错误
- if (lengthV1 == 0 || lengthV2 == 0) {
- throw std::invalid采用argument("One of the vectors has zero length.");
- }
-
- // 计算cos(θ)
- double cosTheta = dotProduct / (lengthV1 * lengthV2);
-
- // 使用反余弦函数得到角度θ,范围在[0, π]
- double thetaRadians = std::acos(cosTheta);
-
- // 如果需要转换为度数
- // double thetaDegrees = thetaRadians * (180.0 / M采用PI);
-
- return thetaRadians;
- }
复制代码 |
|