简体   繁体   English

给定 3D 空间中的一条线,我如何找到从它到点的角度?

[英]Given a line in 3D space, how do I find the angle from it to a point?

I have two sets of points in 3D space.我在 3D 空间中有两组点。 I'd like to draw a line that runs through the center of both sets of points, and then find the angle from that line to each point.我想画一条穿过两组点中心的线,然后找到从该线到每个点的角度。 From there, I'll match points in the two sets up based on how close together their two angles are.从那里,我将根据两个角度的接近程度来匹配两个设置中的点。

I know how to find the center of each set of points (just average them all together) and I know how to match them up (even accounting for the fact they'll wrap around), but I don't know how to find the angle from the line to the point.我知道如何找到每组点的中心(只是将它们平均在一起)并且我知道如何匹配它们(甚至考虑到它们会环绕的事实),但我不知道如何找到从线到点的角度。

Basically I'm saying I want all the points projected onto a plane that is centered on and perpendicular to the line running through the centers, and I want the angles from that line to each point on that plane.基本上我是说我希望所有点都投影到以穿过中心的直线为中心并垂直于该直线的平面上,并且我希望从该直线到该平面上每个点的角度。

I hope I've made myself clear... I don't have much formal training in this stuff (kind of reminds me of Diff Eq from college several years ago), so do ask if I've maybe misused a term and confused you.我希望我已经把自己说清楚了……我在这方面没有太多正式的培训(有点让我想起几年前大学时的 Diff Eq),所以请问我是否误用了一个术语并感到困惑你。

I figure I can translate a solution from any other language to work for me since I assume this is mostly just language agnostic math, but I'm working with Three.JS so if your solution works in javascript/with Three.JS (here's their Vector3 class documentation , just so you know what helper functions it provides) that'd be most helpful.我想我可以将任何其他语言的解决方案翻译成对我有用,因为我认为这主要是与语言无关的数学,但我正在使用 Three.JS 所以如果你的解决方案适用于 javascript/Three.JS(这是他们的Vector3 类文档,只是为了让您知道它提供了哪些帮助函数),这将是最有帮助的。 Thanks!谢谢!

In general The dot product of 2 vectors is equal the cosine of the angle between the 2 vectors multiplied by the magnitude (length) of both vectors.一般来说,2个向量的积等于2个向量之间夹角的余弦乘以两个向量的幅度(长度)。

dot( A, B ) == | A | * | B | * cos( angle_A_B ) 

This follows, that the dot product of 2 unit vectors is equal the cosine of the angle between the 2 vectors, because the length of a unit vector is 1.因此,2 个单位向量的积等于 2 个向量之间夹角的余弦,因为单位向量的长度是 1。

uA = normalize( A )
uB = normalize( B )
cos( angle_A_B ) == dot( uA, uB )

点 A B


In *three.js* all the calculations can be done by the operations of a [`THREE.Vector3`][2]: 在 *three.js* 中,所有计算都可以通过 [`THREE.Vector3`][2] 的操作来完成:
var angle_in_radians = a.angleTo(b);

As mentioned in the comment below, in *three.js* there is an operation **`.angleTo`**, which simplifies the things a lot: 正如下面的评论中提到的,在 *three.js* 中有一个操作 **`.angleTo`**,它简化了很多事情:
 var angle_in_radians = a.angleTo(b);

If you have 4 points `Pa`, `Pb`, `Pc`, `Pd`, which define 2 lines from `Pa` to `Pb` and form `Pc` to `Pd`, then the 2 vectors can be calculated as follows: 如果你有 4 个点 `Pa`、`Pb`、`Pc`、`Pd`,它们定义了从 `Pa` 到 `Pb` 的 2 条线,并形成了 `Pc` 到 `Pd`,那么这两个向量就可以计算出来如下:
 var Pa = new THREE.Vector3( ... ); var Pb = new THREE.Vector3( ... ); var Pc = new THREE.Vector3( ... ); var Pd = new THREE.Vector3( ... ); var a = new THREE.Vector3(); a.copy( Pb ).sub( Pa ); var b = new THREE.Vector3(); a.copy( Pd ).sub( Pc );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM