Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created February 23, 2015 00:57
Show Gist options
  • Select an option

  • Save Craigson/cf3204ea1e5747807d1d to your computer and use it in GitHub Desktop.

Select an option

Save Craigson/cf3204ea1e5747807d1d to your computer and use it in GitHub Desktop.
Generative designs created using the equation for a Hypotrochoid curve.
//Hypotrochoid(a,b,h,da,db)
//a is the radius of the fixed circle
//b is the radius of the circle rotating inside the fixed circle
//h is the distance between the drawing and the centre
//on the rotating circle
Hypotrochoid h;
void setup() {
size(800, 800);
h = new Hypotrochoid(random(50,150),random(10,30),random(20,40),random(0.001,0.005),random(0.004,0.012));
background(50);
pushMatrix();
translate(width/2,height/2);
for (int i = 0; i < 20000; i++){
h.update();
h.display();
}
//h.drawTail();
popMatrix();
}
void draw() {
rotate(0.01);
pause();
}
void save(){
float a = random(0,1000);
int b = int(a);
save("images/generative_" + str(b) + ".jpg");
}
void keyPressed(){
if (key == 's'){
println("file saved");
save();
}
}
//a is the radius of the fixed circle
//b is the radius of the circle rotating inside the fixed circle
//h is the distance between the drawing and the centre
//on the rotating circle
//in a hypocloid, h = b;
class Hypotrochoid {
PVector loc;
float a, b, h;
float theta, dtheta;
float da, db;
float prevX, prevY;
color c;
//ArrayList<PVector> history;
Hypotrochoid(float _a, float _b, float _h, float _da, float _db) {
loc = new PVector( ( (a-b)*cos(theta) + h*cos( ((a-b)/b)*theta) ), ( ((a-b)*sin(theta) - h*sin(((a-b)/b)*theta)) ) );
a = _a;
b = _b;
h = _h;
theta = 0;
dtheta = 0.01;
da += _da;
db += _da;
prevX = 0;
prevY = 0;
c = color(random(0,255),random(0,255),random(0,255),random(0,255));
// history = new ArrayList<PVector>();
}
void display() {
stroke(c);
strokeWeight(1);
// history.add(loc.get());
line(prevX, prevY, loc.x, loc.y);
}
void update() {
prevX = loc.x;
prevY = loc.y;
loc.x = (a-b)*cos(theta) + h*cos( ((a-b)/b)*theta);
loc.y = (a-b)*sin(theta) - h*sin(((a-b)/b)*theta);
theta += dtheta;
h += 0.01;
// println ("prevX: " + prevX + " " + "prevY: " + prevY);
// println ("loc.x: " + loc.x + " " + "loc.y: " + loc.y);
}
// void drawTail() {
// beginShape();
// noFill();
// stroke(0);
// for (PVector v : history) {
// vertex(v.x, v.y);
// }
// endShape();
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment