Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# villares/rotated_rect.pyde

Last active Feb 27, 2022
Rotated Rectangle example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 from random import choice add_library('geomerative') def setup(): size(500, 500) RG.init(this) noLoop() def draw(): background(200) gp = list(range(50, width, 50)) polys = [rg_bar(choice(gp), choice(gp), choice(gp), choice(gp), 10) for _ in range(50)] union_poly = polys.pop() for p in polys: union_poly = union_poly.union(p) RG.shape(union_poly.toShape()) def rg_bar(p1x, p1y, p2x, p2y, w1, w2=None): """ trapezoid, draws a rotated quad with axis starting at (p1x, p1y) ending at (p2x, p2y) where w1 and w2 are the starting and ending side widths. """ if w2 is None: w2 = w1 d = dist(p1x, p1y, p2x, p2y) angle = atan2(p1x - p2x, p2y - p1y) + HALF_PI unrotated_points = ( (p1x, p1y - w1 / 2), (p1x, p1y + w1 / 2), (p1x + d, p1y + w2 / 2), (p1x +d, p1y - w2 / 2) ) points = [rot(pt, angle, center=(p1x, p1y)) for pt in unrotated_points] rg_points = [RPoint(x, y) for x, y in points] #rs = RShape([rg_points]) rs = RPolygon(rg_points) return rs def rot(pt, angle, center=None): xp, yp = pt x0, y0 = center or (0, 0) x, y = xp - x0, yp - y0 # translate to origin xr = x * cos(angle) - y * sin(angle) yr = y * cos(angle) + x * sin(angle) return (xr + x0, yr + y0)
to join this conversation on GitHub. Already have an account? Sign in to comment