gingerbread.py

Created by vnap0v

Created on June 10, 2025

624 Bytes

This script draws a “Gingerbread man” fractal. The shape is generated by applying a simple iteration and plotting the x,y values. It has an extra function which keeps track of the distance between successive x,y points in the iterations, this is used to derive the color of the points to make the fractal look more interesting visually. It uses the kandisnky and math libraries. Tested on the calculator using software version 23.2.6.


# Gingerbread man fractal
import kandinsky
from math import *

def dist(x1,y1,x2,y2):
  return sqrt((x1-x2)**2+(y1-y2)**2)

def drawgingerbread(N=50000):
  kandinsky.fill_rect(0,0,320,230,"black")
  x,y=-0.1,0
  for _ in range(N):
    xold,yold=x,y
    x,y=1-y+abs(x),x
    #print((x-xold)**2+(y-yold)**2)
    xscr=int(300*(x+3.5)/12)
    yscr=int(210*(9.5-y)/12)
    c=int(dist(x,y,xold,yold)/12*512)
    g=(c % 32)*8+7
    b=(c % 256)
    r=(c % 128)*2+1
    kandinsky.set_pixel(xscr,yscr,kandinsky.color(r,g,b))
  kandinsky.draw_string("Gingerbread man fractal",10,2,"green","black")

drawgingerbread()

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.