Finding the angle between two lines

Aryan Arabshahi
December 28, 2021
Share:

To calculate the angle between two lines, the gradients of the lines are needed. To get the gradient of the line, we need two different points and as you know, every two points in space can represent a line.

So, to get the angle of the line with the horizontal axis, you can use the sample below:

from math import degrees, atan


def calc_angle(m1, m2) -> int:
    return round(degrees(atan((m1 - m2) / (1 + (m1 * m2)))))


def get_gradient(p1, p2):
    return(p1[1] - p2[1]) / (p1[0] - p2[0])


def main():
    p1 = (0, 10)
    p2 = (10, 0)
    lines_angle = calc_angle(
        m1=get_gradient(p1=p1, p2=p2),
        m2=0
    )

    print(lines_angle)


if __name__ == '__main__':
    main()

The gradient of each horizontal line is zero so we can consider zero as the gradient of the second line.