如何从鼠标坐标基本鼠标方向 [英] How to get cardinal mouse direction from mouse coordinates
本文介绍了如何从鼠标坐标基本鼠标方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是有可能得到鼠标的方向(左,右,上,下),根据鼠标的最后一个位置和当前位置?我写的代码,以计算两个向量之间的角度,但我不知道这是否是正确的。
可有人请点我朝着正确的方向?
公共枚举方向
{
左= 0,
右键= 1,
向下= 2,
最多= 3
}
私人诠释lastX;
私人诠释lastY;
私人指导方向;
私人无效Form1_MouseDown(对象发件人,MouseEventArgs E)
{
lastX = e.X;
lastY = e.Y;
}
私人无效Form1_MouseMove(对象发件人,MouseEventArgs E)
{
双角= GetAngleBetweenVectors(lastX,lastY,e.X,e.Y);
System.Diagnostics.Debug.WriteLine(angle.ToString());
//的角度-value 0 +价值
//如何从角的方向返回值的范围?
//如果(角GT; ??)
//方向= Direction.Left;
}
私人双GetAngleBetweenVectors(双斧,双好哦,双BX,增加一倍)
{
双THETA = Math.Atan2(AY,AX) - Math.Atan2(通过,BX);
返回Math.Round(THETA * 180 / Math.PI);
}
解决方案
计算的角度似乎过于复杂。为什么不这样做:
INT DX = e.X - lastX;
INT DY = e.Y - lastY;
如果(Math.Abs(DX)GT; Math.Abs(DY))
方向=(DX大于0)? Direction.Right:Direction.Left;
,否则
方向=(DY大于0)? Direction.Down:Direction.Up;
is it possible to get the mouse direction (Left, Right, Up, Down) based on mouse last position and current position? I have written the code to calculate the angle between two vectors but I am not sure if it is right.
Can someone please point me to the right direction?
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);
}
解决方案
Computing the angle seems overly complex. Why not just do something like:
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;
这篇关于如何从鼠标坐标基本鼠标方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文