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, x.y.z coordinates) and a disconnected point. Here are some pictures that might be easier to understand.
This is in 2D to display it easier
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:)
let say you have A(x1,y1,z1) B(x2,y2,z2) C(x3,y3,z3) and the common point is B. So the equation of the line AB becomes : (x1-x2)i + (y1-y2)j + (z1-z2)k
and that for BC it is : (x2-x3)i + (y2-y3)j + (z2-z3)k
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;
}