构建向量类Vecto4

定义一个Vector4向量

class Vector4
{
    public double x,y,z,w;
    //构造函数1
    public Vector4(){}
    //构造函数2
    public Vector4(double x, double y, double z, double w)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.w = w;
    }
    //构造函数3
    public Vector4(Vector4 v)
    {
        this.x = v.x;
        this.y = v.y;
        this.z = v.z;
        this.w = v.w;
    }
    //两个向量减法
    public static Vector4 operator-(Vector4 a, Vector4 b)
    {
        Vector4 c = new Vector4();
        c.x = a.x - b.x;
        c.y = a.y - b.y;
        c.z = a.z - b.z;
        c.w = a.w - b.w;
        return c;
    }
    //两个向量加法
    public static Vector4 operator+(Vector4 a, Vector4 b)
    {
        Vector4 c = new Vector4();
        c.x = a.x + b.x;
        c.y = a.y + b.y;
        c.z = a.z + b.z;
        c.w = a.w + b.w;
        return c;
    }
    //两个向量点乘
    public double Dot(Vector4 v)
    {
        return this.x*v.x + this.y*v.y + this.z*v.z;
    }
    //两个向量叉乘
    public Vector4 Cross(Vector4 v)
    {
         return new Vector4(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * y - this.y * v.x, 0);
    }
}

你可能感兴趣的:(Unity)