运行 处理中有 2 个函数

Running 2 functions in Processing

我似乎无法连续 运行 2 个功能。只执行第一个函数。当我评论这两个功能之一时,草图工作正常。

完整代码如下:

float angle1, angle2;

void setup()
{
  size(500, 500);
  smooth();
  frameRate(10);
}

void draw()
{
  background(64);
  strokeWeight(1);
  noFill();
  translate(width/2, height/2);
  circle1();
  circle2();
}

void circle1()
{
  stroke(200, 0, 0);
  rotate(radians(angle1));
  for(int i = 0; i < 360; i += 5)
  {
    rotate(radians(5));
    bezier(0, 0, -50, -50, -50, -150, 0, -200);
  }
  angle1 -= 1;
}

void circle2()
{
  stroke(0, 200, 0);
  rotate(radians(angle2));
  for(int i = 360; i > 0; i -= 5)
  {
   rotate(radians(-5));
   bezier(0, 0, 50, -50, 50, -150, 0, -200);
  }
  angle2 += 1;
}

为什么会这样?

非常感谢

您的代码确实有效,问题是您使用的是 single/global 坐标系,当您在 circle1() 中调用 rotate() 时,您会旋转 整个草图在一个方向,然后在circle2()你在相反的方向旋转所有东西(因此取消旋转)

你应该看看pushMatrix() and popMatrix(),but especially the 2D Transformations tutorial

简而言之,使用pushMatrix()开始隔离坐标系,popMatrix()恢复到之前的坐标系。这样,您的旋转就不会相互干扰:

float angle1, angle2;

void setup()
{
  size(500, 500);
  smooth();
  frameRate(10);
}

void draw()
{
  background(64);
  strokeWeight(1);
  noFill();
  translate(width/2, height/2);
  circle1();
  circle2();
}

void circle1()
{
  pushMatrix();
  stroke(200, 0, 0);
  rotate(radians(angle1));
  for(int i = 0; i < 360; i += 5)
  {
    rotate(radians(5));
    bezier(0, 0, -50, -50, -50, -150, 0, -200);
  }
  angle1 -= 1;
  popMatrix();
}

void circle2()
{
  pushMatrix();
  stroke(0, 200, 0);
  rotate(radians(angle2));
  for(int i = 360; i > 0; i -= 5)
  {
   rotate(radians(-5));
   bezier(0, 0, 50, -50, 50, -150, 0, -200);
  }
  angle2 += 1;
  popMatrix();
}

此外,请记住您调用 rotate(radians(5))rotate(radians(-5)) 并且您可能希望在 radians() 调用中使用 angle1angle2