美文网首页
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向量类

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

  • DX笔记-现在有点饿了

    DirectX9.0 基础知识:用类D3DXVETOR3表示3D空间中的向量 1.向量相等: 2.计算向量的模: ...

  • Unity基础-向量

    向量是2D、3D数学研究的标准工具,在3D游戏中向量是基础。 一、向量 1、向量的数学定义 向量就是一个数字列表,...

  • OpenGL中向量和矩阵浅析

    向量 向量:在3D笛卡尔坐标系中,一个顶点就是由XYZ定义的,这样的顶点就是向量。(有方向的点就是向量)向量长度(...

  • 3D数学基础

    3D数学基础 1.向量 1.1 定义 向量是有大小和方向的有向线段,向量没有位置,只有大小和方向 1.2 向量运算...

  • 向量运算

    原文 第1节:零向量 1.零向量的概念 对于任意向量x,都有x+y=x,则x被称为零向量。例如,3D零向量为[0 ...

  • UnityAPI.Vector3三维向量

    表示3D的向量和点。 这个结构用于在Unity传递3D位置和方向。它也包含做些普通向量运算的函数。 除了下面列出的...

  • 2018-05-29 3D数学04(第五章)-向量运算

    第1节:零向量 1.零向量的概念 对于任意向量x,都有x+y=x,则y被称为零向量。例如,3D零向量为[0 0 0...

  • 十、OpenGL中的向量、矩阵、矩阵堆栈

    1. 向量 1.1 向量相关概念 向量:在 3D 笛卡尔坐标系中,一个由 x、y、z组成的顶点就是一个向量 单位向...

  • 向量点乘叉乘、矩阵、OpenGL变化

    向量:向量就是在3D笛卡尔坐标中的一个顶点。单位向量就是长度为1的向量、 标量:标量是一个只有数值大小 没有方向,...

网友评论

      本文标题:3D向量类

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