from numpy import *
import pysmm_graphlib as gl
import numpy.random
import framework
from math import pi

n = 50
x = numpy.random.random(n)
y = numpy.random.random(n)
(ll, hh, dist) = gl.PYSMMGL_Emst(x,y)
triangles = gl.PYSMMGL_Delaunay(x,y)


class Plot(framework.Screen):
    def calc(self):
        x = numpy.random.random(n)
        y = numpy.random.random(n)
        (ll, hh, dist) = gl.PYSMMGL_Emst(x,y)
        triangles = gl.PYSMMGL_Delaunay(x,y)
        (w,h) = self.window.get_size()
        self.queue_draw_area(0,0,w,h)

    def draw(self, cr, width, height):
		# draw the background
		cr.set_source_rgb(1,1,1)
		cr.rectangle(0,0,width,height)
		cr.fill()

		# draw points
		cr.set_source_rgb(0,0,0)
		for i in xrange(len(x)):
			cr.arc(x[i]*width,y[i]*height,2,0,2*pi)
			cr.fill()

		# draw the triangulation
		cr.set_source_rgb(0,0,0.5)
		cr.set_line_width(0.3)
		for t in triangles:
			cr.move_to(x[t[0]] *width,y[t[0]]*height)
			cr.line_to(x[t[1]] *width,y[t[1]]*height)
			cr.line_to(x[t[2]] *width,y[t[2]]*height)
			cr.line_to(x[t[0]] *width,y[t[0]]*height)
			cr.stroke()
		cr.set_source_rgb(1,0,0)
		cr.set_line_width(2)
		for i in xrange(len(ll)):
			cr.move_to(x[ll[i]]*width, y[ll[i]]*height)
			cr.line_to(x[hh[i]]*width, y[hh[i]]*height)
			cr.stroke()

framework.run(Plot)
		

