Created
October 5, 2020 01:21
-
-
Save gretaf007/d865a92f6254e5e85df59942f460056a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
size(600,600) | |
background(255) | |
quad(0,0, 45,25, 45,95, 0,70) | |
quad(45,25, 90,0, 90,70, 45,95) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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, thei
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: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:
In that code snippet, the variable
i
gets increased by100
at the end of the loop before the loop repeats. So the first time the shapes are drawn,i
will be 0, next timei
will be 100, then 200, and so on – the loop will repeat as long asi <= 400
, so the values fori
will be: 0, 100, 200, 300, 400. And since I'm addingi
to every x coordinate in thequad()
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: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?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 ifj
is 300, doesj <= 200
evaluate toTrue
? No, so the loop exits. So if this inner loop repeats 3 times forj
equal to 0, 100, and 200, then it is precisely the same as the previous code snippet with three pairs ofquad()
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, usingi
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!