Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and

Saturday, 27 August 2011

Experiments with numpy and pyprocessing (a revised voronoi)

This sketch does not appear to benefit from the use of numpy, but it is on my path to learning how to use numpy in more intensive pixel manipulation, (currently other experiments on GrayScott diffusion are not going too well. I'm failing to dynamically update pixels, which is frustrating because otherwise the code seems good using pyunit from eric5). I thought about trying but I don't think I can use numpy there, possibly directly using jython, but that all that seems diversionary?

Copyright (c) 2011 Martin Prout
This example is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
NB: use of numpy does not seem to speed things up, however is probaby more sparing
of system resources (memory in particular) example

from pyprocessing import *
from random import randint
import numpy as np

def setup():
    pyprocessing setup
    size(800,  600)
    n = randint(200, 300) # number of cells
    nx = np.random.randint(width, size=(n))      # numpy does randint(low, high) but high is excluded
    ny = np.random.randint(height, size= (n))   # if high = None, like here low defaults to randint(0, low)
    nh = np.random.randint(256, size=(3,  n))

    for y in xrange(height):
        for x in xrange(width):
            # find the closest cell center
            dmin = hypot_squared(width - 1, height - 1)          # more intelligible version
            #dmin = ((width - 1) * (width - 1)) + ((height - 1) * (height - 1)) # optimized version?
            j = -1
            for i in xrange(n):
                d = hypot_squared((nx[i] - x),  (ny[i] - y))           # more intelligible version
                #d = ((nx[i] - x) * (nx[i] - x)) + ((ny[i] - y) * (ny[i] - y)) # optimized version?
                if d < dmin:
                    dmin = d
                    j = i
            setScreen(x, y, color(nh[0][j], nh[1][j], nh[2][j])) 

def hypot_squared(a, b):
    Must be cheaper to calculate than hypot
    return (a * a) + (b * b)            


No comments:

Post a Comment


Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby- and processing-3.2.2