3D 中 2 条线之间的角度 [英] Angle between 2 Lines in 3D

查看:36
本文介绍了3D 中 2 条线之间的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

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

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)

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

这篇关于3D 中 2 条线之间的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆