处理3D弧形气缸

时间:2014-10-29 20:14:44

标签: 3d processing cylindrical

对你们所有人来说,我都有一个棘手的问题。我试图使用Processing绘制3个维度,并且遇到了障碍。我想绘制一个沿着给定的innerRadius弧形的圆柱体。我最终希望能够旋转它或者在弧形的不同点开始它,但是一旦我可以绘制弧线,我就能想到它。我的弧代码:

void drawCurvedVessel(int sides, float r, float l, float x, float y, float z, float     innerRadius, float degrees)
{
  float angle = 360 / sides;
  // draw top shape
  float start = innerRadius*degrees;
  translate(x,y,z);
  for (int n = 0; n < l; n++){
    float theta0 = n/innerRadius;
    float theta1 = (n+1)/innerRadius;
    float dx0 = innerRadius*cos(theta0);
    float dy0 = innerRadius*sin(theta0);
    float dx1 = innerRadius*cos(theta1);
    float dy1 = innerRadius*sin(theta1);
    beginShape(TRIANGLE_STRIP);
    for (int i = 0; i < sides + 3; i++) {
      x = cos( radians( i * angle ) ) * r;
      y = sin( radians( i * angle ) ) * r;
      float vertexZ1 = sin(theta1)*(innerRadius+sqrt((x+dx1)*(x+dx1)+y*y));
      vertex( x+dx1, y, vertexZ1);
      float vertexZ0 = sin(theta0)*(innerRadius+sqrt((x+dx0)*(x+dx0)+y*y));
      vertex( x+dx0, y, vertexZ0);
    }
    endShape(TRIANGLE_STRIP);
  }
  translate(-x,-y,-z);
}

渲染得相当好,除了弧沿着一侧倾斜。你能帮我画一个完全圆形的圆弧吗?

编辑:我已更新我的代码。它工作得更好,但它没有完整的循环。相反,它看起来像这样捏在顶部和底部: https://drive.google.com/file/d/0B7A0w7ZdcEuQUmIzQkFlYzBBUkk/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

OP发现答案:

  

事实证明,我太复杂了。为了将来参考,如果有人想要一个可以生成三维弧的代码,这里是代码!

void drawCurvedVessel(int sides, float r, float l, float x, float y, float z, float innerRadius, float degrees)
{
  float angle = 360 / sides;
  int start = int(innerRadius*radians(degrees));
  translate(x,y,z);
  for (int n = start; n < l+start; n++){
    float theta0 = n/innerRadius;
    float theta1 = (n+1)/innerRadius;
    beginShape(TRIANGLE_STRIP);
    for (int i = 0; i < sides + 3; i++) {
      float vy = sin( radians( i * angle ) ) * r;
      float vx0 = (innerRadius-r*cos( radians( i * angle ) ))*cos(theta0);
      float vx1 = (innerRadius-r*cos( radians( i * angle ) ))*cos(theta1);
      float vertexZ1 = sin(theta1)*(innerRadius-cos( radians( i * angle ) ) * r);
      vertex( vx1, vy, vertexZ1);
      float vertexZ0 = sin(theta0)*(innerRadius-cos( radians( i * angle ) ) * r);
      vertex( vx0, vy, vertexZ0);
    }
    endShape(TRIANGLE_STRIP);
  }
  translate(-x,-y,-z);
}