kings_dream.py

Created by vnap0v

Created on June 12, 2025

661 Bytes

King’s Dream Fractal

This script draws the fractal defined by iterating the x,y values through the following function

    x = sin(a*x) + b*sin(a*y)
    y = sin(c*x) + d*sin(c*y)

with constants of

    a = 2.879879
    b = -0.765145
    c = -0.966918
    d = 0.744728

and initial values of x=2, y=0

At each point x,y the screen color is retrieved and increased The script uses the kandinsky and math libraries. Tested on the calculator using software version 23.2.6.


import kandinsky
from math import *
a = 2.879879# constants 
b = -0.765145
c = -0.966918
d = 0.744728

def draw_fractal(N=100000):
  x,y=2.0,2.0 # initial values
  kandinsky.fill_rect(0,0,320,230,"black")
  for _ in range(N):
      x,y=sin(a*x)+b*sin(a*y),sin(c*x)+d*sin(c*y)
      xscr=int(300*(x+2)/4+10)
      yscr=int(220*(y+2)/4+10)
      pr,pg,pb=kandinsky.get_pixel(xscr,yscr)
      pr+=30;pg+=10;pb+=50 # increase color
      pr=min(255,pr) # clip color
      pg=min(255,pg)
      pb=min(255,pb)
      kandinsky.set_pixel(xscr,yscr,(pr,pg,pb))
  kandinsky.draw_string("King's Dream Fractal",10,5,(180,127,255),"black")

draw_fractal()

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.