Inner angle between two lines

前端 未结 8 1285
忘掉有多难
忘掉有多难 2020-12-14 20:17

I have two lines: Line1 and Line2. Each line is defined by two points (P1L1(x1, y1), P2L1(x2, y2) and P1L1(x1, y1), P2L3(x2, y3)). I want to know t

相关标签:
8条回答
  • 2020-12-14 20:45

    I think what you're looking for is the inner product (you may also want to look over the dot product entry) of the two angles. In your case, that's given by:

    float dx21 = x2-x1;
    float dx31 = x3-x1;
    float dy21 = y2-y1;
    float dy31 = y3-y1;
    float m12 = sqrt( dx21*dx21 + dy21*dy21 );
    float m13 = sqrt( dx31*dx31 + dy31*dy31 );
    float theta = acos( (dx21*dx31 + dy21*dy31) / (m12 * m13) );

    Answer is in radians.

    EDIT: Here's a complete implementation. Substitute the problematic values in p1, p2, and p3 and let me know what you get. The point p1 is the vertex where the two lines intersect, in accordance with your definition of the two lines.

    #include <math.h>
    #include <iostream>
    
    template <typename T> class Vector2D
    {
    private:
        T x;
        T y;
    
    public:
        explicit Vector2D(const T& x=0, const T& y=0) : x(x), y(y) {}
        Vector2D(const Vector2D<T>& src) : x(src.x), y(src.y) {}
        virtual ~Vector2D() {}
    
        // Accessors
        inline T X() const { return x; }
        inline T Y() const { return y; }
        inline T X(const T& x) { this->x = x; }
        inline T Y(const T& y) { this->y = y; }
    
        // Vector arithmetic
        inline Vector2D<T> operator-() const
            { return Vector2D<T>(-x, -y); }
    
        inline Vector2D<T> operator+() const
            { return Vector2D<T>(+x, +y); }
    
        inline Vector2D<T> operator+(const Vector2D<T>& v) const
            { return Vector2D<T>(x+v.x, y+v.y); }
    
        inline Vector2D<T> operator-(const Vector2D<T>& v) const
            { return Vector2D<T>(x-v.x, y-v.y); }
    
        inline Vector2D<T> operator*(const T& s) const
            { return Vector2D<T>(x*s, y*s); }
    
        // Dot product
        inline T operator*(const Vector2D<T>& v) const
            { return x*v.x + y*v.y; }
    
        // l-2 norm
        inline T norm() const { return sqrt(x*x + y*y); }
    
        // inner angle (radians)
        static T angle(const Vector2D<T>& v1, const Vector2D<T>& v2)
        {
            return acos( (v1 * v2) / (v1.norm() * v2.norm()) );
        }
    };
    
    int main()
    {
        Vector2D<double> p1(215, 294);
        Vector2D<double> p2(174, 228);
        Vector2D<double> p3(303, 294);
    
        double rad = Vector2D<double>::angle(p2-p1, p3-p1);
        double deg = rad * 180.0 / M_PI;
    
        std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;
    
        p1 = Vector2D<double>(153, 457);
        p2 = Vector2D<double>(19, 457);
        p3 = Vector2D<double>(15, 470);
    
        rad = Vector2D<double>::angle(p2-p1, p3-p1);
        deg = rad * 180.0 / M_PI;
    
        std::cout << "rad = " << rad << "\tdeg = " << deg << std::endl;
    
        return 0;
    }

    The code above yields:

    rad = 2.12667   deg = 121.849
    rad = 0.0939257 deg = 5.38155

    0 讨论(0)
  • 2020-12-14 20:46

    If you use abolute value you will always get the acute angle. That is tangent theta = abs value of m1-m2 over (1 +m1 * m2). If you take inverse tangent your answer will be in radians or degrees however the calculator is set. Sorry this isnt programming lingo, I am a math teacher, not a programmer...

    0 讨论(0)
  • 2020-12-14 20:49

    If you want in between angle in 0 degree to 360 degree then use following code; Its fully tested and functional:

    static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
    double angle1 = atan2(line1Start.y-line1End.y, line1Start.x-line1End.x);
    double angle2 = atan2(line2Start.y-line2End.y, line2Start.x-line2End.x);
    double result = (angle2-angle1) * 180 / 3.14;
    if (result<0) {
        result+=360;
    }
    return result;
    

    }

    Note: Rotation will be clockwise;

    0 讨论(0)
  • 2020-12-14 20:49

    Getting the outer angle vs the inner angle is determined entirely by the order of your subtractions (think about it). You need to subtract the smaller theta from the larger in order to reliably always get the inner angle. You also probably want to use the atan2 function because of the type of data you're expecting.

    0 讨论(0)
  • 2020-12-14 20:50

    The whole point is much easier than the given answers:

    When you use atan(slope) you lose (literally) one bit of information, that is there are exactly two angles (theta) and (theta+PI) in the range (0..2*PI), which give the same value for the function tan().

    Just use atan2(deltax, deltay) and you get the right angle. For instance

    atan2(1,1) == PI/4
    atan2(-1,-1) == 5*PI/4
    

    Then subtract, take absolute value, and if greater than PI subtract from 2*PI.

    0 讨论(0)
  • 2020-12-14 20:52

    Inner angle between 2 vectors (v1, v2) = arc cos ( inner product(v1,v2) / (module(v1) * module(v2)) ).

    Where inner product(v1,v2) = xv1*xv2 + yv1*yv2

    module(v) = sqrt(pow(xv,2) + pow(yv,2))

    So, the answer of your question is implemented on the following example:

    #define PI   3.14159258
    
    int main()
    {
        double x1,y1,x2,y2,y3;
        double m1, m2;
        double mod1, mod2, innerp, angle;
    
        cout << "x1 :";
        cin >> x1;
        cout << "y1 :";
        cin >> y1;
        cout << "x2 :";
        cin >> x2;
        cout << "y2 :";
        cin >> y2;
        cout << "y3 :";
        cin >> y3;
    
        m1 = atan((y2-y1)/(x2-x1)) * 180 / PI;
        m2 = atan((y3-y1)/(x2-x1)) * 180 / PI;
    
        mod1   = sqrt(pow(y2-y1,2)+pow(x2-x1,2));
        mod2   = sqrt(pow(y3-y1,2)+pow(x2-x1,2));
        innerp = (x2-x1)*(x2-x1) + (y2-y1)*(y3-y1);
        angle  = acos(innerp / (mod1 * mod2)) * 180 / PI;
    
        cout << "m1 : " << m1 << endl;
        cout << "m2 : " << m2 << endl;
        cout << "angle : " << angle << endl;
    }
    
    0 讨论(0)
提交回复
热议问题