Don't wanna be here? Send us removal request.
Text
Angle between three points
Once I had to write some code (C++) related to computational geometry involving triangles and meshes (the goal was a Delaunay triangulation). One of the first codes was to find the angle given three points.
The main idea was: read three points (six pair of coordinates) and calculate the internal angle of this triangle formed by this three points.
So, the question is: how to find the angle between two vectors? A little math is needed here.. specifically in the dot product, which can be written as:
,
where θ is the angle formed by the vectors |A| and |B| their magnitude:
Now we can find θ:
To find α, β and θ of the first Figure, we have to calculate the magnitude of the three vectors:
Finally, the angles:
Remember: these angles are in radian. To pass to degree we have to multiply by 180 and divide by π.
Here is the C/C++ code:
#include <stdio.h> #include <math.h> double getMagnitude(int x1, int x2, int y1, int y2) { double aux = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); return sqrt(aux); } double getAngle(int x1, int x2, int x3, int y1, int y2, int y3, double V1, double V2) { double aux = ((x2-x1)*(x3-x1) + (y2-y1)*(y3-y1))/(V1*V2); return acos(aux); } double rad2deg(double angle) { return angle*180/3.1415; } int main(int argc, char *argv[]) { // Coordinates int x1=10, y1=10; int x2=50, y2=10; int x3=10, y3=40; // STEP 1: Magnitude of vectors A, B and C double A = getMagnitude(x2,x3,y2,y3); double B = getMagnitude(x3,x1,y3,y1); double C = getMagnitude(x1,x2,y1,y2); // STEP 2: Angles alpha(C-B) beta(A-B) theta(C-A) double alpha = getAngle(x1,x2,x3,y1,y2,y3,C,B); double beta = getAngle(x3,x2,x1,y3,y2,y1,A,B); double theta = getAngle(x2,x1,x3,y2,y1,y3,C,A); // STEP 3*: Radian to degree alpha = rad2deg(alpha); beta = rad2deg(beta); theta = rad2deg(theta); printf("alpha: %lf\nbeta: %lf\ntheta: %lf\n",alpha,beta,theta); }
You should get this values:
0 notes