美文网首页
3D向量类

3D向量类

作者: APP4x | 来源:发表于2020-03-08 20:09 被阅读0次

    好的类的设计需要关注的问题:
    1.这个类将提供什么操作?
    2.在哪些数据上执行这些操作?

    对于3D向量类的基本操作:
    1.存取向量的各分量
    2.向量间的赋值操作
    3.比较两个向量是否相同

    以及向量之间的操作:
    1.将向量置为零向量
    2.向量求负
    3.求向量的模
    4.向量与标量的乘除法
    5.向量标准化
    6.向量加减法
    7.计算两点间的距离
    8.向量点乘
    9.向量叉乘


    首先定义 Vector3 类:

     public class Vector3
        {
            public static readonly Vector3 zero = new Vector3(0, 0, 0);
            public static readonly Vector3 one = new Vector3(1, 1, 1);
    
            float x, y, z;
    
    
            //part1. 构造函数
            public Vector3()
            {
            }
            public Vector3(float x, float y, float z)
            {
                this.x = x;
                this.y = y;
                this.z = z;
            }
            public Vector3(Vector3 vector) : this(vector.x, vector.y, vector.z)
            {
            }
    
    
            //part2. 标准对象操作
            public static bool operator ==(Vector3 a, Vector3 b)
            {
                return a.x == b.x && a.y == b.y && a.z == b.z;
            }
            public static bool operator !=(Vector3 a, Vector3 b)
            {
                return a.x != b.x || a.y != b.y || a.z != b.z;
            }
            public override bool Equals(object obj)
            {
                Vector3 vector = (Vector3)obj;//此处应该判断 避免抛出异常
                return vector.x == this.x && vector.y == this.y && vector.z == this.z;
            }
            public override int GetHashCode()
            {
                int hashCode = 373119288;
                hashCode = hashCode * -1521134295 + this.x.GetHashCode();
                hashCode = hashCode * -1521134295 + this.y.GetHashCode();
                hashCode = hashCode * -1521134295 + this.z.GetHashCode();
                return hashCode;
            }
    
    
            //part3. 向量运算
            public static Vector3 operator -(Vector3 a)
            {
                return new Vector3(-a.x, -a.y, -a.z);
            }
            public static Vector3 operator +(Vector3 a, Vector3 b)
            {
                return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
            }
            public static Vector3 operator -(Vector3 a, Vector3 b)
            {
                return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
            }
            public static Vector3 operator *(Vector3 a, float f)
            {
                return new Vector3(a.x * f, a.y * f, a.z * f);
            }
            public static Vector3 operator /(Vector3 a, float f)
            {
                float oneOverF = 1f / f;
                Vector3 result = default(Vector3);
                if (oneOverF > 0f)
                {
                    result = new Vector3(a.x * oneOverF, a.y * oneOverF, a.z * oneOverF);
                }
                return result;
            }
    
            //求模
            public float vectorMag()
            {
                return (float)Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
            }
            //标准化
            public void normalize()
            {
                float magSq = this.x * this.x + this.y * this.y + this.z * this.z;
                if (magSq > 0f)
                {
                    float oneOverMag = 1f / (float)Math.Sqrt(magSq);
                    this.x *= oneOverMag;
                    this.y *= oneOverMag;
                    this.z *= oneOverMag;
                }
            }
            //点乘
            public static float operator *(Vector3 a, Vector3 b)
            {
                return a.x * b.x + a.y * b.y + a.z * b.z;
            }
        }
    

    相关文章

      网友评论

          本文标题:3D向量类

          本文链接:https://www.haomeiwen.com/subject/drpuxhtx.html