Koch
Based on the following example from Diagrams https://archives.haskell.org/projects.haskell.org/diagrams/gallery/Koch.html
In [1]:
Copied!
from PIL import Image as PILImage
from chalk import *
from chalk.transform import *
import chalk
from PIL import Image as PILImage from chalk import * from chalk.transform import * import chalk
In [2]:
Copied!
unit_x = Trail.hrule(1)
unit_x = Trail.hrule(1)
In [3]:
Copied!
def koch(n):
if n == 0:
return unit_x.scale_x(5)
else:
return (
koch(n - 1).scale(1 / 3)
+ koch(n - 1).scale(1 / 3).rotate_by(+1 / 6)
+ koch(n - 1).scale(1 / 3).rotate_by(-1 / 6)
+ koch(n - 1).scale(1 / 3)
)
def koch(n): if n == 0: return unit_x.scale_x(5) else: return ( koch(n - 1).scale(1 / 3) + koch(n - 1).scale(1 / 3).rotate_by(+1 / 6) + koch(n - 1).scale(1 / 3).rotate_by(-1 / 6) + koch(n - 1).scale(1 / 3) )
In [4]:
Copied!
d = vcat(koch(i).stroke().line_width(0.01) for i in range(1, 5))
d = vcat(koch(i).stroke().line_width(0.01) for i in range(1, 5))
In [5]:
Copied!
# Render
height = 512
d.render_svg("examples/output/koch.svg", height)
d.render("examples/output/koch.png", height)
PILImage.open("examples/output/koch.png")
# Render height = 512 d.render_svg("examples/output/koch.svg", height) d.render("examples/output/koch.png", height) PILImage.open("examples/output/koch.png")
Out[5]:
Last update: 2022-08-21