我正在用鼠标旋转一个圆圈,但我希望对圆圈的旋转范围有一个限制。(假设是 3 个完整的时间)。当它达到极限时,它不能再朝同一个方向转动,但可以朝相反的方向转动。我让它在最大转弯后停止,但现在我试图找到方向,每次我的鼠标经过圆圈的 x 轴时,方向都会改变,因为 atan2 给了我相对于 x 轴的角度。所以新的鼠标位置在一个象限,最后一个位置在另一个象限,所以减去这些角度并不能得到我想要的。我解释清楚了吗?有什么建议么?
private void HelmPb_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button != MouseButtons.Left) || _dontTurn) return;
double angle = OffsetAngle();
Point temp = MousePosition;
float degrees = Convert.ToSingle(angle - _offsetAngle);
float diff = _lastHelmAngle - degrees;
float absDiff = Math.Abs(diff) % 360;
if (absDiff > 180) absDiff = 360 - absDiff;
double angle1 = Math.Atan2(_lastHelmPoint.Y, _lastHelmPoint.X);
if (angle1 < 0) angle1 += 2*Math.PI;
double angle2 = Math.Atan2(temp.Y, temp.X);
if (angle2 < 0) angle2 += 2*Math.PI;
double direction = angle1 - angle2;
direction = direction*(180/Math.PI);
_deltaHelmTurn += Convert.ToSingle(absDiff);
if (_deltaHelmTurn >= (_maxHelmTurn*360.0))
{
if (direction > 0 && _lastHelmDirection > 0)
{
_deltaHelmTurn = Convert.ToSingle(_maxHelmTurn*360.0);
degrees = Convert.ToSingle(_maxHelmTurn*360.0)%360;
}
}
_lastHelmDirection = direction;
_lastHelmPoint = MousePosition;
_lastHelmAngle = Convert.ToSingle(degrees);
_sameHelmRotation = Convert.ToSingle(degrees);
HelmPb.Image.Dispose();
WaterDepthPlot.Update();
HelmPb.Image = RotateImage(_originalHelmImage, -degrees);
HelmPb.Update();
}
private double OffsetAngle()
{
int helmXMid = HelmPb.PointToScreen(Point.Empty).X + (HelmPb.Width / 2);
int helmYMid = HelmPb.PointToScreen(Point.Empty).Y + (HelmPb.Height / 2);
double angle = AngleFromPoints(MousePosition, new Point(helmXMid, helmYMid));
return angle;
}
private double AngleFromPoints(Point pt1, Point pt2)
{
Point p = new Point(pt1.X - pt2.X, pt1.Y - pt2.Y);
double alpha;
if (p.Y == 0) alpha = p.X > 0 ? 0d : 180d;
else
{
double f = 1d * p.X / (Math.Sqrt(p.X * p.X + p.Y * p.Y));
alpha = Math.Acos(f) * 180d / Math.PI;
if (p.Y > 0) alpha = 360d - alpha;
}
return alpha;
}