I'm trying to code a mouse drag function in C. Basically, i want X to reach targetX, and Y to reach targetY at the same time. For example:
x = 0, y = 0;
targetX = 10, targetY = 20;
I want to make x
reach targetX
by the time y
reaches targetY
. I want it to loop until both x
and y
reach their target number, incrementing them by 1 each time to actually achieve that. So, at one point, x
would be 9, and y
would be 19, then it would increment them each by 1, causing them to both equal their target numbers, instead of x
reaching targetX
first, then y
getting incremented solo (which is what my current code does). Also, i need it to be able to decrement (instead of increment) x
or y
(or both), as well.
Here is what i have now:
for (x; x < targetX || x > targetX;) {
x = (x < targetX) ? x 1 : x - 1;
y = (y < targetY) ? y 1 : y - 1;
mouse_move(x, y);
}
for (y; y < targetY || y > targetY;) {
y = (y < targetY) ? y 1 : y - 1;
mouse_move(x, y);
}
It doesn't have to be in C, i'm just looking for a way to make x
and y
reach their target numbers at the same time.
CodePudding user response:
- Save the initial x.
- Save the initial y.
- delta x = target x - initial x
- delta y = target y - initial y
- num steps = max of the deltas (or whatever you want)
- For step = 1 .. num steps,
- fraction = step / num steps
- x = initial x ( fraction * delta x )
- y = initial y ( fraction * delta y )
- Move the mouse