算法移动鼠标从一点到另一点的直线 [英] Algorithm to move mouse from one point to another in a straight line

查看:397
本文介绍了算法移动鼠标从一点到另一点的直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个小程序,将移动鼠标从当前位置到指定位置。这里是一个我可以使用,将鼠标移动从一个点到另一个,但没有动画的方法:

I am trying to make a small program that will move the mouse from the current position to the given position. Here is a method that i can use which will move the mouse from one point to another but without animation:

moveMouse(int x, int y);

这将移动鼠标从当前坐标的X,Y在屏幕上没有动画。现在,我的工作就是将鼠标移动到该坐标,但它也应该显示鼠标移动一个像素时间。我需要创建一个回路,移动鼠标光标移动几个像素x和y的时间,使这是我一直在思考:

This will move the mouse from the current coordinates to x,y on screen without animation. Now my job is to move the mouse to that coordinate, but it should also show the mouse moving one pixel at a time. I need to create a loop which moves the mouse cursor few pixels x and y at a time so that Here is what i have been thinking:

public void moveMouseAnimation(x,y){
      //Integers x2 and y2 will be the current position of the mouse cursor
      boolean isRunning = true;
      while(isRunning){
           delay(10); // <- 10 Milliseconds pause so that people can see the animation
           x2 -= 1;
           y2 -= 1;
           moveMouse(x2,y2);
           if(x2 == x && y2 == y) isRunning = false; //Ends loop
      }
}

现在我需要找到这样在一条直线上,并达到x和y的鼠标移动,最后正确的X2和Y2值。可能有人帮助我。

Now i need to find correct x2 and y2 values so that the mouse moves in a straight line and reaches x and y at last. Could someone help me.

推荐答案

下面是code做到这一点。这code采用布氏线算法中。有关SOLN更多的裁判尝试 http://en.wikipedia.org/wiki/Bresenham's_line_algorithm如果你正在寻找不具有锯齿线

Below is the code to do that. This code uses Bresenham Line Algo. For more ref on soln try http://en.wikipedia.org/wiki/Bresenham's_line_algorithm if you are looking not to have jagged lines

    boolean steep = Math.abs(y1 - y0) > Math.abs(x1 - x0);
    if (steep) {
        int t;
        // swap(x0, y0);
        t = x0;
        x0 = y0;
        y0 = t;
        // swap(x1, y1);
        t = x1;
        x1 = y1;
        y1 = t;
    }
    if (x0 > x1) {
        int t;
        // swap(x0, x1);
        t = x0;
        x0 = x1;
        x1 = t;

        // swap(y0, y1);
        t = y0;
        y0 = y1;
        y1 = t;
    }
    int deltax = x1 - x0;
    int deltay = Math.abs(y1 - y0);
    int error = deltax / 2;
    int ystep;
    int y = y0;
    if (y0 < y1)
        ystep = 1;
    else
        ystep = -1;

    for (int x = x0; x < x1; x++) {
        if (steep)
            moveMouse(y, x);
        else
            moveMouse(x, y);
        error = error - deltay;
        if (error < 0) {
            y = y + ystep;
            error = error + deltax;
        }
    }

这篇关于算法移动鼠标从一点到另一点的直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆