# Draw simplicial arrangement R(2n), for given command line argument n

import sys
from math import pi,exp,cos,sin

nsides = int(sys.argv[-1])  # This is what passes for a user interface
rotn = cos(2*pi/nsides)+1j*sin(2*pi/nsides)

pixels = 400    # dimensions of bounding box of image

vertical = [1j,-1j]
if nsides & 1:
    offset = 1j/nsides
else:
    offset = 2.5j/nsides
hscale = abs(1+offset)
horizontal = [(-1+offset)/hscale,(1+offset)/hscale]

print '''<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'''

print '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" ' \
		   'width="%dpt" height="%dpt" viewBox="0 0 %d %d">' \
			% (pixels, pixels, pixels, pixels)

print '  <g fill="none" stroke="black">'

def pixelate(point):
    p2 = pixels/2.
    point *= p2
    point += (p2 + 1j*p2)
    return point.real,point.imag

def printrots(line,ntimes):
    p,q = line
    for i in range(ntimes):
        print '    <line x1="%d" y1="%d" x2="%d" y2="%d"/>' % \
            (pixelate(p)+pixelate(q))
        p,q = p*rotn,q*rotn

printrots(horizontal,nsides)
if nsides & 1:
    printrots(vertical,nsides)
else:
    printrots(vertical,nsides/2)
    p,q = vertical
    halfrot = cos(pi/nsides)+1j*sin(pi/nsides)
    p,q = p*halfrot,q*halfrot
    printrots((p,q),nsides/2)

print '  </g>'
print '</svg>'
