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

穿精又带淫゛_ 提交于 2021-02-07 21:10:31

问题


I have two sets of points in 3D space. 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.

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


回答1:


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.

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.

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


In *three.js* all the calculations can be done by the operations of a [`THREE.Vector3`][2]:
var a = new THREE.Vector3( ... );
var b = new THREE.Vector3( ... );

a.normalize();
b.normalize();

var cosAB = a.dot( b );
var angle_in_radians = Math.acos( cosAB );

As mentioned in the comment below, in *three.js* there is an operation **`.angleTo`**, which simplifies the things a lot:
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:
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 );


来源:https://stackoverflow.com/questions/46750535/given-a-line-in-3d-space-how-do-i-find-the-angle-from-it-to-a-point

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