Retrieve a positive or a negative angle from 3 points

前端 未结 4 392
日久生厌
日久生厌 2020-12-17 03:13

I am rotating points around a center point in 2D space. The points are the center point, the old mouse position, and the new mouse position. My rotation function works fin

相关标签:
4条回答
  • 2020-12-17 03:47

    Try this, but I'm not sure:

    double v1x = Xb - Xc;
    double v1y = Yb - Yc;
    double v2x = Xa - Xc;
    double v2y = Ya - Yc;
    
    double angle = Math.Atan2(v1x, v1y) - Math.Atan2(v2x, v2y);
    
    0 讨论(0)
  • 2020-12-17 03:51
    private double AngleFrom3PointsInDegrees(double x1, double y1, double x2, double y2, double x3, double y3)
    {
        double a = x2 - x1;
        double b = y2 - y1;
        double c = x3 - x2;
        double d = y3 - y2;
    
        double atanA = Math.Atan2(a, b);
        double atanB = Math.Atan2(c, d);
    
        return (atanA - atanB) * (-180 / Math.PI); 
        // if Second line is counterclockwise from 1st line angle is 
        // positive, else negative
    }
    
    0 讨论(0)
  • 2020-12-17 04:02

    Given vectors (x1,y1) and (x2,y2), I would suggest computing the cross product and dot product, and then using Atan2() on them. That will work in all cases where both vectors are non-zero and vector lengths are "reasonable".

    0 讨论(0)
  • 2020-12-17 04:06

    It seems like all you need to do is

    angle = angle > Math.PI ? angle - 2*Math.PI : angle;
    

    at the end of your code. That will give you a clockwise rotation to the right of the line defined by centerPoint and oldPoint, and counter-clockwise to the left of it, regardless of orientation.

    0 讨论(0)
提交回复
热议问题