Hi all,
I have created this code which aimes on a square which is taken from the top-left corner to the x and y values of the mouse when the mouse is pressed. Those 2 values are rectX and rectY. Two lines from the x-axis and the y-axis intersect and as you might see, gets closer and closer to the center of the square.
Now, for the problem, when I move it after the lines intersect in the center to a different place. It hadn’t recalibrated most of the time. If the x and y axis of the rectangle is greater than the x and y values it does. If the x axis of the rectangle is greater than the x values of the line it does, and if y axis of the rectangle is greater than the y values of the line it does.
Is there a way to fix this?
Please try this code for yourself.
Thanks, Adi
float a = 1;
float b = 100;
float c = 1;
float d = 100;
int rectX;
int rectY;
void mousePressed() {
rectX = mouseX;
rectY = mouseY;
}
void setup() {
rectMode(CENTER);
frameRate(10);
}
void draw() {
println(frameCount);
background(#9CA0A0);
float x = random(a, b);
float y = random(c, d);
rect(rectX,rectY, 5, 5);
line(x, 0, x, height);
line(0, y, width, y);
if (y>rectX) {
d=d-1;//d=100
}
if (y<rectX) {
c=c+1;//c=1
}
if (x>rectY) {
b=b-1;//b=100
}
if (x<rectY) {
a=a+1;//a=1
}
}