use context essentials2021 # CMPU 101, Section 52 # 27 February 2023 # How small a shape can get (in pixels) before we stop drawing smaller ones CUTOFF = 6 # # Sierpiński Triangles # # Termination: Dividing the size s by two makes it smaller until it's at or below CUTOFF fun s-tri(size :: Number) -> Image: doc: "Produce a Sierpiński triangle of the given size by generating one for s/2 and placing one copy above two copies" if size <= CUTOFF: triangle(size, "outline", "red") else: sub = s-tri(size / 2) above(sub, beside(sub, sub)) end end s-tri(1000) # # Sierpiński Carpets # # Termination: Dividing the size s by three makes it smaller until it's at or below CUTOFF fun s-carpet(size :: Number) -> Image: doc: "Draw a Sierpiński carpet of size s-by-s by generating an s/3 carpet and positioning it on every side of an empty s/3 square" if size <= CUTOFF: square(size, "outline", "red") else: sub = s-carpet(size / 3) blk = square(size / 3, "solid", "white") above3( beside3(sub, sub, sub), beside3(sub, blk, sub), beside3(sub, sub, sub)) end end fun above3(i1 :: Image, i2 :: Image, i3 :: Image) -> Image: doc: "Draw three images above one another" above(i1, above(i2, i3)) end fun beside3(i1 :: Image, i2 :: Image, i3 :: Image) -> Image: doc: "Draw three images beside one another" beside(i1, beside(i2, i3)) end s-carpet(400)