Python calculates the angle between two vectors

Simple small code, may be used in the project, record:

from math import sqrt, pow

def angle_of_vector(v1, v2):
    pi = 3.1415
    vector_prod = v1[0] * v2[0] + v1[1] * v2[1]
    length_prod = sqrt(pow(v1[0], 2) + pow(v1[1], 2)) * sqrt(pow(v2[0], 2) + pow(v2[1], 2))
    cos = vector_prod * 1.0 / (length_prod * 1.0 + 1e-6)
    return (acos(cos) / pi) * 180


if __name__ == '__main__':
    a = [1, 0]
    b = [5, 8]
    print(angle_of_vector(a, b))

The output is the angle system, and the result of the radian system can be directly output acos(cos). The accuracy can be adjusted by yourself.

Guess you like

Origin blog.csdn.net/leviopku/article/details/107248591