KSu*_*edi 4 java algorithm math equation formula
我正在尝试制作一个小程序,将鼠标从当前位置移动到给定位置.这是一个我可以使用的方法,它将鼠标从一个点移动到另一个点,但没有动画:
moveMouse(int x, int y);
Run Code Online (Sandbox Code Playgroud)
这会将鼠标从当前坐标移动到屏幕上的x,y而不动画.现在我的工作是将鼠标移动到该坐标,但它也应该显示鼠标一次移动一个像素.我需要创建一个循环,一次将鼠标光标移动几个像素x和y,这样我就是这样想的:
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
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要找到正确的x2和y2值,以便鼠标沿直线移动并最终到达x和y.有人能帮助我吗
下面是执行此操作的代码.此代码使用Bresenham Line Algo.有关soln的更多参考,请尝试http://en.wikipedia.org/wiki/Bresenham's_line_algorithm,如果您不希望有锯齿状的线条
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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3023 次 |
最近记录: |