首页 > 学院 > 开发设计 > 正文

构建向量类Vecto4

2019-11-09 19:15:37
字体:
来源:转载
供稿:网友

定义一个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); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表