#!/usr/bin/python

import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()

def get_int_param(name, default):
    val = form.getvalue(name)
    if (val is None):
        return default
    return int(val)


def draw_circle(cx, cy, r):
    return '<circle cx="%f" cy="%f" r="%f" style="fill: none; stroke: #000000; " stroke-width="%d" />' % (cx, cy, r, 1)


def draw_circle_fractal(cx, cy, r, depth):
    result = draw_circle(cx, cy, r);
    if (depth > 0):
        r2 = r * 0.464101615
        r3 = r2 * 0.5773502690
        result = result + draw_circle_fractal(cx, cy - r + r2, r2, depth - 1)
        result = result + draw_circle_fractal(cx - r2, cy + r3, r2, depth - 1)
        result = result + draw_circle_fractal(cx + r2, cy + r3, r2, depth - 1)
    return result

print """Content-type: image/svg+xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   viewBox="0 0 600 600"
   version="1.1"
   height="600"
   width="600">
  <g>%s</g>
</svg>
""" % draw_circle_fractal(300, 300, 300, get_int_param("depth", 4));
