a向量正交投影到b

时间:2021-03-07 19:37:39

1.连个向量 点 乘

//两个向量点乘
static double Dot(const double a[3],const double b[3]){
    return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
}

2.两个向量 x 乘

//向量x乘
void CrossVector(const double a[3],const double b[3],double normal[3]){
    normal[0] = a[1] * b[2] - a[2] * b[1];
    normal[1] = a[2] * b[0] - a[0] * b[2];
    normal[2] = a[0] * b[1] - a[1] * b[0];
}

3.对一个数值与一个向量相乘

//对一个向量进行乘积
static void MultiplyVector(double a[3],double scalar){
    for(unsigned int i=0; i<3; i++){
        a[i] *= scalar;
    }
}

4.a向量正交投影到b

//a向量正交投影到b
bool VectorProjection(const double a[3],const double b[3],double a1[3]){
    double bSq = Dot(b,b);
    //判断b是否为0
    if(0 == bSq){
        a1[0] = 0;
        a1[1] = 0;
        a1[2] = 0;
        return false;
    }

    float ratio = Dot(a,b)/bSq;
    for(unsigned int i=0; i<3; i++){
        a1[i] = b[i];
    }
    MultiplyVector(a1,ratio);
    return true;
}

向量投影详细解读https://www.cnblogs.com/graphics/archive/2010/08/03/1791626.html

相关文章