julia.py

Created by vnap0v

Created on April 28, 2026

611 Bytes

Julia Fractal

This code displays a colorful Julia Fractal.

It uses the kandinsky and math modules.

The function

Z**2 + C  --> Z 

is iterated.

Both Z and C are complex values.

C is a complex constant value and defines the appearance of the fractal.

The initial value of Z depends on the position within the fractal, the real part depends on the x coordinate, the imaginary part depends on the y coordinate.

A loop measures how many iterations it takes for the modulus of Z to exceed the value of 2.0

The counter which keeps track of the number of needed iterations is then used to determine the color of the pixel being calculated


# Julia Fractal using only math
# and kandinsky libraries
from kandinsky import *
from math import *
# complex constant for julia fractal
c=complex(-0.5125,0.5213)
# calculation and plotting
for x in range(320):
  re=(x-160)/160*1.5
  for y in range(210):
    im=(y-105)/105
    z=complex(re,im)
    i=0
    while abs(z)<2 and i<1024:
      z=z**2+c
      i+=1
    # apply non linear scaling on i
    i = int(sqrt(i)*8) 
    # calculate color comp. from i
    r = i % 33 * 8; r = min(255, r) 
    g = i % 129 * 2; g = min(255, g)
    b = i % 65 * 4; b = min(255, b)
    set_pixel(x,y,color(r,g,b))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.