Cantor Set

I lifted some code that makes the somewhat dull Cantor set quite pretty by randomizing the line strokes.

Implementation

int setHeight = 60;
color setColour;
int wid = 600;

int recursionDepth = 1;

boolean randomizeBrushStrokes = true;

class CantorSet {
  ArrayList<CantorSet> cantorSetsWithin = new ArrayList<CantorSet>();

  CantorSet() {
  }

  void grow() {
    if (cantorSetsWithin.size() == 0) {
    } else {
      for (CantorSet setWithin : cantorSetsWithin) {
        setWithin.grow();
      }
    }
  }
}

void setup() {
  frameRate(1);
  size(600, 400);
  setColour = color(184 + random(-40, 40), 124 + random(-40, 40), 124 + random(-40, 40));
}

void draw() {

  background(0);
  smooth();

  cantorSet(recursionDepth, 10, 10, width - 20, setColour);
  recursionDepth *= 2;
  if (recursionDepth == 16) recursionDepth = 1;
}

float rnd(float x) {
  float new_x = x + random(-20, 20);
  if (new_x < 0) new_x = 0.0;
  else if (new_x > 255) new_x = 255.0;
  return new_x;
}

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

void cantorSet(int depthRemaining, int y, int offX, int wid, color col) {
  if (depthRemaining == 0) return;

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