Cone, cylinder, pyramid
To create the the sides of cones, cylinder and pyramids you can use this code:
import controlP5.*;
import processing.opengl.*;
ControlP5 controlP5;
float topRadius=10;
float bottomRadius=10;
int tall=100;
int sides=4;
float rotationX=-PI/2;
float rotationY=-PI;
void setup() {
size(400, 400, OPENGL);
controlP5 = new ControlP5(this);
controlP5.addSlider("top", topRadius, 100, 10, 10, 20, 100, 14).setId(1);
controlP5.addSlider("bottom", bottomRadius, 100, 10, 10, 50, 100, 14).setId(2);
controlP5.addSlider("height", 10, 140, 100, 10, 80, 100, 14).setId(3);
Slider s=controlP5.addSlider("sides", 4, 100, 4, 10, 110, 100, 14);
Slider x=controlP5.addSlider("rotationx", rotationX, PI/2, 0, 10, 140, 100, 14);
Slider y=controlP5.addSlider("rotationy", rotationY, PI, 0, 10, 170, 100, 14);
s.setId(4);
s.setMin(3);
s.setMax(30);
y.setId(6);
y.setMin(-PI);
y.setMax(2*PI);
x.setId(5);
x.setMin(-PI);
x.setMax(2*PI);
}
void draw() {
background(0);
controlP5.draw();
lights();
directionalLight(255, 0, 0, -1, 1, 0);
directionalLight(0, 0, 255, 1, 1, 1);
directionalLight(255, 255, 0, 0, -1, 1);
pushMatrix();
translate((int)width / 2, (int)height / 2);
rotateX(rotationX);
rotateY(rotationY);
noStroke();
fill(255);
drawCylinder(topRadius, bottomRadius, tall, sides);
popMatrix();
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
}
public void controlEvent(ControlEvent theEvent) {
switch(theEvent.controller().id()) {
case(1): // top
topRadius = (int)(theEvent.controller().value());
break;
case(2): // bottom
bottomRadius = (int)(theEvent.controller().value());
break;
case(3): // height
tall = (int)(theEvent.controller().value());
break;
case(4): // sides
sides = (int)(theEvent.controller().value());
break;
case(5): // rot x
rotationX = (int)(theEvent.controller().value());
break;
case(6): // rot y
rotationY = (int)(theEvent.controller().value());
break;
}
}
To add the top to the shape:
import controlP5.*;
import processing.opengl.*;
ControlP5 controlP5;
float topRadius=10;
float bottomRadius=10;
int tall=100;
int sides=4;
float rotationX=0;
float rotationY=0;
void setup() {
size(400, 400, OPENGL);
controlP5 = new ControlP5(this);
controlP5.addSlider("top", topRadius, 100, 10, 10, 20, 100, 14).setId(1);
controlP5.addSlider("bottom", bottomRadius, 100, 10, 10, 50, 100, 14).setId(2);
controlP5.addSlider("height", 10, 140, 100, 10, 80, 100, 14).setId(3);
Slider s=controlP5.addSlider("sides", 4, 100, 4, 10, 110, 100, 14);
Slider x=controlP5.addSlider("rotationx", rotationX, PI/2, 0, 10, 140, 100, 14);
Slider y=controlP5.addSlider("rotationy", rotationY, PI, 0, 10, 170, 100, 14);
s.setId(4);
s.setMin(3);
s.setMax(30);
y.setId(6);
y.setMin(-PI);
y.setMax(2*PI);
x.setId(5);
x.setMin(-PI);
x.setMax(2*PI);
}
void draw() {
background(0);
controlP5.draw();
lights();
directionalLight(255, 0, 0, -1,1, 0);
directionalLight(0, 0, 255, 1,1, 1);
directionalLight(255, 255, 0,0,-1, 1);
pushMatrix();
translate((int)width / 2, (int)height / 2);
rotateX(rotationX);
rotateY(rotationY);
noStroke();
fill(255);
drawCylinder(topRadius, bottomRadius, tall, sides);
popMatrix();
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
fill(255,255,255,200);
stroke(0);
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i <sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
public void controlEvent(ControlEvent theEvent) {
switch(theEvent.controller().id()) {
case(1): // top
topRadius = (int)(theEvent.controller().value());
break;
case(2): // bottom
bottomRadius = (int)(theEvent.controller().value());
break;
case(3): // height
tall = (int)(theEvent.controller().value());
break;
case(4): // sides
sides = (int)(theEvent.controller().value());
break;
case(5): // rotation x
rotationX = (int)(theEvent.controller().value());
break;
case(6): // rotation y
rotationY = (int)(theEvent.controller().value());
break;
}
}
To add the bottom:
import controlP5.*;
import processing.opengl.*;
ControlP5 controlP5;
float topRadius=10;
float bottomRadius=10;
int tall=100;
int sides=4;
float rotationX=-PI/2;
float rotationY=-PI;
void setup() {
size(400, 400, OPENGL);
controlP5 = new ControlP5(this);
controlP5.addSlider("top", topRadius, 100, 10, 10, 20, 100, 14).setId(1);
controlP5.addSlider("bottom", bottomRadius, 100, 10, 10, 50, 100, 14).setId(2);
controlP5.addSlider("height", 10, 140, 100, 10, 80, 100, 14).setId(3);
Slider s=controlP5.addSlider("sides", 4, 100, 4, 10, 110, 100, 14);
Slider x=controlP5.addSlider("rotationx", rotationX, PI/2, 0, 10, 140, 100, 14);
Slider y=controlP5.addSlider("rotationy", rotationY, PI, 0, 10, 170, 100, 14);
s.setId(4);
s.setMin(3);
s.setMax(30);
y.setId(6);
y.setMin(-PI);
y.setMax(2*PI);
x.setId(5);
x.setMin(-PI);
x.setMax(2*PI);
}
void draw() {
background(0);
controlP5.draw();
lights();
directionalLight(255, 0, 0, -1,1, 0);
directionalLight(0, 0, 255, 1,1, 1);
directionalLight(255, 255, 0,0,-1, 1);
pushMatrix();
translate((int)width / 2, (int)height / 2);
rotateX(rotationX);
rotateY(rotationY);
noStroke();
fill(255);
drawCylinder(topRadius, bottomRadius, tall, sides);
popMatrix();
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i <sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
fill(255,255,255,200);
stroke(0);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
public void controlEvent(ControlEvent theEvent) {
switch(theEvent.controller().id()) {
case(1): // top
topRadius = (int)(theEvent.controller().value());
break;
case(2): // bottom
bottomRadius = (int)(theEvent.controller().value());
break;
case(3): // height
tall = (int)(theEvent.controller().value());
break;
case(4): // sides
sides = (int)(theEvent.controller().value());
break;
case(5): // rot x
rotationX = (int)(theEvent.controller().value());
break;
case(6): // rot y
rotationY = (int)(theEvent.controller().value());
break;
}
}