【问题标题】:How to do: for looping + increasing of radius each loop?怎么做:for循环+每个循环的半径增加?
【发布时间】:2015-08-30 09:14:14
【问题描述】:

如何创建一个干净简单的代码,在较大的圆内创建一个点/点圆?或类似的东西(我无法发布我想要的图片,抱歉)。有人告诉我尝试在我的代码外部使用 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);
}

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

【问题讨论】:

  • 预期的半径增加是多少。你不能只添加 radius = radius*x for where x 是 for 循环中的增量
  • 为什么不能发布你想要的图片?只是在油漆中模拟它。到目前为止,每个人都在猜测你想要什么。

标签: loops for-loop processing radius


【解决方案1】:

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

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);
  }
}

这只是一个示例,您必须使用数字使其看起来与您想要的完全一样,但过程是相同的:将您的问题分解为更小的步骤,将这些步骤隔离为函数做一件事,然后调用这些函数来完成你的总体目标。

【讨论】:

    【解决方案2】:

    我希望你的问题是正确的- 围绕原点的圆的公式是 x=Rcos(angle) y=Rsin(angle) 其中 angel 在 0 到 2*pi

    之间

    如果你想在点周围画圆,比如说在 (x',y') 周围,公式将是 x= x' + Rcos(angle) y= y' + r罪(角度)

    代码:

      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
            }
        }
    

    【讨论】:

    • 这对我来说没有任何意义 aviyaChe,我对处理很陌生。另外,我的处理程序说 x' 和 y' 的东西是不正确的。我想做的就是在原始集合中创建另一组点,所以就像原始点圈一样,我希望在比以前更小的内部添加另一个点圈 - 以尽可能最干净的方式,而不创建大量嵌套循环。跨度>
    • 这是一个伪代码,需要声明x'和y'。问题不清楚,能不能加个图什么的?
    • x' 和 y' 从上述方程中丢失。这是让我的公式发挥作用所需的缺失部分。
    【解决方案3】:

    如果您使用 两个 for 循环,这可能是最简单的:一个 for 循环在某个半径处绘制圆,另一个 for 循环中包含前一个 for 循环以增加半径。 p>

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 1970-01-01
      • 2015-12-26
      • 2013-08-04
      • 2014-08-21
      • 1970-01-01
      • 2019-11-03
      • 2020-10-09
      • 1970-01-01
      相关资源
      最近更新 更多