Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Cantor's Sets with Processing

This simple Processing programme generates the Cantor's Sets fractal.

Images are generated randomly, but tend to look like this.

int setHeight = 60;
color setColour = color(184 + random(-40, 40),
                        124 + random(-40, 40),
                        124 + random(-40, 40));
int wid = 600;

void setup() {
  size(wid, (int)((log(wid) / log(3) + 1) * (setHeight + 5)));
  background(0);
  smooth();
  
  cantorSet(10, 10, width - 20, setColour);
  
  save("cantorSet.png");
}

float rnd(float x) {
  return x + random(-20, 20);
}

void brushRect(int x, int y, int w, int h) {
  for (int i = 0; i < h; ++i) {
    float r = random(3);
    strokeWeight(r);
    float downpull = random(h / 4);
    float shudder = random(-2, 2);
    line(x + shudder, y + i, x + w - shudder, y + i + downpull);
  }
}

void cantorSet(int y, int offX, int wid, color col) {
  if ((y > height - 10) || (wid < 1))
    return;

  stroke(col);
  brushRect(offX, y, wid, setHeight);
  //fill(col);
  //rect(offX, y, wid, setHeight);
  
  color newCol = color(rnd(red(col)), rnd(green(col)),
                      rnd(blue(col)));
  cantorSet(y + setHeight + 5, offX, wid / 3, newCol);
  cantorSet(y + setHeight + 5, offX + wid*2/3, wid / 3, newCol);
}

Mandelbrot Set Fractal

I take the code from Gustavo Niemeyer and modify it a bit.
import e32
from appuifw import *

app.screen = 'full'
app.body = canvas = Canvas()
width, height = canvas.size

xaxis = width/2
yaxis = height/1.5
scale = 60
iterations = 25

for y in range(height):
  for x in range(width):
    magnitude = 0
    z = 0+0j
    c = complex(float(y-yaxis)/scale, float(x-xaxis)/scale)
    for i in range(iterations):
      z = z**2+c
      if abs(z) > 2:
        v = 765*i/iterations
        if v > 510:
          color = (255, 255, v%255)
        elif v > 255:
          color = (255, v%255, 0)
        else:
          color = (v%255, 0, 0)
        break
    else:
      color = (0, 0, 0)
    canvas.point((x, y), color)
  e32.ao_yield()
 
lock = e32.Ao_lock()
app.exit_key_handler = lock.signal
lock.wait()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS