1

我为大家准备了一个棘手的问题。我正在尝试使用 Processing 绘制 3 个维度并且遇到了障碍。我想绘制一个沿给定内半径弧形的圆柱体。我最终希望能够旋转它或在圆弧中的不同点启动它,但是一旦我可以画出圆弧,我就能弄清楚这一点。我的圆弧代码:

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

4

1 回答 1

0

OP找到的答案:

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

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);
}
于 2016-08-20T11:00:00.323 回答