Archive

Archive for December, 2019

angle between two lines

December 10, 2019 Leave a comment

Problem
You have two lines. What is the angle between them?

Or, you have 3 points, say A, B and C. If you go from A to B (vector 1), then from B to C (vector 2), then what is the angle at point B between the two vectors?

Solution
There is a nice blog post about it here: Find the Angle between three points from 2D using python.

Here is a Python code that is based on the one that you can find in the aforementioned blog post:

import math
from typing import NamedTuple

class Point(NamedTuple):
    x: int
    y: int

def angle(a: Point, b: Point, c: Point) -> float:
    ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
    return ang + 360 if ang < 0 else ang
Categories: python Tags: , , , , ,