Skip to content

Instantly share code, notes, and snippets.

@adusak
Last active August 29, 2015 14:22
Show Gist options
  • Save adusak/36578cda53f972eb28e7 to your computer and use it in GitHub Desktop.
Save adusak/36578cda53f972eb28e7 to your computer and use it in GitHub Desktop.
Multiple reduction copy machine
import copy
from math import sqrt
from python.common.polygon import Polygon
from python.common.polygon_group import PolygonGroup
from python.common.svg import Svg
from python.less9.transformations import *
def triangle(size, origin=False):
pol = Polygon([(0, 0), (1, 0), (0.5, sqrt(3) / 2), (0, 0)])
if origin:
pol.apply_transformation(translation(-0.5, -sqrt(3) / 4))
return pol.apply_transformation(scaling(size, size))
def square(size, origin=False):
sq = Polygon([(0, 1), (1, 1), (1, 0), (0, 0), (0, 1)])
if origin:
sq.apply_transformation(translation(-0.5, -0.5))
return sq.apply_transformation(scaling(size, size))
def sierpinsky_triangle(size=1000):
transformations = [scaling(0.5, 0.5),
combine(scaling(0.5, 0.5), translation(size / 2, (size) * (sqrt(3) / 2))),
combine(scaling(0.5, 0.5), translation(size, 0))]
mrcm(triangle(size, True), transformations, 7, name="sierpinsky")
def serp_relatives(transformations, size=1000, name="relatives"):
start_polygon = square(size, True)
scale = scaling(0.5, 0.5)
moves = [translation(-size / 2, size / 2), translation(size / 2, size / 2), translation(size / 2, -size / 2)]
trans = list(map(lambda x: combine(scale, x[0], x[1]), zip(moves, transformations)))
mrcm(start_polygon, trans, 7, name)
def merge_groups(groups):
top_group = PolygonGroup()
for group in groups:
for polygon in group.polygons:
top_group.polygons.append(polygon)
return top_group
def mrcm(polygon, transformations, iterations, name="multiple_reduction"):
group = PolygonGroup([polygon])
for iteration in range(iterations):
groups = []
for transformation in transformations:
copy_group = copy.deepcopy(group)
copy_group.apply_transformation(transformation)
groups.append(copy_group)
group = merge_groups(groups)
svg = Svg()
group.draw_to_svg(svg)
svg.save("output/" + name)
return group
def tree(size, r, angle, name="tree"):
scale = scaling(r, r)
r1 = rotation(angle)
r2 = rotation(-angle)
transformations = [combine(scale, r1, translation(0, size / 2)),
combine(scale, r2, translation(0, size / 2)),
np.identity(3)]
mrcm(Polygon([(0, -0.5), (0, 0.5)]).apply_transformation(scaling(size, size)), transformations, 8, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment