I need random circles and rectangles in a grid, Stuck in a problem I can't solve!

This is a big problem for me because I don’t know how to do it. I’ve got a small code, but the most important is that i have a grid. this grid is 60x40 and in this grid need to be random circles and rectangles about 10 of them each, there can’t also be 2 rects or circles in one spot and everything has to fit in the grid! can someone please help me out??

This is the code:

``````int vakje = 32;
void setup() {
size(1920,1080);
}

void draw() {
for (int x = 0; x < width; x+= width/60)
{
for (int y = 150; y < height; y+=height/46)
{
stroke(#000000);
fill(#FFFFFF);
rect(x,y,vakje-1,vakje-1);
}
noStroke();
fill(#1E90FF);
rect(0,1048,1920,10);
}
}
``````
1 Like

check out my no overlap code, you should be able to vary the shape you use.

https://www.openprocessing.org/sketch/769200

Things to note, this code does not adhere to a grid, it simply places points randomly and checks for overlap, if you want points to be locked to a grid you need to use the floor function along with a formula to calculate its nearest grid square.

2 Likes

yes, you can make random something, and check if there was one already and try again.
or you find a way where there is

• no same random

possible.

a random sequence to set N

• make a list ( 0 … n )
• shuffled
• use the first N of it
``````int items = 7; // to be placed randomly unique on a grid

int cols = 5;
int rows = 5;
int many = cols*rows;

IntList seq = new IntList(many);
int current=0;

boolean[][] myArr = new boolean[cols][rows];

void preset() {
for (int i = 0; i<many; i++) seq.append(i);
seq.shuffle();
for (int i = 0; i<many; i++) println(" i "+i+" "+seq.get(i));
}

void reset() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
myArr[i][j] = false;
}
}
}

void setitem(int rec) {
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++)
if ( seq.get(j+i*rows) == rec ) myArr[i][j] = true; //_ set acc shuffled list
}

void setitems(int item) {
for ( int i = 0; i < item; i++ ) setitem(i);
}

void checkitem() {
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++)
if ( myArr[i][j] ) {
println("item at i: "+i+", j: "+j);
// do what ever you want do here
}
}

void draw_grid() {
int x0 = 10, y0 = x0, w = 34, h = w, off = 2;
stroke(0);
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++) {
fill(0, 200, 0);
if ( myArr[i][j] ) fill(0, 0, 200);
rect(x0+i*(w+off), y0+j*(h+off), w, h);
}
}

void setup() {
size(200, 200);
reset();  //_____________________________ create empty array
preset(); //_____________________________ create a shuffled list
setitems(items); //______________________ set items
checkitem(); //__________________________ check what is set
}

void draw() {
background(200, 200, 0);
draw_grid(); //__________________________ green rect on yellow background w. blue items
}

``````

but that was only half of your problem?

and actually i used the memory in the shuffled list
and translated into the memory in the grid, what a waste…

SAME SAME BUT DIFFERENT

``````// https://discourse.processing.org/t/i-need-random-circles-and-rectangles-in-a-grid-stuck-in-a-problem-i-cant-solve/15061/3
// rev 02 change from i , j to i .. many
// erase the array

int cols = 4, rows = cols, many = cols*rows; //_________________________ GRID layout
int x0 = 10, y0 = x0, w = 0, h = w, off = 2; //_________________________ grid rect layout ( auto size see setup )
int itemsb = 3; //______________________________________________________ BLUE items to be placed randomly unique on a grid
IntList seqb = new IntList(many); //____________________________________ BLUE list

boolean diagp = true; //________________________________________________ print

void presetb() {
for (int i = 0; i < many; i++) seqb.append(i); //_____________________ make a list of numbers 0 .. many-1
seqb.shuffle(); //____________________________________________________ and shuffle them
for (int i = many-1; i >= itemsb; i--) seqb.remove(i); //_____________ keep only as many we need
seqb.sort(); //_______________________________________________________ optional sort in draw order
}

void diag_print() {
if ( diagp ) {
println("BLUE items: "+itemsb);
for (int i = 0; i < seqb.size(); i++) println("BLUE i "+i+" "+seqb.get(i));
}
}

void draw_grid() {
stroke(0);
for (int i = 0; i < many; i++) {
fill(0, 200, 0);
if ( seqb.hasValue(i) ) fill(0, 0, 200);
rect(x0+(i%cols)*(w+off), y0+(floor(i/cols))*(h+off), w, h); //_____ grid math
}
}

void setup() {
size(200, 200);
w = ( width - 2*x0 ) /cols - off; //__________________________________ auto size rects
h = ( height -2*y0 ) /rows - off;
presetb(); //_________________________________________________________ create a shuffled list BLUE
diag_print();
}

void draw() {
background(200, 200, 0);
draw_grid(); //_______________________________________________________ green rect on yellow background w. BLUE items
}

``````

please note how easy some things get when using IntList
https://processing.org/reference/IntList.html

1 Like

Thank you for your reply! I really appreciate it! but yes, as you sad this was only half of my problem, the next problem is that I also need a few random cirlces just the same as the random rectangles. I’ve made a purple rect which can be moved with the arrow keys. When the purple rect is on the circle or on the blue rect, it has to dissapear and be placed in another random place in the grid.

I now have this code under here, I’ve made a little changes to the script you send me.

I hope you can help me again since the help you just send me was really usefull!

``````int aantalSchatten = 10;
int breedteGrid = 60;
int lengteGrid = 40;
int heleGrid = breedteGrid*lengteGrid;
int huidig=0;
int duikerX = 0;
int duikerY = 150;
int duikerH = 21;
int duikerB = 31;
IntList seq = new IntList(heleGrid);
boolean[][] grid = new boolean[breedteGrid][lengteGrid];

void preset() {
for (int i = 0; i<heleGrid; i++) seq.append(i);
seq.shuffle();
for (int i = 0; i<heleGrid; i++);
}

void reset() {
for (int i = 0; i < breedteGrid; i++) {
for (int j = 0; j < lengteGrid; j++) {
grid[i][j] = false;
}
}
}

void setitem(int rec) {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( seq.get(j+i*lengteGrid) == rec ) grid[i][j] = true; //_ set acc shuffled list
}

void setitems(int item) {
for ( int i = 0; i < item; i++ ) setitem(i);
}

void checkitem() {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( grid[i][j] ) {
// do what ever you want do here
}
}

void draw_grid() {
int x0 = 0, y0 = 150, w = 31, h = 21, off = 1;
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++) {
fill(#FFFFFF);
if ( grid[i][j] ) fill(0, 0, 200);
rect(x0+i*(w+off), y0+j*(h+off), w, h);
}
}

void setup() {
size(1920, 1080);
reset();
preset();
setitems(aantalSchatten);
checkitem();
}

void draw() {
background(#1E90FF);
draw_grid();
fill(#551A8B);
rect(duikerX,duikerY,duikerB,duikerH);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
duikerY -= duikerH+1;
} else if (keyCode == DOWN) {
duikerY += duikerH+1;
} else if (keyCode == LEFT) {
duikerX -= duikerB+1;
} else if (keyCode == RIGHT) {
duikerX += duikerB+1;
}
}

// Gezorgd dat de duiker altijd binnen de grid blijft.
if (duikerX > width-32) duikerX = 0;
if (duikerX < 0) duikerX = width-32;
if (duikerY >height-55 ) duikerY = 150;
if (duikerY < 150) duikerY = height-72;
}
``````
1 Like

now that game is total different question i ignore,

but yes for the second step i recommend using the second code
and double the array and init
so you have a BLUE seqb list and a RED seqr list

``````int itemsb = 3; //______________________________________________________ BLUE items to be placed randomly unique on a grid
IntList seqb = new IntList(many); //____________________________________ BLUE list
int itemsr = 5; //______________________________________________________ RED  items to be placed randomly unique on a grid ! NOT OVER BLUE
IntList seqr = new IntList(many); //____________________________________ RED  list

``````

– where still the red might overwrite some blue,

``````void presetb() {
for (int i = 0; i < many; i++) seqb.append(i); //_____________________ make a list of numbers 0 .. many-1
seqb.shuffle(); //____________________________________________________ and shuffle them
for (int i = many-1; i >= itemsb; i--) seqb.remove(i); //_____________ keep only as many we need
seqb.sort(); //_______________________________________________________ optional sort in draw order
}

void presetr() {
for (int i = 0; i < many; i++) seqr.append(i); //_____________________ make a list of numbers 0 .. many-1
for (int i = many-1; i >= 0; i--) if ( seqb.hasValue(i) ) seqr.remove(i); //__________________________ must be different from BLUE
seqr.shuffle(); //____________________________________________________ and shuffle them
for (int i = seqr.size()-1; i >= itemsr; i--) seqr.remove(i); //______ keep only as many we need !! size is shorter
seqr.sort(); //_______________________________________________________ optional sort in draw order
}

``````

here it looks like:

so i wait to see your version of my second code with 2 arrays.
( ? as your game or just GRID start ? )

``````</> button from forum editor
``````

looks like
```
type or paste code here
```

1 Like

Hey, this is what I got when I put the two togheter, the only problem is, is that the itemsr and itemsb will both show the same color, I don’t know how to fix this. this is all the code i’ve got. Are you still be able to help me?

``````int breedteGrid = 60;
int lengteGrid = 40;
int heleGrid = breedteGrid*lengteGrid;
int huidig=0;
int duikerX = 0;
int duikerY = 150;
int duikerH = 21;
int duikerB = 31;
IntList seq = new IntList(heleGrid);
boolean[][] grid = new boolean[breedteGrid][lengteGrid];
int itemsb = 30; //______________________________________________________ BLUE items to be placed randomly unique on a grid
IntList seqb = new IntList(heleGrid); //____________________________________ BLUE list
int itemsr = 50; //______________________________________________________ RED  items to be placed randomly unique on a grid ! NOT OVER BLUE
IntList seqr = new IntList(heleGrid); //____________________________________ RED  list
void preset() {
for (int i = 0; i<heleGrid; i++) seq.append(i);
seq.shuffle();
for (int i = 0; i<heleGrid; i++);
}
void presetb() {
for (int i = 0; i < heleGrid; i++) seqb.append(i); //_____________________ make a list of numbers 0 .. many-1
seqb.shuffle(); //____________________________________________________ and shuffle them
for (int i = heleGrid-1; i >= itemsb; i--) seqb.remove(i); //_____________ keep only as many we need
seqb.sort(); //_______________________________________________________ optional sort in draw order
}

void presetr() {
for (int i = 0; i < heleGrid; i++) seqr.append(i); //_____________________ make a list of numbers 0 .. many-1
for (int i = heleGrid-1; i >= 0; i--) if ( seqb.hasValue(i) ) seqr.remove(i); //__________________________ must be different from BLUE
seqr.shuffle(); //____________________________________________________ and shuffle them
for (int i = seqr.size()-1; i >= itemsr; i--) seqr.remove(i); //______ keep only as many we need !! size is shorter
seqr.sort(); //_______________________________________________________ optional sort in draw order
}

void reset() {
for (int i = 0; i < breedteGrid; i++) {
for (int j = 0; j < lengteGrid; j++) {
grid[i][j] = false;
}
}
}

void setitem(int rec) {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( seq.get(j+i*lengteGrid) == rec ) grid[i][j] = true; //_ set acc shuffled list
}

void setitems(int item) {
for ( int i = 0; i < item; i++ ) setitem(i);
}

void checkitem() {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( grid[i][j] ) {
}
}

void draw_grid() {
int x0 = 0, y0 = 150, w = 31, h = 21, off = 1;
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++) {
fill(#FFFFFF);
if ( grid[i][j] ) fill(#FF0000);
rect(x0+i*(w+off), y0+j*(h+off), w, h);
}
}

void setup() {
size(1920, 1080);
reset();
preset();
setitems(itemsr);
setitems(itemsb);
checkitem();
}

void draw() {
background(#1E90FF);
draw_grid();
fill(#551A8B);
rect(duikerX,duikerY,duikerB,duikerH);
}

void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
duikerY -= duikerH+1;
} else if (keyCode == DOWN) {
duikerY += duikerH+1;
} else if (keyCode == LEFT) {
duikerX -= duikerB+1;
} else if (keyCode == RIGHT) {
duikerX += duikerB+1;
}
}

// Gezorgd dat de duiker altijd binnen de grid blijft.
if (duikerX > width-32) duikerX = 0;
if (duikerX < 0) duikerX = width-32;
if (duikerY >height-55 ) duikerY = 150;
if (duikerY < 150) duikerY = height-72;
}
``````
1 Like
``````

int breedteGrid = 60;
int lengteGrid = 40;
int heleGrid = breedteGrid*lengteGrid;
int huidig=0;
int duikerX = 0;
int duikerY = 150;
int duikerH = 21;
int duikerB = 31;
IntList seq = new IntList(heleGrid);

int[][] grid = new int[breedteGrid][lengteGrid];

int itemsb = 30; //______________________________________________________ BLUE items to be placed randomly unique on a grid
IntList seqb = new IntList(heleGrid); //____________________________________ BLUE list
int itemsr = 50; //______________________________________________________ RED items to be placed randomly unique on a grid ! NOT OVER BLUE
IntList seqr = new IntList(heleGrid); //____________________________________ RED list

void preset() {
for (int i = 0; i<heleGrid; i++)
seq.append(i);
seq.shuffle();
for (int i = 0; i<heleGrid; i++);  // ??????????????????????????????????
}

void presetb() {
for (int i = 0; i < heleGrid; i++)
seqb.append(i); //_____________________ make a list of numbers 0 … many-1

seqb.shuffle(); //____________________________________________________ and shuffle them

for (int i = heleGrid-1; i >= itemsb; i--) {
// seqb.remove(i); //_____________ keep only as many we need
}
seqb.sort(); //_______________________________________________________ optional sort in draw order
}

void presetr() {
for (int i = 0; i < heleGrid; i++)
seqr.append(i); //_____________________ make a list of numbers 0 … many-1

for (int i = heleGrid-1; i >= 0; i--)
if ( seqb.hasValue(i) ) {
//seqr.remove(i); //__________________________ must be different from BLUE
}

seqr.shuffle(); //____________________________________________________ and shuffle them

//for (int i = seqr.size()-1; i >= itemsr; i--)
//  seqr.remove(i); //______ keep only as many we need !! size is shorter

seqr.sort(); //_______________________________________________________ optional sort in draw order
}

void reset() {
for (int i = 0; i < breedteGrid; i++) {
for (int j = 0; j < lengteGrid; j++) {
grid[i][j] = 0;
}
}
}

void setitem(int rec, int value1) {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( seq.get(j+i*lengteGrid) == rec )
grid[i][j] = value1; //_ set acc shuffled list
}

void setitems(int item, int value1) {
for ( int i = 0; i < item; i++ )
setitem(i, value1);
}

void checkitem() {
for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++)
if ( grid[i][j] == 1) {
// ????????????
}
}

void draw_grid() {
int x0 = 0, y0 = 15, w = 31, h = 21, off = 1;

for (int i = 0; i < breedteGrid; i++)
for (int j = 0; j < lengteGrid; j++) {
fill(255);
if ( grid[i][j] == 1 )
fill(0, 0, 222);
else if ( grid[i][j] == 2 )
fill(255, 0, 222);
rect(x0+i*(w+off), y0+j*(h+off),
w, h);
}
}

void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
duikerY -= duikerH+1;
} else if (keyCode == DOWN) {
duikerY += duikerH+1;
} else if (keyCode == LEFT) {
duikerX -= duikerB+1;
} else if (keyCode == RIGHT) {
duikerX += duikerB+1;
}
}

// Gezorgd dat de duiker altijd binnen de grid blijft.
if (duikerX > width-32) duikerX = 0;
if (duikerX < 0) duikerX = width-32;
if (duikerY >height-55 ) duikerY = 150;
if (duikerY < 150) duikerY = height-72;
}

// -------------------------------------------------------------------

void setup() {
size(1920, 980);
reset();
preset();
setitems(itemsr, 1);
setitems(itemsb, 2);
checkitem();
}

void draw() {
background(0);
draw_grid();
fill(#551A8B);
rect(duikerX, duikerY, duikerB, duikerH);
}
``````
1 Like

my main would be

``````void draw_grid() {
stroke(0);
for (int i = 0; i < many; i++) {
fill(0, 200, 0);
if ( seqb.hasValue(i) ) fill(0, 0, 200);
if ( seqr.hasValue(i) ) fill(200, 0, 0);
int x = x0+(i%cols)*(w+off); //_____________________________________ grid math
int y = y0+(floor(i/cols))*(h+off);
rect(x, y, w, h);
fill(0);
text(i,x+w/2,y+h/2); //_____________________________________________ optional number print
}
}

void setup() {
size(200, 200);
w = ( width - 2*x0 ) /cols - off; //__________________________________ auto size rects
h = ( height -2*y0 ) /rows - off;
presetb(); //_________________________________________________________ create a shuffled list BLUE, itemsb long
presetr(); //_________________________________________________________ create a shuffled list RED,  itemsr long
diag_print();
}

void draw() {
background(200, 200, 0);
draw_grid(); //_______________________________________________________ green rect on yellow background w. BLUE and RED items
}
``````

but the snappy logic

``````    if ( seqb.hasValue(i) ) fill(0, 0, 200);
if ( seqr.hasValue(i) ) fill(200, 0, 0);

``````

can also be used to draw different objects…

if you learn how to repair your code posting
i told you to use the

``````</> button
``````

i might share the rest of the code

1 Like

Thank you all very much for sending the help! this was exactly what I needed in the fist part and was the hardest for me to figure out.

Actually I have no idea how or why this works.

Do you?

Make sure you read through this and understand it.