ICM Week3 Looping

Youmingzhang
2 min readSep 30, 2020

--

looping…

This week I learned how to use loops. Looping is a thing made program powerful. Even though I learn loop before, I can never say that I am comfortable using it because when designing algorithms, how to loop is a challenging thing as always. I decided to draw a rose pattern since the mathematical formulas existing in our nature and our nature using it with repetitions to create beautiful roses.

Rose formula Wiki

Writing this formula in P5 is actually simpler than learning how our nature did it. For my formula to work, I need numerator — “n” and denominator — “d”. These two variable defines the pattern of rose. Then the rest of it just calculation and looping.

k = n / d

r = cos(ka) “a” is the angle rotated.

x = r*cos(a) and y = r*sin(a);

With my point x,y I can add them into a custom shape function and create the curved line for me. My loop starts from 0 to two pi times d. I also need a scale number to scale my points to make them visible and apart.

for(var a=0;a<TWO_PI*d;a+=0.01){
r = 360*cos(k*a);
x = r*cos(a);
y = r*sin(a);

curveVertex(x,y);
}

I created a slider for n and d value so that we can see different rose pattern for different n and d values. Here are some roses I drew.

Looping is powerful with the correct formula. Replicating some patterns in our nature using P5 is very interesting.

--

--

No responses yet