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);
}

Intersecting lines

  
  void intersect()
  {
     float m1,m2,b1,b2,ix,iy;
     Position p1a = new Position(mouseX, mouseY);
     Position p1b = new Position(points[maximum-1]);
     Position p2a = new Position();
     Position p2b = new Position();

     strokeWeight(4);
     stroke(0,255,0);
     line(p1a.x, p1a.y, p1b.x, p1b.y);
     
     // comparison line segment
     m1 = (p1a.y - p1b.y) / (p1a.x - p1b.x);
     b1 = p1a.y-m1*p1a.x;
     
     // cycle through each line segment on the gesture backwards
     for (int i = maximum-1; i > 0; i--) {
       
       //if (points[i].x < 0 && points[i].y < 0) { continue; }
       
       p2a.set(points[i]);
       p2b.set(points[i-1]);
       
       // second line segment
       m2 = (p2a.y - p2b.y) / (p2a.x - p2b.x);
       b2 = p2a.y - m2*p2a.x;
       
       if (m1 != m2) { // if they're not parallel
       
         // find the intersection
         ix = (b2-b1)/(m1-m2);
         iy = m1*ix+b1;
         
         // is the intersection on the line segments?
         if (ix > min(p1a.x, p1b.x) && ix < max(p1a.x, p1b.x) &&
             iy > min(p1a.y, p1b.y) && iy < max(p1a.y, p1b.y) &&
             ix > min(p2a.x, p2b.x) && ix < max(p2a.x, p2b.x) &&
             iy > min(p2a.y, p2b.y) && iy < max(p2a.y, p2b.y)) {
             
             if (i_count < i_max) {
               intersects[i_count].set(ix, iy);
               i_count++;
             } else {
               for (int j = 0; j < i_max-1; j++) {
                 intersects[j].set(intersects[j+1]);
               }
               intersects[i_max-1].set(ix, iy);
             }
             
             println("something");
             // send the points to the shape object
             s.set(points, i);
             reset();
             break;   
         }
       }
     }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS