欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

常用的3d向量方法,根据《3d数学基础:图形与游戏开发》中第六章改写的c#版

程序员文章站 2022-03-26 14:40:43
...

常用的3d向量方法,根据《3d数学基础:图形与游戏开发》中第六章改写的c#版

class Vector3
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Z { get; set; }

        public Vector3() { }
        public Vector3(float x, float y, float z) { this.X = x; this.Y = y; this.Z = z; }
        public Vector3(Vector3 vec) { this.X = vec.X; this.Y = vec.Y; this.Z = vec.Z; }

        #region 运算符重载
        public static Vector3 operator +(Vector3 vec1, Vector3 vec2)
        {
            return new Vector3(vec1.X + vec2.X, vec1.Y + vec2.Y, vec1.Z + vec2.Z);
        }

        public static Vector3 operator -(Vector3 vec)
        {
            return new Vector3(-vec.X, -vec.Y, -vec.Z);
        }

        public static Vector3 operator -(Vector3 vec1, Vector3 vec2)
        {
            return new Vector3(vec1.X - vec2.X, vec1.Y - vec2.Y, vec1.Z - vec2.Z);
        }

        public static Vector3 operator *(Vector3 vec, float ratio)
        {
            return new Vector3(vec.X * ratio, vec.Y * ratio, vec.Z * ratio);
        }

        public static Vector3 operator *(float ratio, Vector3 vec)
        {
            return new Vector3(vec.X * ratio, vec.Y * ratio, vec.Z * ratio);
        }

        public static float operator *(Vector3 vec1, Vector3 vec2)
        {
            return vec1.X * vec2.X + vec1.Y * vec2.Y + vec1.Z * vec2.Z;
        }

        public static Vector3 operator /(Vector3 vec, float ratio)
        {
            if (ratio == 0)
            {
                Console.WriteLine("除数不能为0");
                return null;
            }
            else
            {
                return new Vector3(vec.X / ratio, vec.Y / ratio, vec.Z / ratio);
            }
        }

        public static bool operator ==(Vector3 vec1, Vector3 vec2)
        {
            return !(vec1 != vec2);
        }

        public static bool operator !=(Vector3 vec1, Vector3 vec2)
        {
            return vec1.X != vec2.X || vec1.Y != vec2.Y || vec1.Z != vec2.Z;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else
            {
                if (obj.GetType() == GetType())
                {
                    Vector3 temp = (Vector3)obj;
                    return this.X == temp.X && this.Y == temp.Y && this.Z == temp.Z;
                }
                else
                {
                    return false;
                }
            }
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2}", this.X, this.Y, this.Z);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion

        public Vector3 Normalize()
        {
            float magSq = X * X + Y * Y + Z * Z;
            if (magSq > 0)
            {
                float oneOverMag = 1.0f / (float)Math.Sqrt(magSq);
                float x = X * oneOverMag;
                float y = Y * oneOverMag;
                float z = Z * oneOverMag;
                return new Vector3(x, y, z);
            }
            return new Vector3(0, 0, 0);
        }

        public float Length()
        {
            return (float)Math.Sqrt(X * X + Y * Y + Z * Z);
        }

        public static float Length(Vector3 vec)
        {
            return (float)Math.Sqrt(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z);
        }

        public static Vector3 CrossProduct(Vector3 vec1, Vector3 vec2)
        {
            return new Vector3(
                vec1.Y * vec2.Z - vec1.Z * vec2.Y,
                vec1.Z * vec2.X - vec1.X * vec2.Z,
                vec1.X * vec2.Y - vec1.Y * vec2.X
                );
        }

        public static float Distance(Vector3 vec1, Vector3 vec2)
        {
            float dx = vec1.X - vec2.X;
            float dy = vec1.Y - vec2.Y;
            float dz = vec1.Z - vec2.Z;
            return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);
        }

        public static Vector3 Zero()
        {
            return new Vector3(0, 0, 0);
        }
    }


相关标签: 3d class