📄 capoint2d.h.svn-base
字号:
#ifndef __POINT_2_H_
#define __POINT_2_H_
#include <cmath>
namespace cAni
{
template< typename T >
struct _Point2
{
T x, y;
_Point2():x(0), y(0)
{
}
_Point2(const _Point2 &p):x(p.x), y(p.y)
{
}
_Point2(const T &_x, const T &_y):x(_x), y(_y)
{
}
template<typename T2>
_Point2& operator *= (const T2 &s)
{
x *= s;
y *= s;
return *this;
}
template<typename T2>
_Point2 operator * (const T2 &s) const
{
_Point2 o = *this;
o *= s;
return o;
}
_Point2& operator += (const _Point2& a)
{
x += a.x;
y += a.y;
return *this;
}
_Point2 operator + (const _Point2& b) const
{
_Point2 o = *this;
return o += b;
}
_Point2& operator -= (const _Point2& a)
{
x -= a.x;
y -= a.y;
return *this;
}
_Point2 operator - (const _Point2& b) const
{
_Point2 o = *this;
return o -= b;
}
_Point2 operator - () const
{
return _Point2(-x, -y);
}
_Point2 operator / (const T &t) const
{
return _Point2(x/t, y/t);
}
_Point2& operator /= (const T &t)
{
x /= t;
y /= t;
return *this;
}
T DotProduct() const
{
return x * x + y * y;
}
T operator * (const _Point2 &a) const
{
return x * a.x + y * a.y;
}
friend const _Point2 CrossProduct(const _Point2 &a, const _Point2 &b)
{
return _Point2(a.x * b.y - b.x * a.y, b.x * a.y - a.x * b.y);
}
T operator ^ (const _Point2 &a) const
{
return x * a.y - y * a.x;
}
template< typename T2 >
operator _Point2<T2> () const
{
_Point2<T2> t;
t.x = static_cast<T2>(x);
t.y = static_cast<T2>(y);
return t;
}
T Length() const
{
return x + y;
}
_Point2& Normalize()
{
T len = Length();
if (len == 0)
return *this = _Point2();
else
return *this = *this / len;
}
_Point2& Normalize(T len)
{
T _len = Length();
if (_len == 0)
len = 0;
else
len /= _len;
return *this = *this * len;
}
bool operator == (const _Point2 &a) const
{
return x == a.x && y == a.y;
}
bool operator != (const _Point2 &a) const
{
return ! (*this == a);
}
};
template<>
inline float _Point2<float>::Length() const
{
return sqrtf(DotProduct());
}
template<>
inline double _Point2<double>::Length() const
{
return sqrt(DotProduct());
}
template<>
inline int _Point2<int>::Length() const
{
return (int)sqrtf((float)DotProduct());
}
template<>
inline short _Point2<short>::Length() const
{
return (short)sqrtf((float)DotProduct());
}
typedef _Point2< float > Point2f;
typedef _Point2< int > Point2i;
typedef _Point2< short > Point2s;
} // namespace cAni
#endif // __POINT_2_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -