沿对角线移动鼠标

我可以使用什么样的数学算法来计算移动鼠标的路径? 我只想拥有这种类型的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)。

谢谢! (顺便说一下,这是一个alt帐户,因为我觉得我是一个白痴问我的主要基础高中数学。我只是无法将其翻译成编程。)

编辑:我想出了这个:

 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  -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)); } } 

这是Bresenham的线算法 。 奇怪的是,线条不会在设定角度上绘制。 它们似乎正在向屏幕的左上方倾斜。

将位置坐标存储为浮点值,然后您可以将方向表示为单位矢量并乘以特定速度。

 double mag = Math.Sqrt(directionX * directionX + directionY * directionY); mouseX += (directionX / mag) * speed; mouseY += (directionY / mag) * speed;