import math from polygon import * from matrix import * def polygon(edges, center, radius, phase=0, M=((1,0),(0,1)), color=(0,0,0)): """ Return a regular polygon. edges (int) -- number of sides center (Vector) -- position of center radius (float) -- radius to vertex phase (float) -- rotates polygon 'phase' vertics M (Matrix) -- distortion matrix """ poly = Polygon(color=color) for n in range(edges): x = radius * math.sin((n + phase) * math.tau / edges) y = radius * math.cos((n + phase) * math.tau / edges) poly.append(Vector(*center) + Matrix(*M).prod(Vector(x, y))) return poly def star(edges, center, radius, ratio=0.5, phase=0, M=Matrix((1,0),(0,1)), color=(0,0,0)): """ Return a regular star polygon. edges (int) -- number of convex vertices center (Vector) -- position of center radius (float) -- radius to vertex ratio (float) -- radius ratio to inner vertices phase (float) -- rotates polygon 'phase' vertics M (Matrix) -- distortion matrix """ poly = Polygon(color=color) for n in range(edges): x = radius * math.sin((n + phase) * math.tau / edges) y = radius * math.cos((n + phase) * math.tau / edges) poly.append(Vector(*center) + M.prod(Vector(x, y))) x = ratio * radius * math.sin((n + 0.5 + phase) * math.tau / edges) y = ratio * radius * math.cos((n + 0.5 + phase) * math.tau / edges) poly.append(Vector(*center) + M.prod(Vector(x, y))) return poly def border(polygon): """ Turn a polygon into a border. (split it into its sides) """ bor = [] for i, _ in enumerate(polygon): bor.append(Polygon(polygon[i-1], polygon[i])) return bor def door(start, end, part): """ Turn a wall into a door. (make a hole in the middle with relative size 'part') """ return (Polygon(start, start+(1-part)/2*(end-start)), Polygon(end+(1-part)/2*(start-end), end))