Check if something is inside a rectangle?
in
Programming Questions
•
2 years ago
First a image to put a visual in your brain:
I i didn't had rotation it would have been easy, point is i have.
So how can i check if something is inside the rectangle?
I want it to be done in the update() function inside the WindField class (but if it's easier to it in another way then taht's also fine).
I i didn't had rotation it would have been easy, point is i have.
So how can i check if something is inside the rectangle?
I want it to be done in the update() function inside the WindField class (but if it's easier to it in another way then taht's also fine).
- WindField windField;
ArrayList<Rotor> rotors = new ArrayList<Rotor>();
void setup(){
size(800, 600);
windField = new WindField();
rotors.add(new Rotor(200, 300, radians(45)));
rotors.add(new Rotor(200, 300, radians(-45)));
rotors.add(new Rotor(200, 300, radians(0)));
smooth();
}
void draw(){
background(255);
for (Rotor rotor : rotors){
rotor.display();
}
windField.update();
windField.display();
}
// . . . . . . . . . . . . . .
- class Rotor{
int x;
int y;
float rotation;
float Width = 100;
float Height = 400;
Rotor(int x, int y, float rotation){
this.x = x;
this.y = y;
this.rotation = rotation;
}
void display(){
pushMatrix();
translate(x, y);
rotate(rotation);
rectMode(CENTER);
fill(0, 60);
rect(0, 0, Width, Height);
fill(0);
rect(0, 0, Width, 1);
popMatrix();
}
}
- class WindArrow{
int x, y;
int windedX;
int windedY;
WindArrow(int x, int y){
this.x = x;
this.y = y;
}
void reset(){
windedX = x;
windedY = y;
}
void display(){
ellipse(windedX, windedY, 2, 2);
}
}
- class WindField{
ArrayList<WindArrow> windArrows = new ArrayList<WindArrow>();
WindField(){
for(int y = 0; y < height; y += 15){
for(int x = 0; x < width; x += 15){
windArrows.add(new WindArrow(x, y));
}
}
}
void update(){
// reset all windArraws to it's original position
for(WindArrow windArrow : windArrows){
windArrow.reset();
}
for(Rotor rotor : rotors){
for(WindArrow windArrow : windArrows){
// check if the windArrow is inside the rotor rectangle
// if so, change windArrow.windedX and windArrow.windedY
// in the direction of the Rotor
}
}
}
void display(){
noStroke();
fill(0);
for(WindArrow windArrow : windArrows){
windArrow.display();
}
};
}
1