#!/usr/bin/env python

#    Copyright (C) 2004 Paul Harrison
#    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 gtk, random, math, sys, os

if len(sys.argv) == 1:
   os.system(sys.executable+' '+sys.argv[0]+' 2.0 0.1 &')
   os.system(sys.executable+' '+sys.argv[0]+' 2.0 0.4 &')
   os.system(sys.executable+' '+sys.argv[0]+' 3.0 0.4 &')
   sys.exit(0)

sizex = 60
sizey = 20
edge = 5
x = sizex/2
y = sizey/2

power = float(sys.argv[1])
minimum = float(sys.argv[2])


def generate(power, minimum):
    level = random.random()
    
    return ((1.0-level) ** (1.0/(-power+1.0))) * minimum

def jitter():
    global x, y
    amount = generate(power, minimum)
    
    for i in xrange(100):
        angle = random.random() * 2.0 * math.pi
        xx = x + math.cos(angle) * amount
        yy = y + math.sin(angle) * amount *sizey/sizex
        if yy >= 0 and yy < sizey and xx >= 0 and xx < sizex: break
        
        #Metropolis-Hastings mode:
        #return True

    x = max(0,min(sizex,xx))
    y = max(0,min(sizey,yy))

    if amount > 1.0:
        drawing.queue_draw()
        
    return True

def on_expose(widget, event):
    surface = widget.window
    black = widget.style.black_gc
    white = widget.style.white_gc
    
    surface.draw_rectangle(white, True, edge,edge, sizex+edge*3,sizey+edge*3)
    surface.draw_rectangle(black, False, edge,edge, sizex+edge*3,sizey+edge*3)
    surface.draw_rectangle(black, True, int(x)+edge+1,int(y)+edge+1, edge*3-2,edge*3-2)
    surface.draw_rectangle(white, True, edge*7+sizex,edge, sizex+edge*3,sizey+edge*3)
    surface.draw_rectangle(black, False, edge*7+sizex,edge, sizex+edge*3,sizey+edge*3)
    surface.draw_rectangle(black, True, edge*7+sizex+int(x)+1,int(y)+edge+1, edge*3-2,edge*3-2)
    
    surface.draw_rectangle(black, True, edge*11/2+sizex/2,sizey+edge*10, sizex, edge)

drawing = gtk.DrawingArea()
drawing.connect('expose-event', on_expose)
drawing.set_size_request(sizex*2+edge*11, sizey+edge*15)

window = gtk.Window()
window.set_resizable(False)
window.add(drawing)
window.set_title('%.2f, %.2f' % (power, minimum))
window.show_all()
window.connect('destroy', lambda widget: gtk.main_quit())
    
gtk.timeout_add(10, jitter)

gtk.main()
