simple fractals with python and Tkinter

M

Matthew Wilson

Hi-


I've been goofing off with some basic recursive landscape generating
stuff, drawing the output to a tk window. You can run the program like
so:

from pts import *
Each tile is colored based on the heights of its neighbors. Anyway, maybe
somebody here would enjoy goofing off with this stuff like I have.

The program is available here:

http://www.sarcastic-horse.com/landscape/pts.py

It's unfinished, but it does draw crude pictures. Be careful about the
size of the numbers you feed it. 4 ** 7 took about 120 seconds to compute.

There's something weird happening with where you call it from. In IDLE, I
hit F5, then type draw(4) and the image pops up ok. Otherwise, nothing
happens.
 
M

Michael Peuser

Matthew Wilson said:

Hi Matthew,

Canvas is not very well suited for that kind of visualisation. I often use
the WCK extension (see below for the changes I made waiting for dinner...)

I encapsulated your drawing code into a routine tkDraw and wrote a
corresponding routine (a class in fact) wckDraw. To speed up things I
included the code for color computation directly. (To generate brushes costs
some time).

However a *lot* of time is spend inside pt.crect()!
Thus an impressive improvement was realized with:

import psyco
psyco.full()

Kindly
MichaelP

-------

#make a sorted list of ticks
ticklist = udict.keys()
ticklist.sort()

import Tkinter
root = Tkinter.Tk()
root.title('pts')
# eithet tkDraw or wckDraw
wckDraw(root, ptlist, canvaswidth,
canvaswidth,hmin,hmax,ticklist,numticks)
Tkinter.mainloop()

# old code
def tkDraw(root, ptlist,width,height,hmin,hmax,ticklist,numticks):

cc = Tkinter.Canvas(root, width=width, height=height, bg="white")
for pt in ptlist:
color = pt.calc_color(hmin, hmax)
rect = pt.crect(ticklist, numticks, width)
cc.create_rectangle(rect, fill=color, outline="")

cc.pack()

#new code
from WCK import Widget
class wckDraw(Widget):
def __init__(self, root,ptlist, width,
height,hmin,hmax,ticklist,numticks):

self.width=width
self.height=height
self.ptlist=ptlist
self.ui_init(root)
self.pack()
colors={}
for pt in ptlist:
if pt.h<=0:
col = int(pt.h/hmin*255) # rgb
else:
col = int(pt.h/hmax*255)*256*256+100*256+100 #rgb
if col not in colors:
colors[col] = self.ui_brush(col)
pt.brush = colors[col]
pt.rect = pt.crect(ticklist, numticks, width)
print "brushes:", len(colors)
def ui_handle_config(self):
print "resize"
return (self.width,self.height)

def ui_handle_clear(self, draw,x0, y0, x1, y1):
pass

def ui_handle_repair(self, draw, x0, y0, x1, y1):
for pt in self.ptlist:
draw.rectangle(pt.rect,pt.brush)
 
M

Matthew Wilson

Wow- thanks a lot for the improvements. This feedback is exactly what
I was hoping for when I posted the code. I'll study them today.


Matthew Wilson said:

Hi Matthew,

Canvas is not very well suited for that kind of visualisation. I often use
the WCK extension (see below for the changes I made waiting for dinner...)

I encapsulated your drawing code into a routine tkDraw and wrote a
corresponding routine (a class in fact) wckDraw. To speed up things I
included the code for color computation directly. (To generate brushes costs
some time).

However a *lot* of time is spend inside pt.crect()!
Thus an impressive improvement was realized with:

import psyco
psyco.full()

Kindly
MichaelP

-------

#make a sorted list of ticks
ticklist = udict.keys()
ticklist.sort()

import Tkinter
root = Tkinter.Tk()
root.title('pts')
# eithet tkDraw or wckDraw
wckDraw(root, ptlist, canvaswidth,
canvaswidth,hmin,hmax,ticklist,numticks)
Tkinter.mainloop()

# old code
def tkDraw(root, ptlist,width,height,hmin,hmax,ticklist,numticks):

cc = Tkinter.Canvas(root, width=width, height=height, bg="white")
for pt in ptlist:
color = pt.calc_color(hmin, hmax)
rect = pt.crect(ticklist, numticks, width)
cc.create_rectangle(rect, fill=color, outline="")

cc.pack()

#new code
from WCK import Widget
class wckDraw(Widget):
def __init__(self, root,ptlist, width,
height,hmin,hmax,ticklist,numticks):

self.width=width
self.height=height
self.ptlist=ptlist
self.ui_init(root)
self.pack()
colors={}
for pt in ptlist:
if pt.h<=0:
col = int(pt.h/hmin*255) # rgb
else:
col = int(pt.h/hmax*255)*256*256+100*256+100 #rgb
if col not in colors:
colors[col] = self.ui_brush(col)
pt.brush = colors[col]
pt.rect = pt.crect(ticklist, numticks, width)
print "brushes:", len(colors)
def ui_handle_config(self):
print "resize"
return (self.width,self.height)

def ui_handle_clear(self, draw,x0, y0, x1, y1):
pass

def ui_handle_repair(self, draw, x0, y0, x1, y1):
for pt in self.ptlist:
draw.rectangle(pt.rect,pt.brush)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top