I have an assignment that states the following–Create a program using nested for statements that will produce 5 rows, 7 in each column of diagonals as shown. You will need to size the frame 450 x 450. Determine your coordinates using that scale. Use a spreadsheet program to help you decide the x and y locations to draw the diagonals. You will need to create global variables to use in the line function. The nested for statement will loop through rows and columns.
I came up with the code to use, but i need to know how i can write the following code with nested for loops?
Also, each row of lines needs to be a different color…
Since the universe frowns upon giving complete solutions…
you already have much of what you need. maybe it helps if you think about painting pixels in a digital image, one after another. lets say the image is 20 x 20 pixels in size.
lets say you start at the upper left pixel. you will paint one pixel after another until you reach the end.
you can think of it as a single loop, counting from point #0 to point #399.
your “loop” would be:
i = 0
paint the pixel black
add 1 to i
repeat (until i = 399)
but you can also say: I paint each row - within each row I paint each pixel
i = 0 <- thats the rows
j = 0 <- thats the pixels in a certain row
paint the pixel black
add 1 to j
repeat (until j = 19)
add 1 to i (until i = 19)
repeat
that is what is missing in you approach imho
it is a loop within a loop
that’s what “they” mean when they say: use a nested loop
if you manage the line, then you will probably use that global variable for the co…
please help me figure out what to do
i’m trying to make 5 rows of diagonal lines…
each row has to be a different color…
i know i need to increase y1 and y2 by 60…
i just can’t figure out how to write it…
i can get the first row
size(450, 450);
strokeWeight(8);
stroke(random(0,255), random(0,255), random(0,255));
int i=20;
int y1=40;
int y2=90;
for(i=20; i<400; i +=60)
{
for(i=20; i<400; i +=60)
{
line(i, y1, i+60, y2);
}
}