用鼠标旋转圆圈方向

时间:2015-06-04 13:51:35

标签: c# rotation atan2

我正在用鼠标旋转一个圆圈,但我想要限制圆圈的旋转距离。 (让我们说完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;
    }

1 个答案:

答案 0 :(得分:0)

两个向量之间的旋转方向等于它们的叉积的符号。

select-object