[plug] Interactive generation of polygons from segments
Cameron Patrick
cameron at patrick.wattle.id.au
Sun Jul 17 19:24:03 WST 2005
Cameron Patrick wrote:
> I'm also going to mention matplotlib http://matplotlib.sourceforge.net/
> which is a very handy plotting library for Python. I don't think it's
> directly useful for what you're after, but then again things like
> drawing a scatter graph might have quite a bit in common with drawing
> nice polygons, and it allows you at least a little bit of interactivity
> in terms to doing stuff to your plots when they're clicked on and so on.
15 minutes looking at docs and less than 50 lines of code got me most
of what Jim wanted. Source attached, do what you want with it.
Cameron
-------------- next part --------------
#! /usr/bin/env python
from pylab import *
import sys
if len(sys.argv) != 2:
print "usage: scatter.py input-file"
sys.exit(1)
def add_point(event):
if not event.inaxes:
return
click = array([event.xdata, event.ydata])
best = None
for p in points:
dist = sum(pow(p-click,2))
if best is None or dist < best[0]:
best = (dist, p[0], p[1])
print best
if best is None:
return
polyx.append(best[1])
polyy.append(best[2])
redraw()
def redraw():
cla()
scatter(x, y)
plot(polyx, polyy)
draw()
input_file = sys.argv[1]
lines = [line.strip().split() for line in file(input_file)]
points = array([[float(x) for x in y] for y in lines])
x = points[:, 0]
y = points[:, 1]
polyx = []
polyy = []
connect('button_press_event', add_point)
redraw()
show()
More information about the plug
mailing list