Julia Set with Numpy and py5

I’ve been doing some more experimenting with py5 (and remember I’m a rubyist at heart) so I was very pleased when I was able to create a julia set with py5 and numpy. @solub, @villares and @tabreturn will certainly do better:-


Possibly add a bit of color?

6 Likes

super nice! I wouldn’t ever do as good!

After a bit of experimentation I created a colourful version see my github repo trick was to initialize background in numpy, otherwise julia is surrounded with a blob of color.

1 Like

Lovely !

One thing I would like to try is testing animated sketches where most calculations can be vectorized and compare with their Python mode versions with regular for loops to measure the differences in speed. I’m thinking about algorithms with:

  • extensive grid operations (ex: Game of Life, texture synthesis)
  • numerous distance calculations (ex: Metaballs are almost impossible to animate in Python mode)
  • numerous force calculations (ex: physics simulations, differential growth)
2 Likes

Yeah that would be interesting to me, but I have to go in baby steps my python’s so rusty next plan is to reimplement my voronoi sketch from pyprocessing. Actually thanks to dorverbin on stack overflow this did not take long:-

import py5
import numpy as np
# sketch inspired by dorverbin
# https://stackoverflow.com/questions/53696900/render-voronoi-diagram-to-numpy-array
# and yes one-hot coding is a thing
def settings():
    py5.size(400,  300)

def setup():
    sketch_title('Fast Voronoi')
    n = py5.random_int(30, 50)
    cx = np.random.randint(py5.width, size=(n))
    cy = np.random.randint(py5.height, size=(n))
    X, Y = np.meshgrid(np.arange(py5.width), np.arange(py5.height))
    squared_dist = (X[:, :, np.newaxis] - cx[np.newaxis, np.newaxis, :]) ** 2 + \
                   (Y[:, :, np.newaxis] - cy[np.newaxis, np.newaxis, :]) ** 2
    indices = np.argmin(squared_dist, axis=2)
    # Convert the previous 2D array to a 3D array where the extra dimension is a one-hot
    # encoding of the index
    one_hot_indices = indices[:, :, np.newaxis, np.newaxis] == np.arange(cx.size)[np.newaxis, np.newaxis, :, np.newaxis]
    # Create a random color for each center
    colours = np.random.randint(255, size=(n, 3), dtype=np.uint8)
    voronoi = (one_hot_indices * colours[np.newaxis, np.newaxis, :, :]).sum(axis=2)
    py5.set_np_pixels(voronoi, bands='RGB')

def sketch_title(title):
    py5.get_surface().set_title(title)

py5.run_sketch()
1 Like

I am making progress with numpy and py5, but I’m struggling a bit get decent display output with a Newton Fractal in processing, whereas its a cinch with matplotlib see blog entry Newton Fractal with Numpy. All suggestions welcome @solub, @tabreturn, @villares.

1 Like

Oh! I’m sorry, I’m not at all familiar with this numpy magic :slight_smile: I hope I can learn from your examples :smiley: