Skip to content

Instantly share code, notes, and snippets.

@CatherineH
Last active December 25, 2020 17:24
Show Gist options
  • Save CatherineH/620d78bb4653c0e864d09a71085276b3 to your computer and use it in GitHub Desktop.
Save CatherineH/620d78bb4653c0e864d09a71085276b3 to your computer and use it in GitHub Desktop.
two ways of generating a trapezoid tiling
import svgwrite
from svgwrite.path import Path
from svgwrite.shapes import Polygon, Polyline
from svgwrite.container import Group
from svgwrite.mixins import Transform
from math import sin, cos, pi, sqrt
width = 50
angle = 60*pi/360.0
margin = 10
scale_ratio = (width-margin)/width
dwg = svgwrite.Drawing('trapezoid.svg', profile='tiny')
def generate_group():
repeating_group = Group(fill="none")
polygon_points = [[0, 0], [width*cos(angle), width*sin(angle)], [width*cos(angle), width*(sin(angle)+1)], [0, width], [0, 0]]
polygon = Polyline(points=polygon_points, stroke=svgwrite.rgb(10, 10, 16, '%'))
polygon.translate(scale_ratio*margin*sin(angle), scale_ratio*margin*cos(angle))
polygon.scale(scale_ratio)
polygon_larger = Polyline(points=polygon_points, stroke=svgwrite.rgb(10, 10, 16, '%'))
group = Group(fill=svgwrite.rgb(50, 50, 0, '%'), fill_opacity=0.5)
group.add(polygon)
group.translate(0, width*(1+2*sin(angle)))
group.rotate(-60)
group2 = Group(fill=svgwrite.rgb(50, 0, 0, '%'))
group2.add(polygon_larger)
group2.add(polygon)
group2.translate(width*cos(angle), width*(1+sin(angle)))
group2.rotate(-120)
group3 = Group(fill=svgwrite.rgb(0, 50, 0, '%'))
group3.add(polygon)
group3.translate(-width*cos(angle), width*(1+sin(angle)))
group3.rotate(-60)
group4 = Group(fill=svgwrite.rgb(0, 0, 50, '%'))
group4.add(polygon_larger)
group4.add(polygon)
repeating_group.add(group4)
repeating_group.add(group)
repeating_group.add(group2)
repeating_group.add(group3)
return repeating_group
for i in range(10):
for j in range(5):
r_group = generate_group()
r_group.translate(i*2*width*cos(angle), j*2*width)
dwg.add(r_group)
dwg.save(pretty=True)
corner = margin*cos(angle/2)
dwg = svgwrite.Drawing('trapezoid_path.svg', profile='tiny')
path = Path(stroke=svgwrite.rgb(10, 10, 16, '%'), fill="none")
for j in range(7):
for i in range(7):
path.push(f"m {margin} 0")
path.push(f"l {width*cos(angle)} {width*sin(angle)}")
path.push(f"l 0 {width}")
path.push(f"l {-width*cos(angle)} {-width*sin(angle)}")
path.push(f"l 0 {-width}")
path.push(f"m {width*cos(angle)} {width*sin(angle)}")
path.push(f"m {margin} 0")
path.push(f"l {width*cos(angle)} {-width*sin(angle)}")
path.push(f"l 0 {width}")
path.push(f"l {-width*cos(angle)} {width*sin(angle)}")
path.push(f"l 0 {-width}")
path.push(f"m 0 {width}")
path.push(f"m {margin*sin(angle)} {margin*cos(angle)}")
path.push(f"l {width*cos(angle)} {width*sin(angle)}")
path.push(f"l {width*cos(angle)} {-width*sin(angle)}")
path.push(f"l {-width*cos(angle)} {-width*sin(angle)}")
path.push(f"l {-width*cos(angle)} {width*sin(angle)}")
path.push(f"m {-margin*cos(angle)} {margin*sin(angle)}")
path.push(f"l {width*cos(angle)} {width*sin(angle)}")
path.push(f"l {-width*cos(angle)} {width*sin(angle)}")
path.push(f"l {-width*cos(angle)} {-width*sin(angle)}")
path.push(f"l {width*cos(angle)} {-width*sin(angle)}")
path.push(f"m {margin*cos(angle)} {-margin*sin(angle)}")
path.push(f"m {width*cos(angle)} {-width*sin(angle)}")
path.push(f"m {-margin*sin(angle)} {-margin*cos(angle)-width}")
path.push(f"M 0 {j*(2*width+2*margin)}")
dwg.add(path)
dwg.save(pretty=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment