processing绘图问题 圆柱体函数

请教各位,processing中有画立方体和球体的函数,但是有画圆柱体的函数吗?

没有!但是可以手动实现。参考以下代码:

void cylinder(float bottom, float top, float h, int sides)
{
  pushMatrix();

  translate(0, h/2, 0);

  float angle;
  float[] x = new float[sides+1];
  float[] z = new float[sides+1];

  float[] x2 = new float[sides+1];
  float[] z2 = new float[sides+1];

  //get the x and z position on a circle for all the sides
  for (int i=0; i < x.length; i++) {
    angle = TWO_PI / (sides) * i;
    x[i] = sin(angle) * bottom;
    z[i] = cos(angle) * bottom;
  }

  for (int i=0; i < x.length; i++) {
    angle = TWO_PI / (sides) * i;
    x2[i] = sin(angle) * top;
    z2[i] = cos(angle) * top;
  }

  //draw the bottom of the cylinder
  beginShape(TRIANGLE_FAN);

  vertex(0, -h/2, 0);

  for (int i=0; i < x.length; i++) {
    vertex(x[i], -h/2, z[i]);
  }

  endShape();

  //draw the center of the cylinder
  beginShape(QUAD_STRIP); 

  for (int i=0; i < x.length; i++) {
    vertex(x[i], -h/2, z[i]);
    vertex(x2[i], h/2, z2[i]);
  }

  endShape();

  //draw the top of the cylinder
  beginShape(TRIANGLE_FAN); 

  vertex(0, h/2, 0);

  for (int i=0; i < x.length; i++) {
    vertex(x2[i], h/2, z2[i]);
  }

  endShape();

  popMatrix();
}



void setup() {
  size(500, 400, P3D);
  background(10, 80, 100);
  stroke(240, 70, 70);
  fill(119, 250, 83);
}

void draw() {
  noFill();
  translate(100, 100, -100);
  cylinder(100, 100, 150, 50);
}

来自:GitHub