Connected Circles

Hey,

this is a task that I have to solve for a course at uni. It’s processing for beginners. We use Processing 3.

What do I do to connect each circle at y = 100 with those at y = 550? Apparently, a nested loop is a solution, but I have already tried and it didn’t work out for me.

This is what I have gotten so far. Below you can see a picture of what it is supposed to look like.

Appreciate your help.

Code:

size (1000,650);
background (255);
int spacing = 100;
int diameter = 50;
int x1 = -50;
int y1 = 100;
int y2 = 550;
int x2 = x1 += spacing;
ellipseMode (CENTER);
for (x1 = 50; x1 < width; x1 += spacing)
{
  fill (120); ellipse (x1,y1,diameter,diameter);
  ellipse (x1,y2,diameter,diameter);
  line (x1,y1,x2,y2);
  line (x2,y1,x1,y2);
}
1 Like

Please, when writing or editing a comment, use a little </> button above the text field to format your code into monospace font, like this one.

Anyways, I think it makes sense to get rid of int x1 and int x2 definitions, and replace for(x1 =.. with for(int x1 =.., and then put another for loop inside of that one for(int x2=... That second for loop is a nested one.
Something like this:

size (1000, 650);
background (255);
int spacing = 100;
int diameter = 50;
int y1 = 100;
int y2 = 550;
ellipseMode (CENTER);
for (int x1 = 50; x1 < width; x1 += spacing)
{
  fill (120);
  ellipse (x1, y1, diameter, diameter);
  ellipse (x1, y2, diameter, diameter);
  for (int x2 = 50; x2 < width; x2 += spacing) {
    line (x1, y1, x2, y2);
  }
}
2 Likes

Thanks so much, Architector! However, seeing a possible solution, I realize I should have been able to figure that out, am a bit disappointed with myself haha. Also thank you for the advice on the formatting, this is my first post, so I didn’t know. Your help is much appreciated!!! Cheers :v:

1 Like