Hi @Dimdimtri,
Few advices on your code :
Most of the time, you don’t want to have hard coded values inside of your code like the spacing between the crosses (here 20
) or the size of the crosses specially when they are referenced multiple times. Instead, use variables and this is what they are meant for.
Just define them at the top of your program :
int crossSpacing = 20;
int crossSize = 6;
The pros of this method is that if you need to change the size of the crosses, just do it at the beginning of the sketch instead of changing it in 4 places (and it could be worse…)
So you can use them in your loop :
for ( int y = crossSpacing; y <= height - crossSpacing; y += crossSpacing) {
for ( int x = crossSpacing; x <= width - crossSpacing; x += crossSpacing) {
line(x, y, x + crossSize, y + crossSize);
line(x + crossSize, y, x, y + crossSize);
}
}
And then to draw random lines between the crosses, you can choose two random points on the grid (their x and y coordinates are multiple of crossSpacing
) and draw a line :
void randomLineOnGrid(int gridSpacing) {
// The size of the grid on x and y
int xSize = width / gridSpacing;
int ySize = height / gridSpacing;
// Choose random coordinates inside that space
int x1 = int(random(xSize)) * gridSpacing;
int y1 = int(random(ySize)) * gridSpacing;
int x2 = int(random(xSize)) * gridSpacing;
int y2 = int(random(ySize)) * gridSpacing;
// Display the line
line(x1, y1, x2, y2);
}
You can then call this function however you want (that’s also why we write functions!)
You will probably notice that the lines are not centered on the crosses because you are drawing the crosses from the upper left corner but you can fix this alone
Also another way to do this is to store the origin points of the crosses then choose two random elements in that array and display the line