Just a simple little demo I made instead of doing my math Razz

Code:

#!/usr/bin/python2
import math, colorsys, time, gc, array
from graphics import *

def mapto(x, in_min, in_max, out_min, out_max):
    return float((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)

gc.enable()
pal = "Rasta"
scr = GraphWin("Plasma",75,75, autoflush=False)
#let's make a nice palette
palette = []
if pal == "Rainbow":
    for h in range(256):
        r,g,b = colorsys.hsv_to_rgb(mapto(h,0.0,255.0,0.0,1.0), 1.0, 1.0)
        palette.append("#%02x%02x%02x" % (r*255,g*255,b*255))
if pal == "Rasta":
    for i in range(256):
        r = int(128.0 + 128 * math.sin(3.1415 * i / 16.0))
        g = int(128.0 + 128 * math.sin(3.1415 * i / 128.0))
        palette.append("#%02x%02x00" % (r,g))
print palette[0]
plasma = [[]]
for x in range(75):
    plasma.append([])
    for y in range(75):
        color = int(128.0 + (128.0 * math.sin(x / 16.0)) + 128.0 + (128.0 * math.sin(y / 8.0)) + 128.0 + (128.0 * math.sin((x+y)) / 16.0) + 128.0 + (128.0 * math.sin(math.sqrt(x * x + y * y) / 8.0))) / 4
        plasma[x].append(color)

for mod in range(100):
    for x in range(75):
        for y in range(75):
            scr.plotPixel(x,y,palette[int((plasma[x][y] + mod) % 256)])
    scr.update()


The code gets progressively slower as it runs. Does anyone have any idea why?
As it runs meaning as it plots successive pixels and/or successive frames in that final triple-nested loop? I don't see anything obvious there, so I am going to assume that there's some overhead in scr that you're not accounting for. By the time it gets there, all of the necessary arrays are allocated and populated, correct?
KermMartian wrote:
As it runs meaning as it plots successive pixels and/or successive frames in that final triple-nested loop? I don't see anything obvious there, so I am going to assume that there's some overhead in scr that you're not accounting for. By the time it gets there, all of the necessary arrays are allocated and populated, correct?

They should be, yes. I feared that the graphics.py was to blame, but it's by far the easiest graphics lib to use. I might try rewriting it using pygame. Should I plot the plasma pixel by pixel or should I make a buffer of images? Which would be faster?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement