模板类重载操作运算符

这篇文章资料来自于网络,对部分知识整理,这里只是记录一下,仅供参考。

重载操作运算符

#ifndef __IRR_DIMENSION2D_H_INCLUDED__
#define __IRR_DIMENSION2D_H_INCLUDED__


namespace ZCY
{
	//! Specifies a 2 dimensional size.
	template <class T>
	class Dimension2d
	{
		public:
			//! Default constructor for empty dimension
			Dimension2d() : Width(0), Height(0) {}
			//! Constructor with width and height
			Dimension2d(const T& width, const T& height)
				: Width(width), Height(height) {}

			//! Equality operator
			bool operator==(const Dimension2d<T>& other) const
			{
				return Width == other.Width && Height == other.Height;
			}

			//! Inequality operator
			bool operator!=(const Dimension2d<T>& other) const
			{
				return ! (*this == other);
			}


			//! Set to new values
			Dimension2d<T>& set(const T& width, const T& height)
			{
				Width = width;
				Height = height;
				return *this;
			}

			//! Divide width and height by scalar
			Dimension2d<T>& operator/=(const T& scale)
			{
				Width /= scale;
				Height /= scale;
				return *this;
			}

			//! Divide width and height by scalar
			Dimension2d<T> operator/(const T& scale) const
			{
				return Dimension2d<T>(Width/scale, Height/scale);
			}

			//! Multiply width and height by scalar
			Dimension2d<T>& operator*=(const T& scale)
			{
				Width *= scale;
				Height *= scale;
				return *this;
			}

			//! Multiply width and height by scalar
			Dimension2d<T> operator*(const T& scale) const
			{
				return Dimension2d<T>(Width*scale, Height*scale);
			}

			//! Add two dimensions
			Dimension2d<T>& operator+=(const Dimension2d<T>& other)
			{
				Width *= other.Width;
				Height *= other.Height;
				return *this;
			}

			//! Add two dimensions
			Dimension2d<T> operator+(const Dimension2d<T>& other) const
			{
				return Dimension2d<T>(Width+other.Width, Height+other.Height);
			}

			//! Get area
			T getArea() const
			{
				return Width*Height;
			}

			//! Get the optimal size according to some properties
			/** This is a function often used for texture dimension
			calculations. The function returns the next larger or
			smaller dimension which is a power-of-two dimension
			(2^n,2^m) and/or square (Width=Height).
			\param requirePowerOfTwo Forces the result to use only
			powers of two as values.
			\param requireSquare Makes width==height in the result
			\param larger Choose whether the result is larger or
			smaller than the current dimension. If one dimension
			need not be changed it is kept with any value of larger.
			\return The optimal dimension under the given
			constraints. */
			Dimension2d<T> getOptimalSize(
					bool requirePowerOfTwo=true,
					bool requireSquare=false,
					bool larger=true) const
			{
				unsigned int i=1;
				unsigned int j=1;
				if (requirePowerOfTwo)
				{
					while (i<(unsigned int)Width)
						i<<=1;
					if (!larger && i!=1 && i!=(unsigned int)Width)
						i>>=1;
					while (j<(unsigned int)Height)
						j<<=1;
					if (!larger && j!=1 && j!=(unsigned int)Height)
						j>>=1;
				}
				else
				{
					i=(unsigned int)Width;
					j=(unsigned int)Height;
				}

				if (requireSquare)
				{
					if ((larger && (i>j)) || (!larger && (i<j)))
						j=i;
					else
						i=j;
				}
				return Dimension2d<T>((T)i,(T)j);
			}

			//! Get the interpolated dimension
			/** \param other Other dimension to interpolate with.
			\param d Value between 0.0f and 1.0f.
			\return Interpolated dimension. */
			Dimension2d<T> getInterpolated(const Dimension2d<T>& other, float d) const
			{
				T inv = (T) (1.0f - d);
				return Dimension2d<T>(other.Width*inv + Width*d, other.Height*inv + Height*d);
			}


			//! Width of the dimension.
			T Width;
			//! Height of the dimension.
			T Height;
	};

	//! Typedef for an float dimension.
	typedef Dimension2d<float> Dimension2df;
	//! Typedef for an integer dimension.
	typedef Dimension2d<signed int> Dimension2di;

} // end namespace

#endif

#ifndef __IRR_POSITION_H_INCLUDED__
#define __IRR_POSITION_H_INCLUDED__

#include "dimension2d.h"

namespace PPE
{

	//! Simple class for holding 2d coordinates.
	/** Not supposed for doing geometric calculations.
	use vector2d instead for things like that.
	*/
	template <class T>
	class Position2d
	{
		public:
			//! Default constructor for (0,0).
			Position2d() : x(0), y(0) {}
			Position2d(T X, T Y) : x(X), y(Y) {}
			Position2d(const Position2d<T>& other)
				: x(other.x), y(other.y) {}

			bool operator == (const Position2d<T>& other) const
			{
				return x == other.x && y == other.y;
			}

			bool operator != (const Position2d<T>& other) const
			{
				return x != other.x || y != other.y;
			}

			const Position2d<T>& operator+=(const Position2d<T>& other)
			{
				x += other.x;
				y += other.y;
				return *this;
			}

			const Position2d<T>& operator-=(const Position2d<T>& other)
			{
				x -= other.x;
				y -= other.y;
				return *this;
			}

			const Position2d<T>& operator+=(const Dimension2d<T>& other)
			{
				x += other.Width;
				y += other.Height;
				return *this;
			}

			const Position2d<T>& operator-=(const Dimension2d<T>& other)
			{
				x -= other.Width;
				y -= other.Height;
				return *this;
			}

			Position2d<T> operator-(const Position2d<T>& other) const
			{
				return Position2d<T>(x-other.x, y-other.y);
			}

			Position2d<T> operator+(const Position2d<T>& other) const
			{
				return Position2d<T>(x+other.x, y+other.y);
			}

			Position2d<T> operator*(const Position2d<T>& other) const
			{
				return Position2d<T>(x*other.x, y*other.y);
			}

			Position2d<T> operator*(const T& scalar) const
			{
				return Position2d<T>(x*scalar, y*scalar);
			}

			Position2d<T> operator+(const Dimension2d<T>& other) const
			{
				return Position2d<T>(x+other.Width, y+other.Height);
			}

			Position2d<T> operator-(const Dimension2d<T>& other) const
			{
				return Position2d<T>(x-other.Width, y-other.Height);
			}

			const Position2d<T>& operator=(const Position2d<T>& other)
			{
				x = other.x;
				y = other.y;
				return *this;
			}

			double distance(const Position2d<T>& other)
			{
				T dx = x > other.x ? x - other.x : other.x - x;
				T dy = y > other.y ? y - other.y : other.y - y;
				dx = dx * dx;
				dy = dy * dy;

				return sqrt( double(dx + dy) );
			}

			//! X coordinate of the position.
			T x;
			//! Y coordinate of the position.
			T y;
	};

	//! Typedef for an float position.
	typedef Position2d<float> Position2df;
	//! Typedef for an integer position.
	typedef Position2d<uint32> Position2di;

	typedef Position2d<double> Position2dd;
} // end namespace

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值