Does anyone have a script/code/function to calculate the angle from the north line (bearing) of a line formed by two points (x1,y1) and (x2,y2)?
how to calculate the angle of a line form by two points from the north direction?
180 views Asked by Marco Contreras At
2
There are 2 answers
1
On
float calc_azimuth(float x1, float y1, float x2, float y2)
{
//calculate deltas
float deltax = x2 - x1;
float deltay = y2 - y1;
//chech if the deltas are zeros, and if they are, then use a small number
if (deltax == 0.0)
deltax = 0.000001;
if (deltay == 0.0)
deltay = 0.000001;
//Calculate angle
float angle = abs(atan(deltay / deltax) * (180/3.14159));
//define and initialize azimuth
float azim = 0;
//first quadrant
if (x2 >= x1 && y2 > y1)
azim = 90.0 - angle;
//second quadrant
else if (x2 > x1 && y2 <= y1)
azim = angle + 90.0;
//third quadrant
else if (x2 <= x1 && y2 < y1)
azim = 270.0 - angle;
//fourth quadrant
else
azim = 270.0 + angle;
return azim;
}
Two points (x1,y1) and (x2,y2)
In javascript