首页 > 代码库 > c#封装三维向量,另外也看了下别人的C++封装

c#封装三维向量,另外也看了下别人的C++封装

一、 c#实现
/*
    Vector3 Definition
    Created by taotao man on 2016-4-12
    brief:封装三位向量类Vector3
    // 修改记录:
    date:
    add SetA()
    Change GetA();
*/



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ceshi
{
    public class Vector3
    {
        private float x;
        private float y;
        private float z;
        private const float E = 0.0000001f;

        public float X
        {
            set { x = value; }
            get { return x; }
        }

        public float Y
        {
            set { y = value; }
            get { return y; }
        }

        public float Z
        {
            set { z = value; }
            get { return z; }
        }

        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector3(Vector3 vct)
        {
            this.x = vct.x;
            this.y = vct.y;
            this.z = vct.z;
        }

        //向量加法
        public static Vector3 operator +(Vector3 a, Vector3 b)
        {
            Vector3 result = new Vector3(a.x + b.x , a.y + b.y, a.z +b.z);
            return result;
        }

        //向量减法
        public static Vector3 operator -(Vector3 a, Vector3 b)
        {
            Vector3 result = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
            return result;
        }
             
        //向量除以一个数
        public static Vector3 operator /(Vector3 a, float b)
        {
            if (b != 0)
            {
                return new Vector3(a.x / b, a.y / b, a.z / b);
            }
            else
            {
                return new Vector3(0, 0, 0);
            }
        }

        // 左乘一个数
        public static Vector3 operator *(float a, Vector3 b)
        {
            return new Vector3(a * b.x, a * b.y, a * b.z);
        }

        // 右乘一个数
        public static Vector3 operator *(Vector3 a, float b)
        {
            return new Vector3(a.x * b, a.y * b, a.z * b);
        }

        // 向量的点乘
        public static float 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)
        {
            if (Math.Abs(a.x - b.x) < E && Math.Abs(a.y - b.y) < E && Math.Abs(a.z - b.z) < E)
            {
                return true;
            }
            else
                return false;
        }

        // 判断两个向量不等
        public static bool operator !=(Vector3 a, Vector3 b)
        {
            return !(a == b);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

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

        public override string ToString()
        {
            return base.ToString();
        }

        // 向量叉积
        public static Vector3 Cross(Vector3 a, Vector3 b)
        {
            return new Vector3(a.y * b.z - a.z * b.y,
                                a.z * b.x - a.x * b.z,
                                a.x * b.y - a.y * b.x);
        }

        //向量的模
        public static float Magnitude(Vector3 a)
        {
            return (float)Math.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
        }

        // 单位化向量

        public static Vector3 Normalize(Vector3 a)
        {
            float magnitude = Magnitude(a);
            return new Vector3(a.x / magnitude, a.y / magnitude, a.z / magnitude);
        }
    }
}

二、c++实现

转载自:3D数学基础图形与游戏开发

 

#include <math.h>

class Vector3
{
public:
	float x, y, z;

	// 默认构造函数
	Vector3(){}
	// 复制构造函数
	Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z){}
	//
	// 带参数的构造函数,用三个值完成初始化
	Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz){}
	// 标准对象操作
	// 重载运算符,并返回引用,以实现左值
	Vector3 &operator = (const Vector3 &a)
	{
		x = a.x; y = a.y; z = a.z;
		return *this;
	}
	//
	//重载“==”操作符
	bool operator == (const Vector3 &a) const
	{
		return x == a.x && y == a.y && z == a.z;
	}
	bool operator != (const Vector3 &a) const
	{
		return x != a.x || y != a.y || z != a.z;
	}

	//向量运算
	// 置为零向量
	void zero()
	{
		x = y = z =0.0f;
	}
	// 重载一元“-”运算符
	Vector3 operator -()const
	{
		return Vector3(-x, -y , -z);
	}
	//重载二元“+”和“-”运算符
	Vector3 operator +(const Vector3 &a) const
	{
		return Vector3(x + a.x, y + a.y, z + a.z);
	}
	Vector3 operator -(const Vector3 &a) const
	{
		return Vector3(x - a.x, y - a.y, z - a.z);
	}
	// 与标量的乘除法
	Vector3 operator * (float a) const
	{
		return Vector3(x * a, y * a, z * a);
	}
	Vector3 operator / (float a) const
	{
		float oneOverA = 1.0f / a;
		return Vector3(x * oneOverA, y * oneOverA, z * oneOverA);
	}
	// 重载自反运算符
	Vector3 &operator += (const Vector3 &a)
	{
		x += a.x; y += a.y; z += a.z;
		return *this;
	}
	Vector3 &operator -= (const Vector3 &a)
	{
		x -= a.x; y -= a.y; z -= a.z;
		return *this;
	}
	Vector3 &operator *= (float a)
	{
		x *= a; y *= a; z *= a;
		return * this;
	}
	Vector3 &operator /= (float a)
	{
		float oneOverA = 1.0f / a;
		x *= oneOverA; y *= oneOverA; z *= oneOverA;
		return *this;
	}
	// 向量标准化
	void normalize()
	{
		float magSq = x * x + y * y + z * z;
		if(magSq > 0.0f)
		{
			float oneOverMag = 1.0f / sqrt(magSq);
			x *= oneOverMag;
			y *= oneOverMag;
			z *= oneOverMag;
		}
	}
	// 向量点乘,重载标准的乘法运算符
	float operator *(const Vector3 &a) const
	{
		return x * a.x + y * a.y + z * a.z;
	}
};

// 非成员函数
// 求向量模
inline float vectorMag(const Vector3 &a)
{
	return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
//计算两个向量的叉乘
inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b)
{
	return Vector3
		(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
//
//实现标量左乘
inline Vector3 operator *(float k, const Vector3 &v)
{
	return Vector3(k * v.x, k * v.y, k * v.z);
}
// 计算两点间的距离
inline float distance(const Vector3 &a, const Vector3 &b)
{
	float dx = a.x - b.x;
	float dy = a.y - b.y;
	float dz = a.x - b.z;
	return sqrt(dx * dx + dy * dy + dz * dz);
}
//提供一个全局零向量
extern const Vector3 kZeroVector;
 

c#封装三维向量,另外也看了下别人的C++封装