[英]Calculating angle between a line and a point in 3D
I'm working on a problem in c++ where I need to determine the angle between a line represented as 2 points in 3d (etc, xyz coordinates) and a disconnected point. 我正在研究c ++中的一个问题,我需要确定在3d中表示为2点(例如,xyz坐标)的线与断开点之间的角度。 Here are some pictures that might be easier to understand.
以下是一些可能更容易理解的图片。
This is in 2D to display it easier 以2D形式显示以使其更容易
So what I need help with is finding this angle 所以我需要帮助的是找到这个角度
I've been searching for several hours to solve this now, and I suspect that I've just missed something obvious. 我一直在寻找几个小时来解决这个问题,我怀疑我只是错过了一些明显的东西。 But if anyone can help me with this I will be very greatful:)
但是如果有人可以帮助我,我将非常感激:)
You have 2 vectors, first related to line in 3D and other vector connecting end point of the line and a point in 3D. 您有2个向量,第一个向量与3D中的线有关,其他向量与该线的端点和3D中的一个点相连。
To calculate angle theta
between 2 vectors, you can take advantage of the fact that V1.V2 = |V1| x |V2| x consine(theta)
要计算两个矢量之间的角度
theta
,您可以利用以下事实: V1.V2 = |V1| x |V2| x consine(theta)
V1.V2 = |V1| x |V2| x consine(theta)
Here is code snippet I copied from here , to calculate dot product. 这是我从此处复制的代码段,以计算点积。
#include<numeric>
int main() {
double V1[] = {1, 2, 3}; // vector direction i.e. point P2 - point P1
double V2[] = {4, 5, 6}; // vector direction i.e. point P3 - point P2
std::cout << "The scalar product is: "
<< std::inner_product(begin(V1), end(V1), begin(V2), 0.0);
// TODO: Get theta
return 0;
}
Once you have the dot product, divide it by magnitudes of 2 vectors and then take inverse consine to get theta. 获得点积后,将其除以2个向量的大小,然后取反余弦来获得theta。
Use the Law of Cosines : 使用余弦定律 :
gamma = acos((asq + bsq - csq) / 2 / sqrt(asq*bsq))
where asq
and bsq
are the squared distances between the vertex and the other two points, and csq
is the squared distance between those two points. 其中
asq
和bsq
是顶点与其他两个点之间的平方距离,而csq
是这两个点之间的平方距离。
let say you have A(x 1 ,y 1 ,z 1 ) B(x 2 ,y 2 ,z 2 ) C(x 3 ,y 3 ,z 3 ) and the common point is B. So the equation of the line AB becomes : (x 1 -x 2 )i + (y 1 -y 2 )j + (z 1 -z 2 )k
and that for BC it is : (x 2 -x 3 )i + (y 2 -y 3 )j + (z 2 -z 3 )k
假设您有A(x 1 ,y 1 ,z 1 )B(x 2 ,y 2 ,z 2 )C(x 3 ,y 3 ,z 3 )并且公共点是B。所以线的方程式AB变为:
(x 1 -x 2 )i + (y 1 -y 2 )j + (z 1 -z 2 )k
,而BC则为: (x 2 -x 3 )i + (y 2 -y 3 )j + (z 2 -z 3 )k
Cos theta
= (AB.BC)/(|AB|*|BC|)
Cos
theta
= (AB.BC)/(|AB|*|BC|)
Here is the code 这是代码
#include<iostream>
#include<math.h>
#define PI 3.14159265
using namespace std;
int main()
{
int x1,x2,x3,y1,y2,y3,z1,z2,z3;
cout<<"for the first\n";
cin>>x1>>y1>>z1;
cout<<"\nfor the second\n";
cin>>x2>>y2>>z2;
cout<<"\nfor the third\n";
cin>>x3>>y3>>z3;
float dot_product = (x1-x2)*(x2-x3) + (y1-y2)*(y2-y3)+ (z1-z2)*(z2-z3);
float mod_denom1 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
float mod_denom2 = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) + (z2-z3)*(z2-z3));
float cosnum = (dot_product/((mod_denom1)*(mod_denom2)));
float cos = acos(cosnum)*180/PI;
cout<< cos;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.