如何使用FOR循环在圆圈中创建圆圈(在处理中)

时间:2015-01-12 13:40:37

标签: for-loop geometry 2d processing trigonometry

我需要创建一个循环,它会在Processing中围绕圆圈平均圈出圆圈。

我知道我可以以某种方式实现FOR循环。

我需要能够增加或减少此圈子周围的圈数(按下按钮),但保持它们的间距相等。

我知道公式我需要包含在FOR循环中以获得X和Y轴。公式:

being
   X = R*cos(angle-90)+Y0
   Y = R*sin(angle-90)+X0

我理解FOR循环的三个参数;什么时候开始,什么时候结束,什么时候运行会发生变化。

我看不到的是如何将公式实现到FOR循环中。

非常感谢

以下是我的代码

void setup () {
  size (600, 600);
  background (255, 255, 255);
  smooth ();
  ellipse (width/2, height/2, 200, 200); // the guide circle. Not needed in final code.
}


void draw() {


  for (int i = 0; i < 20; i ++) {
    for (int j = 0; j < 20; j ++) {

      ellipse (i *20, j * 20, 20, 20);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

这段代码可以解决问题:

    float incrementalAngle = 0.0;

void setup(){
  size(600, 600);
  smooth();
  background(0);

  ellipse(width/2, height/2, 200, 200);
  drawCircles(20, 200);
}

void draw(){

}

void drawCircles(int circlesNumber, int bigCircleNumber){
  float angle = incrementalAngle;

  for(int i = 0; i < circlesNumber; i++){
    ellipse(bigCircleNumber * cos(incrementalAngle) + height/2, 
            bigCircleNumber * sin(incrementalAngle) + width/2, 
            circlesNumber, circlesNumber);
    incrementalAngle += TWO_PI / circlesNumber;  
  } 
}

Here is the output

所以不需要第二个循环,你试图引入的公式将进入你的椭圆的X和Y位置,通过玩角度和cos和sin你可以得到你的结果寻找。

现在剩下的就是通过点击mousePressed()方法并绘制该数量来获得所需的圈数。

希望这有用,如果您需要更多帮助,请致电我

此致 何。

答案 1 :(得分:0)

感谢所有帮助过的人。

我设法做到了(与你@Jose Gonzalez略有不同

   int nbr_circles = 2;
void setup() {    
  size(600, 600);

  smooth();
  background(255);
} 

void draw() { 
  background(255);
  float cx = width/2.0;
  float cy = height/2.0;
  fill(0);
  //float x, y; //  
  for (int i = 0; i < nbr_circles; i++) 
  {
    float angle = i * TWO_PI / nbr_circles;
    float x = cx + 110.0 * cos(angle);                
    float y = cy + 110.0 * sin(angle);                
    ellipse(x, y, 20, 20);
  }
}

void mousePressed() {

  if (mouseButton == LEFT) {
    if (nbr_circles < 20)
    nbr_circles = nbr_circles + 1;

  } else if (mouseButton == RIGHT) {
    if (nbr_circles > 2) 
      nbr_circles = nbr_circles - 1;

  }
}