怎么做:循环+每次循环增加半径?

How to do: for looping + increasing of radius each loop?

如何创建一个干净简单的代码,在较大的圆圈内创建一个 point/dots 圆圈?或者类似的东西(抱歉,我无法 post 我想要的图像)。我被告知尝试在我的代码外部使用 for 循环,并在循环的每次迭代中稍微增加半径。但是,我不知道如何增加半径?

这是我到目前为止一直在试验的代码:

size (400, 400);
background(255);
noStroke();
fill(0);
smooth();
translate(width/2, height/2);

int numpoints = 10;
float angleinc = 2 * PI / numpoints;
int radius = 100;

for (int i = 0; i < numpoints; i++) {
  float x = cos(angleinc * i) * radius;
  float y = sin(angleinc * i) * radius;

  ellipse(x, y, 4, 4);
}

请提供任何快速帮助,我们将不胜感激。另外,我对处理和编码还很陌生,所以我不是最好的...

我希望我回答对了你的问题- 围绕原点的圆的公式是 x=Rcos(angle) y=Rsin(angle) 其中天使在 0 到 2*pi

之间

如果你想围绕点画圆,假设围绕 (x',y'),公式为 x= x' + Rcos(angle) y= y' + r sin(角度)

代码:

  float epsilon = 0.0001f;
    float R = 5.5.f; 
    for (float angle = 0.0; angle < 2*PI; angle += epsilon ) {
        float x = x' + R*cos(angle);
        float y = y' + R*sin(angle);
        drawPoint(x,y);
        if( /*condition for changing the radius*/ )
        {
            R = R*2; // or any change you want to do for R
        }
    }

如果使用 两个 for 循环可能最简单:一个 for 循环以特定半径绘制圆,另一个 for 循环包含前一个 for 循环,增加半径。

int numCircles = 3;

//This for loop increases the radius and draws the circle with another for loop
for (int j = 0; j < numCircles; j++)
{
  //This for loop draws the actual circle
  for (int i = 0; i < numpoints; i++)
  {
    float x = cos(angleinc * i) * radius;
    float y = sin(angleinc * i) * radius;

    ellipse(x, y, 4, 4);
  }
  //(add code here that increases the radius)
}

如果您将问题分解成更小的步骤,您的运气会更好。第一步是创建一个绘制单个 "ring" 较小圆圈的函数。你已经完成了这一步,你需要做的就是将它分成自己的功能:

void drawCircle(int outerRadius, int innerRadius) {

  int numpoints = 10;
  float angleinc = 2 * PI / numpoints;
  for (int i = 0; i < numpoints; i++) {
    float x = cos(angleinc * i) * outerRadius;
    float y = sin(angleinc * i) * outerRadius;

    ellipse(x, y, innerRadius, innerRadius);
  }
}

然后,要绘制一组越来越大的环,只需多次调用该函数即可:

  drawCircle(50, 8);
  drawCircle(75, 12);
  drawCircle(100, 16);

你可以将其压缩成for loop:

  for(int i = 2; i <= 4; i++){
    drawCircle(25*i, 4*i);
  }

整个事情看起来像这样:

void setup() {
  size (400, 400);
}

void draw() {
  background(255);
  noStroke();
  fill(0);
  smooth();
  translate(width/2, height/2);

  for(int i = 2; i <= 4; i++){
    drawCircle(25*i, 4*i);
  }
}

void drawCircle(int outerRadius, int innerRadius) {

  int numpoints = 10;
  float angleinc = 2 * PI / numpoints;
  for (int i = 0; i < numpoints; i++) {
    float x = cos(angleinc * i) * outerRadius;
    float y = sin(angleinc * i) * outerRadius;

    ellipse(x, y, innerRadius, innerRadius);
  }
}

这只是一个示例,您必须仔细研究数字以使其看起来完全符合您的要求,但过程是相同的:将您的问题分解为更小的步骤,分离这些步骤进入做一件事的函数,然后调用这些函数来完成你的总体目标。