gpt4 book ai didi

c# - 如何从鼠标坐标获取基本鼠标方向

转载 作者:太空狗 更新时间:2023-10-29 20:52:25 25 4
gpt4 key购买 nike

是否可以根据鼠标的最后位置和当前位置获取鼠标方向(左、右、上、下)?我已经编写了代码来计算两个向量之间的角度,但我不确定它是否正确。

有人能给我指出正确的方向吗?

    public enum Direction
{
Left = 0,
Right = 1,
Down = 2,
Up = 3
}

private int lastX;
private int lastY;
private Direction direction;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastX = e.X;
lastY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
double angle = GetAngleBetweenVectors(lastX, lastY, e.X, e.Y);
System.Diagnostics.Debug.WriteLine(angle.ToString());
//The angle returns a range of values from -value 0 +value
//How to get the direction from the angle?
//if (angle > ??)
// direction = Direction.Left;
}

private double GetAngleBetweenVectors(double Ax, double Ay, double Bx, double By)
{
double theta = Math.Atan2(Ay, Ax) - Math.Atan2(By, Bx);
return Math.Round(theta * 180 / Math.PI);
}

最佳答案

计算角度似乎过于复杂。为什么不做类似的事情:

int dx = e.X - lastX;
int dy = e.Y - lastY;
if(Math.Abs(dx) > Math.Abs(dy))
direction = (dx > 0) ? Direction.Right : Direction.Left;
else
direction = (dy > 0) ? Direction.Down : Direction.Up;

关于c# - 如何从鼠标坐标获取基本鼠标方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1689397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com