简体   繁体   English

在处理中使用Java绘制螺旋

[英]Drawing Spiral Using Java in Processing

I have a Java program written in Processing I made that draws a spiral in processing but I am not sure how some of the lines of code work.我有一个用 Processing 编写的 Java 程序,它在处理中绘制了一个螺旋,但我不确定某些代码行是如何工作的。 I wrote them based on a tutorial.我是根据教程写的。 I added comments in capital letters to the lines I do not understand .我在我不明白的行中添加了大写字母的注释。 The comments in lowercase are lines that I do understand .小写的注释是我理解的行 If you understand how those lines work, please explain in very simple terms!如果您了解这些线路的工作原理,请用非常简单的术语进行解释! Thank you so much.非常感谢。

void setup()
  {
    size(500,500);
    frameRate(15);
  }

  void draw()
  { 
    background(0); //fills background with black
    noStroke(); //gets rid of stroke

    int circlenumber = 999;// determines how many circles will be drawn 
    float radius = 5; //radius of each small circle
    float area = (radius) * (radius) * PI; //area of each small circle
    float total = 0; //total areas of circles already drawn
    float offset = frameCount * 0.01; //HOW DOES IT WORK & WHAT DOES IT DO

    for (int i = 1; i <= circlenumber; ++i) { // loops through all of the circles making up the pattern

      float angle = i*19 + offset; //HOW DOES IT WORK & WHAT DOES IT DO
      total += area; // adds up the areas of all the small circles that have already been drawn
      float amplitude = sqrt( total / PI ); //amplitude of trigonometric spiral
      float x = width/2 + cos(angle) * amplitude;//HOW DOES IT WORK & WHAT DOES IT DO

      float hue = i;//determines circle color based on circle number
      fill(hue, 44, 255);//fills circle with that color

      ellipse(x, 1*i, radius*2, radius*2); //draws circle
    }
  }

Essentially what this is doing is doing a vertical cosine curve with a changing amplitude.本质上,这是在做一条幅度变化的垂直余弦曲线。 Here is a link to a similar thing to what the program is doing.这是指向与程序正在执行的操作类似的事情的链接。 https://www.desmos.com/calculator/p9lwmvknkh https://www.desmos.com/calculator/p9lwmvknkh

Here is an explanation of this different parts in order.以下是按顺序对这些不同部分的解释。 I'm gonna reference some of the variables from the link I provided:我将从我提供的链接中引用一些变量:

float offset = frameCount * 0.01

What this is doing is determining how quickly the cosine curve is animating.这样做的目的是确定余弦曲线动画的速度。 It is the "a" value from desmos.它是来自 desmos 的“a”值。 To have the program run, each ellipse must change its angle in the cosine function just a little bit each frame so that it moves.为了让程序运行,每个椭圆必须在余弦函数中在每一帧中稍微改变它的角度,以便它移动。 frameCount is a variable that stores the current amount of frames that the animation/sketch has run for, and it goes up every frame, similar to the a-value being animated. frameCount是一个变量,用于存储动画/草图运行的当前帧数,并且每帧都会增加,类似于正在设置动画的 a 值。

for (int i = 1; i <= circlenumber; ++i) {
      float angle = i*19 + offset;

This here is responsible for determining how far from the top the current ellipse should be, modified by a stretching factor.这在这里负责确定当前椭圆应该离顶部多远,由拉伸因子修改。 It's increasing each time so that each ellipse is slightly further along in the cosine curve.它每次都在增加,因此每个椭圆在余弦曲线上都稍微远一些。 This is equivalent to the 5(y+a) from desmos.这相当于来自 desmos 的 5(y+a)。 The y-value is the i as it is the dependent variable. y 值是 i,因为它是因变量。 That is the case because for each ellipse we need to determine how far it is from the top and then how far it is from the centre.之所以如此,是因为对于每个椭圆,我们需要确定它离顶部有多远,然后离中心有多远。 The offset is the a-value because of the reasons discussed above.由于上述原因,偏移量是 a 值。

float x = width/2 + cos(angle) * amplitude

This calculates how far the ellipse is from the centre of the screen (x-centre, y value is determined for each ellipse by which ellipse it is).这将计算椭圆距屏幕中心的距离(x 中心,y 值由每个椭圆确定为哪个椭圆)。 The width/2 is simply moving all of the ellipses around the centre line. width/2 只是围绕中心线移动所有椭圆。 If you notice on Desmos, the center line is y-axis.如果您在 Desmos 上注意到,中心线是 y 轴。 Since in Processing, if something goes off screen (either below 0 or above width ), we don't actually see it, the tutorial said to offset it so the whole thing shows.因为在 Processing 中,如果有东西离开屏幕(低于 0 或高于width ),我们实际上并没有看到它,教程说要抵消它,所以整个事情都显示出来了。 The cos(angle)*amplitude is essentially the whole function on Desmos. cos(angle)*amplitude 本质上是 Desmos 上的整个函数。 cos(angle) is the cosine part, while amplitude is the stuff before that. cos(angle) 是余弦部分,而幅度是之前的东西。 What this can be treated as is essentially just a scaled version of the dependent variable.这可以被视为本质上只是因变量的缩放版本。 On desmos, what I'm doing is sqrt(-y+4) while the tutorial essentially did sqrt(25*i) .在 desmos 上,我正在做的是sqrt(-y+4)而教程基本上是sqrt(25*i) Every frame, the total (area) is reset to 0. Every time we draw a circle, we increase it by the pi * r^2 (area of circle).每一帧,总(面积)都被重置为 0。每次我们画一个圆时,我们将它增加 pi * r^2(圆的面积)。 That is where the dependent variable (i) comes in. If you notice, they write float amplitude = sqrt( total / PI );这就是因变量 (i) 的用武之地。如果您注意到,他们会写成float amplitude = sqrt( total / PI ); so the pi from the area is cancelled out.所以该区域的 pi 被抵消了。

One thing to keep in mind is that the circles aren't actually moving down, it's all an illusion.要记住的一件事是,圆圈实际上并没有向下移动,这完全是一种错觉。 To demonstrate this, here is some modified code that will draw lines.为了证明这一点,这里有一些修改后的代码来画线。 If you track a circle along the line, you'll notice that it doesn't actually move down.如果你沿着这条线跟踪一个圆圈,你会注意到它实际上并没有向下移动。

void setup()
  {
    size(500,500);
    frameRate(15);
  }

  void draw()
  { 
    background(0); //fills background with black
    noStroke(); //gets rid of stroke

    int circlenumber = 999;// determines how many circles will be drawn 
    float radius = 5; //radius of each small circle
    float area = (radius) * (radius) * PI; //area of each small circle
    float total = 0; //total areas of circles already drawn
    float offset = frameCount * 0.01; //HOW DOES IT WORK & WHAT DOES IT DO

    for (int i = 1; i <= circlenumber; ++i) { // loops through all of the circles making up the pattern

      float angle = i*19 + offset; //HOW DOES IT WORK & WHAT DOES IT DO
      total += area; // adds up the areas of all the small circles that have already been drawn
      float amplitude = sqrt( total / PI ); //amplitude of trigonometric spiral
      float x = width/2 + cos(angle) * amplitude;//HOW DOES IT WORK & WHAT DOES IT DO

      float hue = i;//determines circle color based on circle number
      fill(hue, 44, 255);//fills circle with that color

      stroke(hue,44,255);
      if(i%30 == 0)
          line(0,i,width,i);

      ellipse(x, i, radius*2, radius*2); //draws circle
    }
  }

Hopefully this helps clarify some of the issues with understanding.希望这有助于澄清一些理解问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM