#!/usr/bin/env python
#    Copyright (C) 2005, Maxime Biais <maxime@biais.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os

class Scene:
    def __init__(self,name="svg", height=1200, width=1600):
        self.name = name
        self.items = []
        self.height = height
        self.width = width

    def add(self,item):
        self.items.append(item)

    def strarray(self):
        var = ["<?xml version=\"1.0\"?>\n",
               "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"%d\" width=\"%d\" >\n" % (self.height,self.width),
               " <g style=\"fill-opacity:1.0; stroke:black;\n",
               "  stroke-width:1;\">\n"]
        for item in self.items:
            var += str(item)
        var += [" </g>\n</svg>\n"]
        return var

    def write_svg(self,filename=None):
        if filename:
            self.svgname = filename
        else:
            self.svgname = self.name + ".svg"
        file = open(self.svgname,'w')
        file.writelines(self.strarray())
        file.close()

class Arc:
    def __init__(self, radius = 50, x1 = 1,  y1 = 1, x2 = 1, y2 = 1, w = 10,
                 orient = 0, alpha = 0.1):
        self.radius = radius
        self.x1 = x1
        self.x2 = x2
        self.y1 = y1
        self.y2 = y2
        self.w = w
        self.orient = orient
        self.alpha = alpha

    def __str__(self):
        return """
<path
   d = "M %d %d A %d,%d 0 0 %d %d,%d"
   style = "stroke:#5f79f4; fill:none; stroke-width:%d; stroke-opacity:%f"
/>
""" % (self.x1, self.y1, self.radius, self.radius, self.orient, self.x2, self.y2, self.w, self.alpha)

def randarcs(n):
    import random
    y = 1000
    res = []
    i = 0
    mx1 = mx2 = 1500
    mw = 50
    while i < n:
        x1 = int(random.random() * mx1) + 50
        x2 = int(random.random() * mx2) + 50
        w = int(random.random() * mw) + 2
        alpha = random.random() / 2 + 0.0
        if x2 - x1 < 20:
            continue
        i += 1
        print x1, x2, w
        res.append(Arc(1, x1, y, x2, y, w, 1, alpha))
    return res

def random_arcs():
    scene = Scene('test')
    for i in randarcs(40):
        scene.add(i)
    scene.write_svg()

if __name__ == '__main__':
    random_arcs()
