C++-向量的点乘、叉乘等操作

综述

向量的点乘、叉乘等操作。

代码



double ThreeDot(Point p,Point a,Point b){
    return(a[0]-p[0])*(b[0]-p[0])+(a[1]-p[1])*(b[1]-p[1])+(a[2]-p[2])*(b[2]-p[2]);
}//pa与pb的点积 (3点)   

Point ThreeCross(Point p,Point a,Point b){
    double x,y,z;
    x=(a[1]-p[1])*(b[2]-p[2])-(a[2]-p[2])*(b[1]-p[1]);
    y=(a[2]-p[2])*(b[0]-p[0])-(a[0]-p[0])*(b[2]-p[2]);
    z=(a[0]-p[0])*(b[1]-p[1])-(a[1]-p[1])*(b[0]-p[0]);
    Point C(x,y,z);
    return C;
}//pa与pb的叉积(3点)

double ThreeDot(Point a,Point b){
    return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
}//向量a与b的点积 (向量)

Point ThreeCross(Point a,Point b){

    int C[3];
    C[0]=a[1]*b[2]-a[2]*b[1];
    C[1]=a[2]*b[0]-a[0]*b[2];
    C[2]=a[0]*b[1]-a[1]*b[0];
    Point nC(C[0],C[1],C[2]);
    return nC;
}//向量a与b的叉积(向量)
double TetrahedronArea(Point a,Point b,Point c,Point d) {

    Point temp(d[0] - a[0], d[1] - a[1], d[2] - a[2]);
    return abs(ThreeDot(ThreeCross(a, b, c), temp)) / 6;
}

你可能感兴趣的:(CG,C++)