Skip to content

Instantly share code, notes, and snippets.

Created Feb 24, 2017
Embed
What would you like to do?
c is for cylinders
// gif by davey
// aka bees & bombs
// @beesandbombs
int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);
else
return 1 - 0.5 * pow(2*(1 - p), g);
}
float mn = .5*sqrt(3), ia = atan(sqrt(.5));
void push() {
pushMatrix();
pushStyle();
}
void pop() {
popStyle();
popMatrix();
}
void draw() {
if (!recording) {
t = mouseX*1.0/width;
c = mouseY*1.0/height;
if (mousePressed)
println(c);
draw_();
} else {
for (int i=0; i<width*height; i++)
for (int a=0; a<3; a++)
result[i][a] = 0;
c = 0;
for (int sa=0; sa<samplesPerFrame; sa++) {
t = map(frameCount-1 + sa*shutterAngle/samplesPerFrame, 0, numFrames, 0, 1);
draw_();
loadPixels();
for (int i=0; i<pixels.length; i++) {
result[i][0] += pixels[i] >> 16 & 0xff;
result[i][1] += pixels[i] >> 8 & 0xff;
result[i][2] += pixels[i] & 0xff;
}
}
loadPixels();
for (int i=0; i<pixels.length; i++)
pixels[i] = 0xff << 24 |
int(result[i][0]*1.0/samplesPerFrame) << 16 |
int(result[i][1]*1.0/samplesPerFrame) << 8 |
int(result[i][2]*1.0/samplesPerFrame);
updatePixels();
saveFrame("f###.gif");
if (frameCount==numFrames)
exit();
}
}
//////////////////////////////////////////////////////////////////////////////
int samplesPerFrame = 4;
int numFrames = 280;
float shutterAngle = .4;
boolean recording = false;
int N = 120;
void cyl(float r, float h, int col) {
fill(col == 0 ? 32 : 250);
beginShape(TRIANGLE_STRIP);
for (int i=0; i<=N; i++) {
vertex(r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N), -h/2);
vertex(r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N), h/2);
}
endShape(CLOSE);
fill(col == 0 ? 250 : 32);
beginShape(TRIANGLE_FAN);
vertex(0,0,-h/2);
for (int i=0; i<=N; i++)
vertex(r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N), -h/2);
endShape();
beginShape(TRIANGLE_FAN);
vertex(0,0,h/2);
for (int i=0; i<=N; i++)
vertex(r*cos(TWO_PI*i/N), r*sin(TWO_PI*i/N), h/2);
endShape();
}
void setup() {
size(540, 540, P3D);
result = new int[width*height][3];
smooth(8);
noStroke();
ortho();
}
void draw_() {
background(250);
push();
translate(width/2, height/2);
scale(1.06);
rotate(PI/3);
rotateX(TWO_PI*ease(t,5.5));
rotate(-PI/3);
///// 3 cylinders for C
push();
translate(0,0,0);
cyl(100,100,1);
pop();
push();
translate(0,0,240);
cyl(50,75,0);
pop();
push();
translate(60,0,110);
rotateX(HALF_PI);
cyl(50,45,1);
pop();
///// various others
push();
translate(-120,-150,40);
cyl(85,60,0);
pop();
push();
translate(-110,-120,-80);
cyl(50,30,1);
pop();
push();
translate(50,100,-200);
cyl(80,30,0);
pop();
push();
translate(-150,100,100);
rotateX(HALF_PI);
cyl(50,80,1);
pop();
push();
translate(120,-150,90);
rotate(HALF_PI);
rotateX(HALF_PI);
cyl(50,80,1);
pop();
push();
translate(-30,40,120);
rotateX(HALF_PI);
cyl(30,40,0);
pop();
push();
translate(310,30,-100);
cyl(50,70,1);
pop();
push();
translate(50,10,-120);
rotate(HALF_PI);
rotateX(HALF_PI);
cyl(40,50,0);
pop();
push();
translate(100,130,70);
cyl(45,55,0);
pop();
push();
translate(30,-150,-170);
cyl(45,55,0);
pop();
push();
translate(-280,80,-90);
rotate(HALF_PI);
rotateX(HALF_PI);
cyl(30,45,0);
pop();
pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment