3D中2条线之间的角度

时间:2017-10-17 12:30:28

标签: swift scenekit

我知道如何在2D中获得2点之间的atan2角度,但这在3D中如何工作?: 假设我有3个点A,B,C(所有都是带有x,y,z坐标的SCNVector3 第一行端点A和B. 第二行端点B和C. 现在我想得到两条线之间的角度......(在ios Swift中) 我读了一些关于点积和acos的东西,但不知何故它不起作用......

i = 0:

        var vector1 = SCNVector3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
        var vector2 = SCNVector3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
        var dotProduct = vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z
        var theta = acos(dotProduct)
        var tmp_winkel = GLKMathRadiansToDegrees(theta)

2 个答案:

答案 0 :(得分:2)

dot product考虑​​了向量的范数(大小)。确保你处理单位向量,或除以它们的规范的乘积。

import SceneKit
import simd

var vector1 = float3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
var vector2 = float3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
var dotProduct = dot(normalize(vector1), normalize(vector2))
var theta = acos(dotProduct)

var vector1 = float3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
var vector2 = float3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
var dotProduct = dot(vector1, vector2)
var theta = acos(dotProduct) / (length(vector1) * length(vector2))

答案 1 :(得分:1)

所以为了获得我现在所做的方向(感谢mnuages提示):

var vectorn = cross(normalize(vector1), normalize(vector2))
if (vectorn.y > 0) { //righ hand side }
else { //left hand side}