Home > Enterprise >  Make 2 numbers reach their target numbers at the same time
Make 2 numbers reach their target numbers at the same time

Time:07-02

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:

  1. Save the initial x.
  2. Save the initial y.
  3. delta x = target x - initial x
  4. delta y = target y - initial y
  5. num steps = max of the deltas (or whatever you want)
  6. For step = 1 .. num steps,
    1. fraction = step / num steps
    2. x = initial x ( fraction * delta x )
    3. y = initial y ( fraction * delta y )
    4. Move the mouse
  • Related