-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cylinder.pde
70 lines (59 loc) · 2.21 KB
/
Cylinder.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
final float cylinderBaseSize = 20;
final float cylinderHeight = 50;
final int cylinderResolution = 40;
final color defaultCylinderColour = color(220, 60, 60);
class Cylinder {
PShape closedCylinder = new PShape();
PShape openCylinder = new PShape();
PShape topDisk = new PShape();
//#############################################
//-----------CONSTRUCTOR OF CYLINDER-----------
//#############################################
Cylinder(color cylinderColour) {
// Initialise the Cylinder
closedCylinder = new PShape();
openCylinder = new PShape();
topDisk = new PShape();
float angle;
float[] x = new float[cylinderResolution + 1];
float[] z = 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;
z[i] = cos(angle) * cylinderBaseSize;
}
//#############################################
//-----------SHAPE OF OPEN CYLINDER------------
//#############################################
openCylinder = createShape();
openCylinder.beginShape(QUAD_STRIP);
openCylinder.fill(cylinderColour);
//Draw the border of the cylinder
for(int i = 0; i < x.length; i++) {
openCylinder.vertex(x[i] , 0, z[i]);
openCylinder.vertex(x[i], -cylinderHeight, z[i]);
}
openCylinder.endShape();
//#############################################
//-----------DISK OF CLOSED CYLINDER-----------
//#############################################
topDisk = createShape();
topDisk.beginShape(TRIANGLE_FAN);
topDisk.fill(cylinderColour);
for (int i = 0; i< x.length; i++) {
topDisk.vertex(x[i], -cylinderHeight, z[i]);
}
topDisk.endShape();
// MERGE TOP DISK WITH OPEN CYLINDER
closedCylinder = createShape(GROUP);
closedCylinder.addChild(openCylinder);
closedCylinder.addChild(topDisk);
}
//#############################################
//----------------DISPLAY METHOD---------------
//#############################################
void display() {
gameSurface.shape(closedCylinder);
}
}