Calculating Internal Angles of Points Along Edges in a Polygon in Python

98 views Asked by At

I have a polygon that is defined as a list of its vertices in a clockwise direction. For a given point along one of the polygon's edges, I want to calculate the two degrees between which the 180 degrees of that point's interior angle lies.

For instance, if I have a polygon that is a rectangle with vertices: [(2, 0), (0, 0), (0, 2), (2, 2)], then I can expect (1, 0) to have between 0 and 180 degrees in an anticlockwise direction. (0, 1) would have between 90 and 270 degrees in a clockwise direction. Here is my code thus far; the function can identify the angle between but I have no way of knowing whether the interior should be clockwise or anticlockwise. I want to be able to tell whether to go clockwise or anticlockwise from d1 to d2 to get the internal angle.

def calculate_angle(self, point, vertice1, vertice2): # point between vertice1/vertice2

    x, y = point
    x1, y1 = v1
    x2, y2 = v2

    a = [(x - x1), (y - y1)]
    b = [(x - x2), (y - y2)]

    d1 = (math.degrees(atan2(a[1], a[0])) + 360) % 360
    d2 = (math.degrees(atan2(b[1], b[0])) + 360) % 360
                 
    d1, d2 = min([d1, d2]), max([d1, d2])
0

There are 0 answers