Inner angle between two lines

前端 未结 8 1287
忘掉有多难
忘掉有多难 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: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;
    }
    

提交回复
热议问题