python azimuth calculation

Transfer from:

https://www.jb51.net/article/178694.htm

Use atan2 to calculate the azimuth, the range is -pi, pi

atan2 (y, x) means that the origin of the coordinate is the starting point, and the angle between the ray pointing to (x, y) on the coordinate plane and the positive direction of the x-axis.

A positive result indicates a counterclockwise rotation angle from the X axis, and a negative result indicates a clockwise rotation angle from the X axis.

Both atan and atan2 are inverse tangent functions, such as: there are two points point (x1, y1), and point (x2, y2);

Then the angle calculation methods of the slope formed by these two points are:


float angle = atan2( y2-y1, x2-x1 );

 

 

Mathematics commonly used operations on trigonometric functions:

1

2

3

4

5

6

import math

math.acos(x)  # 返回 x 的反余弦 弧度值。 

math.asin(x)  # 返回 x 的反正弦 弧度值。 

math.degrees(x)  # 将 弧度 转换为 角度, 如 degrees(math.pi/2) , 返回90.0 

math.radians(x)  # 将 角度 转换为 弧度

注意负数角度的转换。

 

 

 

 

Calculate the angle of two vectors

#Calculate the angle between two vectors, v1, v2 are the starting point coordinate and end point coordinate of the two vectors, the coordinate order is the first few columns (y) and then the
fewest rows (x) def angleBetween (v1, v2) :
    dx1 = v1 [2]-v1 [0]
    dy1 = v1 [3     ]     -v1 [1] dx2 = v2 [2] -v2
    [0]
dy2 = v2 [3]
-v2 [1] angle1 = math.atan2 (dy1, dx1)
    angle1 = int (angle1 * 180 / math.pi)
    # print (angle1)
    angle2 = math.atan2 (dy2,
    dx2 ) angle2 = int (angle2 * 180 / math.pi)
    # print (angle2)
    if angle1 * angle2> = 0:
        included_angle = abs (angle1-angle2)
    else:
        included_angle = abs (angle1) + abs (angle2)
        if included_angle> 180:
            included_angle = 360-included_angle
    return included_angle

发布了90 篇原创文章 · 获赞 13 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_32425195/article/details/105221610