The angle between 2 vectors

#include<bits/stdc++.h>
#define ll long long

// input: 2 vectors (a1, a2) and (b1, b2)
// output: angle between 2 vectors (unit: degree)
double angle_2_vector(double a1, double a2, double b1, double b2) {
    // PI = 2 * acos(0);
    
    // dot product
    double dot = a1*b1 + a2*b2;
    
    // product of length
    double plen = sqrt(a1*a1 + a2*a2) * sqrt(b1*b1 + b2*b2);
    
    // cos of angle
    double cos_angle = dot / plen;
//      cout << "cos: " << cos_angle << endl;
    
    // angle
    double angle = acos(cos_angle) * 180.0 / PI;
    
    return angle;
}
    
    

#include<bits/stdc++.h>
#define ll long long

// input: 2 vectors (a1, a2, a3) and (b1, b2, b3)
// output: angle between 2 vectors (unit: degree)
double angle_2_vector(double a1, double a2, double a3, double b1, double b2, double b3) {
    // PI = 2 * acos(0);
    
    // dot product
    double dot = a1*b1 + a2*b2 + a3*b3;
    
    // product of length
    double plen = sqrt(a1*a1 + a2*a2 + a3*a3) * sqrt(b1*b1 + b2*b2 + b3*b3);
    
    // cos of angle
    double cos_angle = dot / plen;
//      cout << "cos: " << cos_angle << endl;
    
    // angle
    double angle = acos(cos_angle) * 180.0 / PI;
    
    return angle;
}

Leave a Reply