-
-
Save gretaf007/d865a92f6254e5e85df59942f460056a to your computer and use it in GitHub Desktop.
size(600,600) | |
background(255) | |
quad(0,0, 45,25, 45,95, 0,70) | |
quad(45,25, 90,0, 90,70, 45,95) |
Hi Greta. Great questions, and you're making perfect sense. Sorry to hear that you're confused. I'll make sure to leave time to review loops this week since we were a bit rushed last week.
As for your question: You can definitely create a loop that draws both of these shapes at once. As I tried to explain in the lecture notes, you can put any arbitrary commands inside a loop, so that means you could draw many things. All of the commands inside the loop get repeated together. As a first step, putting both of your shapes inside a loop would look something like this:
i = 0
while i <= 9:
quad(0,0, 45,25, 45,95, 0,70)
quad(45,25, 90,0, 90,70, 45,95)
i = i + 1
That will repeat 10 times, for i
equal to 0, 1, 2, 3, etc up to 9. But the problem with that is that all the x,y coordinates inside the loop are still hard-coded, so that as the loop repeats, those two shapes will get drawn in the exact same spot all 10 times.
To change this (and in response to your question about how to space the shapes out from each other) you need to use the looping variable somehow inside the loop when you are drawing your shapes. In the below snippet, I'm adding the variable i
to every x coordinate. Every time the loop repeats, the i
variable will be incremented by 1, so the two shapes will be scooted over to the right by one pixel. Try stepping through this mentally for 2-3 repetitions, then copy/paste into Processing and run it:
i = 0
while i <= 9:
quad(i, 0, 45+i, 25, 45+i, 95, i, 70)
quad(45+i,25, 90+i,0, 90+i,70, 45+i,95)
i = i + 1
Running that, it looks a little weird. But if you look closely, you can see that it's actually drawn each shape 10 times, but only one pixel apart. To space things out a little more there are a few ways you could do it. One way would be to change the start, stop, and increment of your looping variable. Again, step through this code mentally for a few repetitions, then copy/paste into Processing and run it:
i = 0
while i <= 400:
quad(i, 0, 45+i, 25, 45+i, 95, i, 70)
quad(45+i,25, 90+i,0, 90+i,70, 45+i,95)
i = i + 100
In that code snippet, the variable i
gets increased by 100
at the end of the loop before the loop repeats. So the first time the shapes are drawn, i
will be 0, next time i
will be 100, then 200, and so on – the loop will repeat as long as i <= 400
, so the values for i
will be: 0, 100, 200, 300, 400. And since I'm adding i
to every x coordinate in the quad()
commands, the shapes will be scooted over 100 pixels to the right each time the loop repeats.
As to the last part of your question, let's think about the way that you get your shapes to repeat not only in a line horizontally but also vertically too. Look at this code, same as above, but with the quad()
commands copy/pasted two additional times:
i = 0
while i <= 400:
quad(i, 0, 45 + i, 25, 45 + i, 95, i, 70)
quad(45 + i, 25, 90 + i, 0, 90 + i, 70, 45 + i, 95)
# shifted down by 100 pixels
quad(i, 100, 45 + i, 125, 45 + i, 195, i, 170)
quad(45 + i, 125, 90 + i, 100, 90 + i, 170, 45 + i, 195)
# shifted down by 200 pixels
quad(i, 200, 45 + i, 225, 45 + i, 295, i, 270)
quad(45 + i, 225, 90 + i, 200, 90 + i, 270, 45 + i, 295)
i = i + 100
If you look closely at the second pair of quad()
s, I've added 100 to each y value. And if you look at the third pair, I've added 200 to each y value. Try running that and see what it looks like.
Now let's say instead of 3 rows, you wanted 5 or 10 or 100. You could copy/paste the quad()
commands and adjust the y values, but this would be tedious. Adding this type of repetition is what you would use a loop for! But we're already in a loop! But that's OK: you can put a loop inside a loop, and this is what I explain in the lecture notes is called a nested loop.
Read carefully through this code, but only focus on the inner loop, where my comments say "From here ... to here". Step through it mentally. What will the values of j
be?
i = 0
while i <= 400:
# inner loop. From here:
j = 0
while j <= 200:
quad(i, j, 45 + i, 25+j, 45 + i, 95+j, i, 70+j)
quad(45 + i, 25+j, 90 + i, j, 90 + i, 70+j, 45 + i, 95+j)
j = j + 100
# to here.
i = i + 100
j
starts at 0, then gets increased by 100, and the loop repeats, then it gets increased again to 200, then loop repeats again, then it gets increased to 300, but if j
is 300, does j <= 200
evaluate to True
? No, so the loop exits. So if this inner loop repeats 3 times for j
equal to 0, 100, and 200, then it is precisely the same as the previous code snippet with three pairs of quad()
commands. That means that each time this inner loop runs, it draws your shapes three times, in a vertical column. But remember that the outer loop repeats 5 times, using i
to scoot the shapes over to the right each time. So in other words, each time the outer loop runs, the inner loop runs 3 times. The inner loop generates one column of 3 things, and the outer loop generates 5 repetitions of that.
So now you have a nested loop that draws a grid of shapes, and you can adjust the start, stop, and increment of each loop to experiment with different amounts of repetition.
Did you get this far? Did that clarify anything? Let me know!
@rors
If I wanted to loop these shapes, could I create a loop for both of them at once? or would I do two separate loops for each shape. I am also unclear about how the spacing works when you're making a loop. If it is the latter, how do I make the shapes spaced from each other and repeat not only in a line horizontally but vertically too?
I hope that makes sense, I am trying to make sense of the lecture notes, but I am a little confused