移动鼠标沿对角线 [英] Moving the mouse along a diagonal line

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

问题描述

我可以使用什么样的数学算法来计算路径移动鼠标?我只是想有这种类型的函数:

What kind of math algorithm could I use to calculate the path to move a mouse? I simply want to have this type of function:

animateMouseDiag(int X, int Y){
    //Move mouse 1 step towards goal, for loop most likely, from the current Mouse.Position
    Thread.Sleep(1);
}

例如,如果我给它animateMouseDiag(100,300),它会移动鼠标100到右侧和300下降,但对角,以'L'不对,然后向下。同样,如果我给它(-50,-200),它会沿着对角线路径将其移动到那些相对坐标(50左和200以上)。

For example, if I give it animateMouseDiag(100,300), it would move the mouse 100 to the right and 300 down, but diagonally, not right-then-down in an 'L'. Similarly, if I gave it (-50,-200) it would move it to those relative coordinates (50 left and 200 up) along the diagonal path.

感谢您! (顺便说一句,这是一个alt帐户,因为我觉得自己像个白痴问我的主要的基本的高中数学。我只是不能把它翻译成节目。)

Thank you! (By the way, this is an alt account since I feel like an idiot asking about basic high school math on my main. I just can't translate it into programming.)

编辑:我想出了这一点:

I have come up with this:

public static void animateCursorTo(int toX, int toY)
        {
            double x0 = Cursor.Position.X;
            double y0 = Cursor.Position.Y;

            double dx = Math.Abs(toX-x0);
            double dy = Math.Abs(toY-y0);

            double sx, sy, err, e2;

            if (x0 < toX) sx = 1;
            else sx = -1;
            if (y0 < toY) sy = 1;
            else sy = -1;
            err = dx-dy;

            for(int i=0; i < toX; i++){
                //setPixel(x0,y0)
                e2 = 2*err;
                if (e2 > -dy) {
                    err = err - dy;
                    x0 = x0 + sx;
                }
                if (e2 <  dx) {
                    err = err + dx;
                    y0 = y0 + sy;
                }
                Cursor.Position = new Point(Convert.ToInt32(x0),Convert.ToInt32(y0));
            }
        }

这是的布雷森汉姆直线算法。奇怪的是,虽然,该行不画一套角度。他们似乎是向屏幕的左上方引力。

This is Bresenham's line algorithm. Strangely though, the lines don't draw on a set angle. They seem to be gravitating towards the top left of the screen.

推荐答案

店铺的位置坐标为浮点值,那么你就可以表示的方向作为一个单位矢量和由一个特定的速度繁殖。

Store the position coordinates as floating point values, then you can represent the direction as a unit vector and multiply by a specific speed.

double mag = Math.Sqrt(directionX * directionX  + directionY * directionY);

mouseX += (directionX / mag) * speed;
mouseY += (directionY / mag) * speed;

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

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