There is a Newton Fractal sketch on Jorg Kantel blog that I converted to run with py5:-
import py5
import cmath
imgx = 512
imgy = 512
# Drawing area
# xa = 1.126
xa = -2.0
xb = 2.0
ya = -2.0
yb = 2.0
maxIt = 20 # max iterations allowed
h = 1e-6 # stepsize for numerical derivative
eps = 1e-3 # max error allowed
def f(z):
# return cmath.sin(z)
# return z*z*z*z*z*z - 1.0
return z*(z*z*z*z*z*z - 1.0)
def settings():
py5.size(imgx, imgy)
def setup():
global img
img = py5.create_image(py5.width, py5.height, 1) # 1 = RGB
py5.no_loop()
def draw():
global img
py5.load_pixels()
img.load_pixels()
for y in range(imgy):
zy = y*(yb - ya)/(imgy - 1) + ya
for x in range(imgx):
zx = x*(xb - xa)/(imgx - 1) + xa
z = complex(zx, zy)
for i in range(maxIt):
# Complex numerical derivative
dz = (f(z + complex(h, h)) - f(z))/complex(h, h)
if dz != 0:
z0 = z - f(z)/dz # Newton iteration
if abs(z0 - z) < eps:
# Stop when close enough to any root
break
z = z0
loc = x + y * py5.width
# pixels[loc] = color(i%5*64, i%9*32, i%17*16)
py5.pixels[loc] = py5.color(i%5*64, i%17*16, i%9*32)
py5.update_pixels()
py5.run_sketch()
Here is sketch running from geany on my RaspberryPI4 see New Python Processing project: Py5 - #11 by monkstone