Problem with displaying a PShape

ShapeMaker sm;
void setup() {
  size(600, 600, P3D);
  sm = new ShapeMaker();
  sm.makeCylinder(100, 100, 200, 40);
  //sm.makeEllipse(200);
}

void draw() {
  background(0);
  translate(width/2, height/2);
  sm.displayShape();
}

class ShapeMaker {

  PShape heldShape;

  ShapeMaker() {
    this.heldShape = new PShape();
  }

  void displayShape() {
    shape(heldShape);
  }

  PShape makeEllipse(int radius) {
    PShape ellipse = createShape(ELLIPSE, 0, 0, radius, radius);
    this.heldShape = ellipse;
    return ellipse;
  }

  PShape makeCylinder(int topRadius, int bottomRadius, int height, int numSides) {
    int sideSpacing = 360/numSides;
    PShape cylinder = new PShape(GROUP);
    PShape top = createShape();
    PShape bottom = createShape();
    PShape sides = new PShape(GROUP);
    top.setStroke(3);
    top.beginShape();
    for (int i = 0; i <= numSides; i++) {
      top.vertex(cos(radians(sideSpacing*i))*topRadius, 0, sin(radians(sideSpacing*i))*topRadius);
    } 
    top.endShape();
    bottom.setStroke(3);
    bottom.beginShape();
    for (int i = 0; i <= numSides; i++) {
      bottom.vertex(cos(radians(sideSpacing*i))*bottomRadius, height, sin(radians(sideSpacing*i))*bottomRadius);
    } 
    bottom.endShape();
    for (int i = 0; i <= numSides; i++) {
      sides.addChild(weirdRect(
        makePV(cos(radians(sideSpacing*i))*topRadius, 0, sin(radians(sideSpacing*i))*topRadius), 
        makePV(cos(radians(sideSpacing*i))*bottomRadius, height, sin(radians(sideSpacing*i))*bottomRadius), 
        makePV(cos(radians(sideSpacing*(i+1)))*bottomRadius, height, sin(radians(sideSpacing*(i+1)))*bottomRadius), 
        makePV(cos(radians(sideSpacing*(i+1)))*topRadius, 0, sin(radians(sideSpacing*(i+1)))*topRadius)
        ));
    }
    top.setFill(color(0, 0, 255));
    bottom.setFill(color(255, 0, 0));
    sides.setFill(color(0, 255, 0));
    cylinder.addChild(top);
    cylinder.addChild(bottom);
    cylinder.addChild(sides);
    return cylinder;
  }
}

PShape weirdRect(PVector pointOne, PVector pointTwo, PVector pointThree, PVector pointFour) {
  PShape s = createShape();
  s.beginShape();
  s.vertex(pointOne.x, pointOne.y, pointOne.z);
  s.vertex(pointTwo.x, pointTwo.y, pointTwo.z);
  s.vertex(pointThree.x, pointThree.y, pointThree.z);
  s.vertex(pointFour.x, pointOne.y, pointFour.z);
  s.endShape(CLOSE);
  return s;
}

PVector makePV(float x, float y, float z) {
  PVector cylinder = new PVector(x, y, z);
  return cylinder;
}

In this code when I try to display the cylinder I made inside of the class it doesn’t display anything but if i do something else like a ellipse through the same class it does. Could you help me?

2 Likes

I tinkered with this a bit and it displays:

PShape makeCylinder(int topRadius, int bottomRadius, int height, int numSides) 
    {
    int sideSpacing = 360/numSides;
    PShape cylinder = createShape(GROUP);   
    PShape top = createShape();
    PShape bottom = createShape();   
    PShape sides = createShape(GROUP);
    
    top.setStroke(3);
    top.beginShape();
    for (int i = 0; i <= numSides; i++) 
      {
      top.vertex(cos(radians(sideSpacing*i))*topRadius, -height/2, sin(radians(sideSpacing*i))*topRadius);
      } 
    top.endShape();
    
    
    bottom.setStroke(3);
    bottom.beginShape();
    for (int i = 0; i <= numSides; i++) 
      {
      bottom.vertex(cos(radians(sideSpacing*i))*bottomRadius, height, sin(radians(sideSpacing*i))*bottomRadius);
      } 
    bottom.endShape();
      
    for (int i = 0; i <= numSides; i++) 
      {
      sides.addChild(weirdRect(
        makePV(cos(radians(sideSpacing*i))*topRadius, 0, sin(radians(sideSpacing*i))*topRadius), 
        makePV(cos(radians(sideSpacing*i))*bottomRadius, height/2, sin(radians(sideSpacing*i))*bottomRadius), 
        makePV(cos(radians(sideSpacing*(i+1)))*bottomRadius, height/2, sin(radians(sideSpacing*(i+1)))*bottomRadius), 
        makePV(cos(radians(sideSpacing*(i+1)))*topRadius, 0, sin(radians(sideSpacing*(i+1)))*topRadius)
        ));
      }
       
    top.setStroke(3);
    top.setFill(color(0, 0, 255));   
    bottom.setFill(color(255, 0, 0));  
    sides.setFill(color(0, 255, 0));
    
    cylinder.addChild(top);
    cylinder.addChild(bottom);
    cylinder.addChild(sides);
    
    this.heldShape = cylinder;
    return cylinder;
    }
2 Likes

Thanks @glv ill try it out.

1 Like