Hey guys, so I know that there are other posts about this topic but they don’t really seem to help. Other solutions make use of arrayList and class, which I was not taught. And so I was wondering if it is possible to achieve this using what I’ve coded.
In the code below, I’m trying to generate 6 random rectangles that don’t overlap. The code within the enclosed by the line of === is my attempt at populating the arrays, and using a while and for loop to check each of the distances, however, they still overlap.
Thanks!
(I know the variable names are confusing. What I’ve posted is only a small part of what I need to do)
int rectSize = 80;
float [] rectxCoords = new float [6];
float [] rectyCoords = new float [6];
void drawRects(float x, float y) {
rectMode(CENTER);
fill(230, 0, 0);
rect(x, y, rectSize, 120);
}
void setup() {
size(1200, 900);
//BACKGROUND
fill(#FF7E7E);
rect(0, 0, width/3, height);
//===========================================
int i=0;
while (i<6) {
rectxCoords[i]=random(rectSize/2, 400-rectSize/2);
rectyCoords[i]=random(250, 850);
for (int j=0; j<6; j++) {
rectxCoords[j]=random(rectSize/2, 400-rectSize/2);
rectyCoords[j]=random(250, 850);
float d=dist(rectxCoords[i], rectyCoords[i], rectxCoords[j], rectyCoords[j]);
if (d<90) {
rectxCoords[i]=rectxCoords[j];
rectyCoords[i]=rectyCoords[j];
i++;
}
}
}
//======================================================
}
void draw() {
for (int i=0; i<6; i++) {
drawRects(rectxCoords[i], rectyCoords[i]);
}
}