3d 空间中 3 点之间的角度

问题描述 投票:0回答:5

我有 3 个包含 X、Y、Z 坐标的点:

var A = {x: 100, y: 100, z: 80},
    B = {x: 100, y: 175, z: 80},
    C = {x: 100, y: 100, z: 120};

坐标是来自 3d CSS 变换的像素。 如何获得向量 BA 和 BC 之间的角度? 数学公式就可以了,JavaScript 代码会更好。 谢谢你。

enter image description here

math 3d geometry
5个回答
40
投票

在伪代码中,向量 BA(称为 v1)是:

v1 = {A.x - B.x, A.y - B.y, A.z - B.z}

类似地,向量 BC(称为 v2)是:

v2 = {C.x - B.x, C.y - B.y, C.z - B.z}

v1
v2
的点积是它们之间角度的余弦函数(按它们大小的乘积缩放)。所以首先标准化
v1
v2
:

v1mag = sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z)
v1norm = {v1.x / v1mag, v1.y / v1mag, v1.z / v1mag}

v2mag = sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z)
v2norm = {v2.x / v2mag, v2.y / v2mag, v2.z / v2mag}

然后计算点积:

res = v1norm.x * v2norm.x + v1norm.y * v2norm.y + v1norm.z * v2norm.z

最后恢复角度:

angle = acos(res)

6
投票
double GetAngleABC( double* a, double* b, double* c )
{
    double ab[3] = { b[0] - a[0], b[1] - a[1], b[2] - a[2] };
    double bc[3] = { c[0] - b[0], c[1] - b[1], c[2] - b[2]  };

    double abVec = sqrt(ab[0] * ab[0] + ab[1] * ab[1] + ab[2] * ab[2]);
    double bcVec = sqrt(bc[0] * bc[0] + bc[1] * bc[1] + bc[2] * bc[2]);

    double abNorm[3] = {ab[0] / abVec, ab[1] / abVec, ab[2] / abVec};
    double bcNorm[3] = {bc[0] / bcVec, bc[1] / bcVec, bc[2] / bcVec};

    double res = abNorm[0] * bcNorm[0] + abNorm[1] * bcNorm[1] + abNorm[2] * bcNorm[2];

    return acos(res)*180.0/ 3.141592653589793;
}


double a[] = {1, 0, 0};

double b[] = {0, 0, 0};

double c[] = {0, 1, 0};

std::cout<< "The angle of ABC is " << GetAngleABC(a,b,c)<< "º " << std::endl;

3
投票

Python 中相同(输出以度为单位):

import numpy as np
import math 
import time

def angle_2p_3d(a, b, c):       

    v1 = np.array([ a[0] - b[0], a[1] - b[1], a[2] - b[2] ])
    v2 = np.array([ c[0] - b[0], c[1] - b[1], c[2] - b[2] ])

    v1mag = np.sqrt([ v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] ])
    v1norm = np.array([ v1[0] / v1mag, v1[1] / v1mag, v1[2] / v1mag ])

    v2mag = np.sqrt(v2[0] * v2[0] + v2[1] * v2[1] + v2[2] * v2[2])
    v2norm = np.array([ v2[0] / v2mag, v2[1] / v2mag, v2[2] / v2mag ])
    res = v1norm[0] * v2norm[0] + v1norm[1] * v2norm[1] + v1norm[2] * v2norm[2]
    angle_rad = np.arccos(res)

    return math.degrees(angle_rad)


p1 = np.array([1,0,0])
p2 = np.array([0,0,0])
p3 = np.array([0,0,1])

start = time.time()
angle= angle_2p_3d(p1, p2, p3)
end = time.time()

print("angle: ", angle)
print("elapsed in: ", end - start)

输出:

角度:90.0

已过去:8.392333984375e-05


2
投票

@Roger Swift 算法

func SCNVector3Angle(start: SCNVector3, mid: SCNVector3, end: SCNVector3) -> Double {
    let v1 = (start - mid)
    let v2 = (end - mid)
    let v1norm = v1.normalized()
    let v2norm = v2.normalized()

    let res = v1norm.x * v2norm.x + v1norm.y * v2norm.y + v1norm.z * v2norm.z
    let angle: Double = Double(GLKMathRadiansToDegrees(acos(res)))
    return angle
}

/**
* Subtracts two SCNVector3 vectors and returns the result as a new SCNVector3.
*/
func - (left: SCNVector3, right: SCNVector3) -> SCNVector3 {
    return SCNVector3Make(left.x - right.x, left.y - right.y, left.z - right.z)
}

extension SCNVector3
{
    /**
    * Returns the length (magnitude) of the vector described by the SCNVector3
    */
    func length() -> Float {
        return sqrtf(x*x + y*y + z*z)
    }

    /**
    * Normalizes the vector described by the SCNVector3 to length 1.0 and returns
    * the result as a new SCNVector3.
    */
    func normalized() -> SCNVector3 {
        return self / length()
    }
}

0
投票

现在 MATLAB (R2018a) 版本:

%% Calculate the Anlge Between 3 Points in 3D Space
clear, clc, close all
format compact

%% Test calculating the angle between 3 points
%
% Find angle (∠BC) between vector AB and vector BC
%
%      *  <--- C (100, 100, 120)
%     /
%    / 
%   /
%  *  <------- A (100, 100, 80)
%  |
%  |
%  |
%  *  <------- B (100, 175, 80)
aa = [100, 100, 80];
bb = [100, 175, 80];
cc = [100, 100, 120];
theta = angle_3points(aa, bb, cc);
fprintf("Angle: %.4f degrees\n", theta);  % Result: Angle: 28.0725 degrees


%% Function definition for calculating the angle between 3 points in 3D space
function theta = angle_3points(a, b, c)
    % THETA = ANGLE_3POINTS(A, B, C)
    %  Function to calculate angle between 3 points
    %  
    %  Inputs:
    %    a: The first point. 1x3 vector.
    %    b: The second point. 1x3 vector.
    %    c: The third point. 1x3 vector.
    %  Outputs:
    %    theta: The angle between vector ab and vector bc in degrees.
    if nargin < 3 || isempty(a) || isempty(b) || isempty(c)
        error 'Parameters `a`, `b`, and `c` are all required inputs.';
    end
    
    % Check the sizes of vectors a, b, and c
    [n, m] = size(a);
    if n ~= 1 || m ~= 3
        error 'Parameter `a` must be a 1x3 vector.';
    end
    [n, m] = size(b);
    if n ~= 1 || m ~= 3
        error 'Parameter `b` must be a 1x3 vector.';
    end
    [n, m] = size(c);
    if n ~= 1 || m ~= 3
        error 'Parameter `c` must be a 1x3 vector.';
    end
    clear n m;

    v1 = a - b;
    v1_mag = sqrt(sum(v1.^2));
    v1_norm = v1 ./ v1_mag;
    
    v2 = c - b;
    v2_mag = sqrt(sum(v2.^2));
    v2_norm = v2 ./ v2_mag;
    
    theta = v1_norm(1)*v2_norm(1) + v1_norm(2)*v2_norm(2) + ...
            v1_norm(3)*v2_norm(3);
    theta = acos(theta) * 180 / pi();
end
© www.soinside.com 2019 - 2024. All rights reserved.