Nested for loops---help please!

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…

size(450, 450);
strokeWeight(8);
fill(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)
{
line(i, y1, i+60, y2);
line(i, y1+60, i+60, y2+60);
line(i, y1+120, i+60, y2+120);
line(i, y1+180, i+60, y2+180);
line(i, y1+240, i+60, y2+240);
}

Hello,

I see a pattern here that could be in a loop:
0, 60, 120, 180, 240

:)

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… :slight_smile:

2 Likes

Hello,

More resources:

6.3- For Loop - Processing Tutorial - YouTube

6.6- Nested Loops - Processing Tutorial - YouTube

https://processing.org/reference/for.html

:)

I’ve looked at all of those and still can’t figure out how to write the code correctly

Keep working at it.

  • There is a simple example in the reference I provided to plot points.
  • Build on that example and draw lines (diagonals?).
  • Then modify to meet the requirements of you homework assignment.
  • And then try to do it from scratch without an example.

Rename variables in loops to x and y or row and col if that helps.

This is an achievable exercise.

:)

image

i did that, but i am still only getting one row of diagonal lines
int y1=40;
int y2=90;

for(int i=20; i<400; i = i+60)
{
for(int j=0; j<400; j = j+60)

{
line(i, y1, i+60, y2);
}
}

You have to use both i and j

Think of a chess board where you have rows and columns. That’s why you use a nested for loop.

Please format your code:
https://discourse.processing.org/faq#format-your-code

y1 and y2 are not changing so will always be writing over the same row.

There is an example here for you to think about:
https://processing.org/reference/for.html

println() is useful to see the values of variables in your code:
https://processing.org/reference/println_.html

:)

where do i use j in my code?

There is an example here for you to think about and it uses i and j:
https://processing.org/reference/for.html

:)

yeah, i get that…but i’m doing lines and not points…that doesn’t work for me

Make some experiments with your code, play with it

Run it, change it, run it again

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);
  }
}

A point is one end of a line.

The reference page for line() can be found here along with tutorials and examples:
https://processing.org/

:)

Here you calculate your line.

Here you have to use i AND j

Think of a chess board.

Play with this line, change it and run the code and see what happens

a bunch of mess! still cant figure it out

1 Like

solved now

thanks to all

Chrisir

1 Like