Angle between 2 Lines in 3D

╄→гoц情女王★ 提交于 2019-12-24 05:49:49

问题


i know how to get Angles with atan2 between 2 Points in 2D, but how does this work in 3D?: lets say i have 3 Points A,B,C (all are SCNVector3 with x,y,z coordinates First Line Endpoints A and B 2nd Line Endpoints B and C Now i want to get the angle between the 2 Lines... (in ios Swift) I read something about the dot product and acos but somehow it does not work...

With 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)

回答1:


the dot product takes the norm (magnitude) of the vectors into account. Make sure you deal with unit vectors, or divide by the product of their norms.

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)

or

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)))



回答2:


so for getting the direction i do now (thanks mnuages for the hint):

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


来源:https://stackoverflow.com/questions/46790176/angle-between-2-lines-in-3d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!