when rects intersect randomize fill color once
in
Programming Questions
•
4 months ago
i know you can do this with boolean but i can't see how to do this in this particular code, can somebody, please, take a look?
RectIntersect[] r=new RectIntersect[10];
float m,n;
void setup() {
size(400, 400);
ellipseMode(RADIUS);
smooth();
for(int i=0;i<r.length;i++){
r[i]= new RectIntersect(random(width),i*50,20,20,m,i*50,20,20,random(2,5));
}
}
void draw() {
background(204);
for(int i=0;i<r.length;i++){
r[i].display();
}
}
class RectIntersect {
float rX1,rY1,rW1,rH1,rX2,rY2,rW2,rH2,speed;
RectIntersect(float _rX1,float _rY1,float _rW1,float _rH1,
float _rX2,float _rY2,float _rW2,float _rH2,float _speed){
rX1=_rX1; // x
rY1=_rY1; // y
rW1=_rW1; // width
rH1=_rH1; // height
rX2=_rX2; // x
rY2=_rY2; // y
rW2=_rW2; // width
rH2=_rH2; // height
speed=_speed;
}
void display(){
bounce();
if (rectRectIntersect(rX1, rY1, rX1+rW1, rY1+rH1, rX2, rY2, rX2+rW2, rY2+rH2) == true) {
fill(random(255),random(255),random(255));
} else {
fill(255);
}
rect(rX1, rY1, rW1, rH1);
rect(rX2, rY2, rW2, rH2);
}
void bounce(){
rX2 += speed;
if (rX2<- 0 || rX2> width-rW2) speed *= -1;
}
boolean rectRectIntersect(float left, float top, float right, float bottom,
float otherLeft, float otherTop, float otherRight, float otherBottom) {
return !(left > otherRight || right < otherLeft || top > otherBottom || bottom < otherTop);
}
}
1