Page MenuHomec4science

Cylinder.pde
No OneTemporary

File Metadata

Created
Sat, Nov 9, 08:54

Cylinder.pde

class Cylinder {
float cylinderBaseSize = 50;
float cylinderHeight = 50;
int cylinderResolution = 40;
PShape openCylinder = new PShape();
PShape closedCylinder = new PShape();
Cylinder() {
float angle;
float[] x = new float[cylinderResolution + 1];
float[] y = new float[cylinderResolution + 1];
//get the x and y position on a circle for all the sides
for (int i = 0; i < x.length; i++) {
angle = (TWO_PI / cylinderResolution) * i;
x[i] = sin(angle) * cylinderBaseSize;
y[i] = cos(angle) * cylinderBaseSize;
}
openCylinder = createShape();
openCylinder.beginShape(QUAD_STRIP);
//draw the border of the cylinder
for (int i = 0; i < x.length; i++) {
openCylinder.vertex(x[i], y[i], 0);
openCylinder.vertex(x[i], y[i], cylinderHeight);
}
openCylinder.endShape();
closedCylinder= createShape();
closedCylinder.beginShape(TRIANGLE_FAN);
closedCylinder.vertex(0, 0, cylinderHeight);
for (int i = 0; i < x.length; i++) {
closedCylinder.vertex(x[i], y[i], cylinderHeight);
}
closedCylinder.endShape();
}
void draw(float x,float y,float z,float r) {
pushMatrix();
translate(x, z, y);
rotateX(r);
shape(openCylinder);
shape(closedCylinder);
popMatrix();
}
}

Event Timeline