# Julia Set with Numpy and py5

**URL:** <https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992>\
**Category:** Gallery\
**Created:** [July 1, 2021, 7:55am UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992 "2021-07-01T07:55:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [July 1, 2021, 7:55am UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/1 "2021-07-01T07:55:13Z")

</div>

I’ve been doing some more experimenting with [py5](https://discourse.processing.org/t/new-python-processing-project-py5/30837) (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:-

 ![julia](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a88689db437596d507df10b99f1e317e9a36d7ef.png)  
Possibly add a bit of color?

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [July 1, 2021, 4:15pm UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/2 "2021-07-01T16:15:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [July 2, 2021, 6:29am UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/3 "2021-07-02T06:29:11Z")

</div>

After a bit of experimentation I created a colourful version see my [github repo](https://github.com/monkstone/py5-examples/blob/main/module_mode/julia.py) trick was to initialize background in numpy, otherwise julia is surrounded with a blob of color.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [July 2, 2021, 1:53pm UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/4 "2021-07-02T13:53:52Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [July 3, 2021, 6:55am UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/5 "2021-07-03T06:55:45Z")

</div>

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](https://github.com/monkstone/pyprocessing-experiments/blob/master/misc/np_voronoi.py). Actually thanks to dorverbin on stack overflow this did not take long:-

```python
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()

```

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [July 9, 2021, 3:11pm UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/6 "2021-07-09T15:11:33Z")

</div>

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](https://monkstone.github.io/py5-examples/2021/07/10/newton.html). All suggestions welcome @solub, @tabreturn, @villares.

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [July 9, 2021, 5:41pm UTC](https://discourse.processing.org/t/julia-set-with-numpy-and-py5/30992/7 "2021-07-09T17:41:48Z")

</div>

> [@monkstone](#):
>
> ```auto
> voronoi = (one_hot_indices * colours[np.newaxis, np.newaxis, :, :]).sum(axis=2)
> py5.set_np_pixels(voronoi, bands='RGB')
> 
> ```

Oh! I’m sorry, I’m not at all familiar with this _numpy_ magic 🙂 I hope I can learn from your examples 😃
