# Join crosses with random lines

Hi everyone, I have a simple question but which one I’m blocking on. I would like to join by a line the center of two crosses at random.

I’m still new to processing.

I’ve understood that

line (x + 23, y + 23, random (int x), random (int y));

does not work because from what I understand the int cannot be put into coordinates directly.

``````
int x, y;

void setup() {

size(1122, 793);
background(255);

}

void draw() {

for ( int y = 20; y <=790; y += 20) {       // Position des point en y , largeur total du positionnement en  y, espace entre deux point en y
for ( int x = 20 ; x <= 1100; x += 20){   // Position des point en x , largeur total du positionnement en  x, espace entre deux point en

line(x, y, x+6, y+6);  // Croix
line(x+6, y, x, y+6);  // Croix
}
}

// Draw gray box
stroke(153);

line(x+23, y+23, , );

}
``````

x1 = random(width);
y1 = random…
x2 = random(width);
y2 = random…

line(x1, y1,x2,y2);

write a function cross and call it from draw()

cross (x1,y1);
cross (x2,y2);

The function cross

(not tested)

``````void cross (float x, float y) {

line(x, y, x+6, y+6);  // Croix line \
line(x+6, y, x, y+6);  // Croix  /

}

``````

I tried your code and I have a line with a cross at each end. Thank you.

I don’t know if I misinterpreted your code or misspoken. I have already done all the crosses, I just wanted to know how to use the data to create random lines. I put a picture to help understand better.

The lines were added on the screenshot This part?

``````
x1 = random(width);
y1 = random…
x2 = random(width);
y2 = random…

line(x1, y1,x2,y2);
``````

Yes I understand the random classic
I just want to understand how to put in the random this to take one of the points like X1 and another like X2 from those already existing

``````for ( int y = 20; y <=790; y += 20) {       // Position des point en y , largeur total du positionnement en  y, espace entre deux point en y
for ( int x = 20 ; x <= 1100; x += 20){   // Position des point en x , largeur total du positionnement en  x, espace entre deux point en

//line(x, y, x+6, y+6);  // Croix
//line(x+6, y, x, y+6);  // Croix

point(x+3, y+3);
``````

Hi @Dimdimtri,

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

Thanks Josephh ( again ) !

For the part of the crosses I understood everything and much simpler thank you.

However, I still don’t understand the part of the line. I use the lines you made but I can’t see anything.

To refocus the lines does not tell me anything I think I already know;)

Please post your entire code, so we can check it. Joseph is very experienced

1 Like

Thanks guys

``````int crossSpacing = 20;
int crossSize = 6;

void setup() {

size(1122, 793);
background(255);

}

void draw() {

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

}

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

Hello,

since you started the for loop not by 0 but with the spacing (`crossSpacing`), you need to add the spacing when calculating the random values

``````  // Choose random coordinates inside that space
int x1 = int(random(xSize)) * gridSpacing+gridSpacing;
int y1 = int(random(ySize)) * gridSpacing+gridSpacing;

int x2 = int(random(xSize)) * gridSpacing+gridSpacing;
int y2 = int(random(ySize)) * gridSpacing+gridSpacing;
``````

Also you need to add `crossSize/2` to the values (to go to the center of the “X” and not to its upper left corner); I haven’t done this yet.

Warm regards,

Chrisir

Full Sketch:

``````int crossSpacing = 20;
int crossSize = 6;

void setup() {

size(1122, 793);
background(255);
}

void draw() {
noLoop();
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);
}
}
randomLineOnGrid(crossSpacing);
randomLineOnGrid(crossSpacing);
randomLineOnGrid(crossSpacing);
randomLineOnGrid(crossSpacing);
}

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+gridSpacing;
int y1 = int(random(ySize)) * gridSpacing+gridSpacing;

int x2 = int(random(xSize)) * gridSpacing+gridSpacing;
int y2 = int(random(ySize)) * gridSpacing+gridSpacing;

// Display the line
line(x1, y1, x2, y2);
}
//
``````
2 Likes

Yes, if you want to be even more modular, you can encapsulate the code in functions.

``````// Global variables
int crossSpacing = 20;
int crossSize = 6;

void setup() {
size(1122, 793);
}

void draw() {
// White background
background(255);

// Display the crosses
displayCrosses();

// Display 10 lines
displayRandomLines(10);

// Break the loop, it's not interactive here
noLoop();
}

/*
Display the crosses
*/
void displayCrosses() {
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);
}
}
}

/*
Draw a random line between crosses
*/
void randomLineOnGrid() {
// The size of the grid on x and y
int xSize = width / crossSpacing;
int ySize = height / crossSpacing;

// Choose random coordinates inside that space
// Add half of the size of a cross
int x1 = int(random(xSize)) * crossSpacing + crossSize / 2;
int y1 = int(random(ySize)) * crossSpacing + crossSize / 2;

int x2 = int(random(xSize)) * crossSpacing + crossSize / 2;
int y2 = int(random(ySize)) * crossSpacing + crossSize / 2;

// Display the line
line(x1, y1, x2, y2);
}

/*
Display n random lines by calling the previous function
*/
void displayRandomLines(int n) {
for (int i = 0; i < n; i++) {
randomLineOnGrid();
}
}

``````

I commented the code, so I hope it’s more clear!

If you are not really sure how to use functions, check this link :

Thanks guys !

1 Like

Hi guys! @josephh @Chrisir
I took a nice white sheet of paper and I took all the code from zero! And I say a big thank you, because in the end I learned how to put my code (and the next ones) in order. I learned some basics that I was missing. Thank you again to both of you! 1 Like

Last question for which I can’t find the variable, where I have to modify the code to delimit the lines in the crossroads area?

I tried to modify the

``````  int xSize = width / crossSpacing;
int ySize = height / crossSpacing;
``````

but nothing

Do you mean restrict the area in which the random lines can be created?

If so, you can restrict the range of a random coordinates like this :

``````// the random value will be limited by 5
// So you'll have a margin of 5 crosses
int x1 = int(random(5, xSize - 5)) * crossSpacing + crossSize / 2;
``````
1 Like

Thanks, I don’t know if it’s luck or not, but thinking about it a bit I found:

``````  int x1 = int(random(1, xSize)) * crossSpacing + crossSize / 2;
int y1 = int(random(1, ySize)) * crossSpacing + crossSize / 2;

int x2 = int(random(1, xSize)) * crossSpacing + crossSize / 2;
int y2 = int(random(1, ySize)) * crossSpacing + crossSize / 2;
``````

and for the moment it has always remained on a cross!

Thanks again 1 Like

Yes you are right, it’s because when we are displaying the crosses, we start the for loop at `crossSpacing`. (to add a gap between the borders of the window and the crosses)

Then if you generate a random number between `[1, xSize[` for example, the position will always be shifted by 1 space.

1 Like

Hello josephh, I wanted to ask you a question. You know (and I think so), how to do a random but only use a cross once. to avoid over-exposure? thank you in advance.

Hi again,

Do you mean avoid crossing lines?

Sorry, for the late reply, long weekend.

I modified the original code a little, I use it as a base code for my personal exercises. Now I no longer make lines but rectangles. And I was wondering if it was possible with the random function once a number has been chosen to exclude it to avoid taking it twice (in this case not to choose the same cross twice).

``````// Global variables for points

int crossSpacing = 82;
int crossSize = 6;

int rectangleSize = 82;

float a = 13.66;
float b = 10.87;

// Export Svg

//import processing.svg.*;

void setup() {

size(1122, 793);
smooth(1);
//beginRecord(SVG, "filename.svg");

}

void draw() {
// White background
background(255);

// Display the crosses
displayCrosses();

// Display n rectangleV
displayRandomrectangleV(10);

// Display n rectangleH
displayRandomrectangleH(10);

// Display n FIRSTCERCLE
displayRandomFIRSTCERCLE(10);

// Break the loop, it's not interactive here
noLoop();
}

/*
Display the crosses
*/
void displayCrosses() {
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);
}
}
}

/*
Draw a random rectengleV between crosses
*/
void randomrectangleVGrid() {
// The size of the grid on x and y
int xSize = width / crossSpacing;
int ySize = height / crossSpacing;

// Choose random coordinates inside that space
// Add half of the size of a cross

int x1 = int(random(1,xSize-1)) * crossSpacing + crossSize / 2;
int y1 = int(random(1,ySize-1)) * crossSpacing + crossSize / 2;

// Display the line

for (float i = 0; i < 90; i = i+a) {
line(x1 + i, y1, x1 + i, y1 + rectangleSize );

}

noFill();

}

/*
Display n random rectangleV by calling the previous function
*/
void displayRandomrectangleV(int n) {
for (int i = 0; i < n; i++) {
randomrectangleVGrid();

}
}

/*
Draw a random rectengleH between crosses
*/
void randomrectangleHGrid() {
// The size of the grid on x and y
int xSize = width / crossSpacing;
int ySize = height / crossSpacing;

// Choose random coordinates inside that space
// Add half of the size of a cross

int x2 = int(random(1,xSize-1)) * crossSpacing + crossSize / 2;
int y2 = int(random(1,ySize-1)) * crossSpacing + crossSize / 2;

// Display the line

for (float i = 0; i < 90; i = i+a) {
line(x2, y2 + i, x2 + rectangleSize, y2 + i);

}

noFill();

}

/*
Display n random rectangleH by calling the previous function
*/
void displayRandomrectangleH(int n) {
for (int i = 0; i < n; i++) {
randomrectangleHGrid();

}
}

/*
Draw a random firstcercle between crosses
*/
void randomFIRSTCERCLEGrid() {
// The size of the grid on x and y
int xSize = width / crossSpacing;
int ySize = height / crossSpacing;

// Choose random coordinates inside that space
// Add half of the size of a cross

int x3 = int(random(1,xSize-1)) * crossSpacing + crossSize / 2;
int y3 = int(random(1,ySize-1)) * crossSpacing + crossSize / 2;

// Display the line

//rect(x3, y3, rectangleSize, rectangleSize);

for (float i=2; i<13; i+=2) {

}

noFill();

}

/*
Display n random firstcercle by calling the previous function
*/
void displayRandomFIRSTCERCLE(int n) {
for (int i = 0; i < n; i++) {
randomFIRSTCERCLEGrid();

}
}

``````