# Problem with displaying a PShape

**URL:** https://discourse.processing.org/t/problem-with-displaying-a-pshape/12267
**Category:** Coding Questions
**Created:** [June 24, 2019, 3:34pm UTC](https://discourse.processing.org/t/problem-with-displaying-a-pshape/12267 "2019-06-24T15:34:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![elijb](https://avatars.discourse-cdn.com/v4/letter/e/ebca7d/32.png) [@elijb](https://discourse.processing.org/u/elijb)
#### Post date: [June 24, 2019, 3:34pm UTC](https://discourse.processing.org/t/problem-with-displaying-a-pshape/12267/1 "2019-06-24T15:34:22Z")

</div>

```auto
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?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 24, 2019, 6:00pm UTC](https://discourse.processing.org/t/problem-with-displaying-a-pshape/12267/2 "2019-06-24T18:00:55Z")

</div>

I tinkered with this a bit and it displays:

```auto
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;
    }

```

---

<div class="post-metadata">

### Author: ![elijb](https://avatars.discourse-cdn.com/v4/letter/e/ebca7d/32.png) [@elijb](https://discourse.processing.org/u/elijb)
#### Post date: [June 26, 2019, 6:56pm UTC](https://discourse.processing.org/t/problem-with-displaying-a-pshape/12267/3 "2019-06-26T18:56:12Z")

</div>

Thanks @glv ill try it out.
