简体   繁体   中英

how to calculate the angle of a line form by two points from the north direction?

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)?

Two points (x1,y1) and (x2,y2)
In javascript

function calcAngleDegrees(x1, y1,x2,y2) {
let x = x2 - x1 ;
let y = y2 - y1 ;
  return Math.atan2(y, x) * 180 / Math.PI;
}
let angleDegree = calcAngleDegrees(0,0,5,-5); // -45
let bearingFromAngle = (450 - angleDegree ) % 360; // 135
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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM