

Task
Produce a graphical or ASCII-art representation of a Sierpinski square curve of at least order 3.

F sierpinski_square(fname, size, length, order)
   V x = (size - length) / 2
   V y = Float(length)
   V angle = 0.0
 
   V outfile = File(fname, ‘w’)
   outfile.write(‘<svg xmlns='http://www.w3.org/2000/svg' width='’size‘' height='’size"'>\n")
   outfile.write("<rect width='100%' height='100%' fill='white'/>\n")
   outfile.write(‘<path stroke-width='1' stroke='black' fill='none' d='’)
   V s = ‘F+XF+F+XF’
   L 0 .< order
      s = s.replace(‘X’, ‘XF-F+F-XF+F+XF-F+F-X’)
 
   outfile.write(‘M’x‘,’y)
   L(c) s
      S c
         ‘F’
            x += length * cos(radians(angle))
            y += length * sin(radians(angle))
            outfile.write(‘ L’x‘,’y)
         ‘+’
            angle = (angle + 90) % 360
         ‘-’
            angle = (angle - 90 + 360) % 360
 
   outfile.write("'/>\n</svg>\n")
 
sierpinski_square(‘sierpinski_square.svg’, 635, 5, 5)