From: Alex T. <al...@tw...> - 2005-09-27 23:07:34
|
Andrew P wrote: > Hello, > > I have a program that spits out an array like: > > [[1 1 1 1 0] > [1 1 1 0 0] > [0 1 0 0 0] > [0 0 1 0 1] > [1 1 0 0 0]] > > Does anybody have a suggestion about the quickest way to draw this? > > I'm really very new at this. I'm just trying to avoid looping over > each element in the array to create a new array with RGB values, since > I want to have this animate as it draws, and scroll. This is for > cellular automata, in case anybody cares :) > How quickly do you need to draw it ? (And is 100x100 a realistic size limit, or do you need bigger ?) I tried a very simple loop as you described (see code below) and it took around 0.07 seconds to draw it on my 2-3 year old laptop (actually, to erase the previous set and draw a new set of points). You could probably optimize this significantly by drawing only those points which have changed rather than drawing all the previous set (to erase them) and then all the new set. But I'd recommend not optimizing it until (or unless) you have proven that you need to - try it the simple way first, and see whether optimization is needed or not. > I saw the drawPointList method, but I don't see a way to get from a > 2D array to a what looks like a list of x,y pairs? Is that what it > wants? Something like [[0,1],[0,2],[0,4]] instead of [0,1,1,0,1]? > Either way, that doesn't seem like a very direct method coming from an > array, but I could be way off here. I don't see a way to quickly get > to that point tho. > You probably don't want points - I think this would likely be too small so each individual point would be barely visible. So this sample code uses rectangles (I set the size to be 4 pixels). This is only the relevant snippet - but hopefully the context is clear enough (but let me know if you'd like the whole example and I'll send it to you directly - but note it's been adapted from another similar experiment I did, so has quite a bit of cruft in it ...) (first generate random points, then erase prior set, then draw new set)) > start = time.clock() > l2d = [] > for i in range(K): > line = [] > for j in range(K): > line.append(random.randint(0,1)) > l2d.append(line) > print "generate", K, "took", time.clock()- start > > start = time.clock() > pens = [] > self.components.bmpCanvas.freeze() > self.components.bmpCanvas.drawRectangleList(self.rects, > self.whitepens) > > self.rects = [] > pens = [] > self.whitepens = [] > siz = 4 > for i in range(K): > for j in range(K): > if l2d[i][j]: > self.rects.append( (siz*i, siz*j, siz, siz) ) > pens.append(self.basicpen) > self.whitepens.append(self.whitepen) > self.components.bmpCanvas.drawRectangleList(self.rects, pens) > self.components.bmpCanvas.thaw() > next = time.clock() > > print "Took ", next-start -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.6/111 - Release Date: 23/09/2005 |