01.03.2011 - as a initial commit

This commit is contained in:
peri4
2011-03-01 06:07:16 +03:00
parent 3610ea9212
commit b21a0496cd
20 changed files with 1589 additions and 522 deletions

View File

@@ -12,7 +12,9 @@ public:
PIPoint() {x = y = 0;};
PIPoint(Type x_, Type y_) {set(x_, y_);}
inline void set(Type x_, Type y_) {x = x_; y = y_;}
inline PIPoint<Type> & set(Type x_, Type y_) {x = x_; y = y_; return *this;}
inline PIPoint<Type> & move(Type x_, Type y_) {x += x_; y += y_; return *this;}
inline PIPoint<Type> & move(const PIPoint<Type> & p) {x += p.x; y += p.y; return *this;}
inline double angleRad() const {return atan2(y, x);}
inline int angleDeg() const {return round(atan2(y, x) * 180. / M_PI);}
@@ -27,6 +29,9 @@ public:
inline bool operator !=(const PIPoint<Type> & p) const {return (x != p.x || y != p.y);}
};
template<typename Type>
inline std::ostream & operator <<(std::ostream & s, const PIPoint<Type> & v) {s << '{' << v.x << ", " << v.y << '}'; return s;}
template<typename Type>
class PIRect {
public:
@@ -38,8 +43,10 @@ public:
PIRect() {x0 = y0 = x1 = y1 = 0;};
PIRect(Type x, Type y, Type w, Type h) {set(x, y, w, h);}
PIRect(const PIPoint<Type> & tl, const PIPoint<Type> & br) {set(tl.x, tl.y, br.x, br.y);}
PIRect(const PIPoint<Type> & p0, const PIPoint<Type> & p1, const PIPoint<Type> & p2) {set(piMin<Type>(p0.x, p1.x, p2.x), piMin<Type>(p0.y, p1.y, p2.y),
piMax<Type>(p0.x, p1.x, p2.x), piMax<Type>(p0.y, p1.y, p2.y));}
inline void set(Type x, Type y, Type w, Type h) {x0 = x; y0 = y; x1 = x + w; y1 = y + h;}
inline PIRect<Type> & set(Type x, Type y, Type w, Type h) {x0 = x; y0 = y; x1 = x + w; y1 = y + h; return *this;}
inline bool pointIn(Type x, Type y) const {return (x <= x1 && x >= x0 && y <= y1 && y >= y0);}
inline bool pointIn(const PIPoint<Type> & p) const {return pointIn(p.x, p.y);}
inline bool isEmpty() const {return (x1 - x0 == 0 && y1 - y0 == 0);}
@@ -51,8 +58,12 @@ public:
inline PIRect<Type> & scale(const PIPoint<Type> & p) {setWidth(width() * p.x); setHeight(height() * p.y); return *this;}
inline PIRect<Type> scaled(Type x, Type y) {PIRect<Type> r(*this); r.scale(x, y); return r;}
inline PIRect<Type> scaled(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.scale(p); return r;}
//inline void unite(const PIRect<Type> & r) {;}
//inline PIRect<Type> & united(const PIRect<Type> & r) {unite(r); return *this;}
inline PIRect<Type> & normalize() {if (x0 > x1) piSwap<Type>(x0, x1); if (y0 > y1) piSwap<Type>(y0, y1); return *this;}
inline PIRect<Type> normalized() {PIRect<Type> r(*this); r.normalize(); return r;}
inline PIRect<Type> & unite(const PIRect<Type> & r) {x0 = piMin<Type>(x0, r.x0); y0 = piMin<Type>(y0, r.y0); x1 = piMax<Type>(x1, r.x1); y1 = piMax<Type>(y1, r.y1); return *this;}
inline PIRect<Type> united(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.unite(rect); return r;}
inline PIRect<Type> & intersect(const PIRect<Type> & r) {x0 = piMax<Type>(x0, r.x0); y0 = piMax<Type>(y0, r.y0); x1 = piMin<Type>(x1, r.x1); y1 = piMin<Type>(y1, r.y1); if (x0 > x1 || y0 > y1) x0 = x1 = y0 = y1 = Type(0); return *this;}
inline PIRect<Type> intersected(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.intersect(rect); return r;}
inline Type top() const {return y0;}
inline Type left() const {return x0;}
inline Type right() const {return x1;}
@@ -77,13 +88,24 @@ public:
inline void operator -=(const PIPoint<Type> & p) {translate(-p);}
inline void operator *=(Type p) {x0 *= p; x1 *= p; y0 *= p; y1 *= p;}
inline void operator /=(Type p) {x0 /= p; x1 /= p; y0 /= p; y1 /= p;}
inline void operator |=(const PIRect<Type> & r) {unite(r);}
inline void operator &=(const PIRect<Type> & r) {intersect(r);}
inline PIRect<Type> operator +(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(p);}
inline PIRect<Type> operator -(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(-p);}
inline PIRect<Type> operator |(const PIRect<Type> & r) {return PIRect<Type>(*this).united(r);}
inline PIRect<Type> operator &(const PIRect<Type> & r) {return PIRect<Type>(*this).intersected(r);}
};
template<typename Type>
inline std::ostream & operator <<(std::ostream & s, const PIRect<Type> & v) {s << '{' << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << '}'; return s;}
typedef PIPoint<int> PIPointi;
typedef PIPoint<uint> PIPointu;
typedef PIPoint<float> PIPointf;
typedef PIPoint<double> PIPointd;
typedef PIRect<int> PIRecti;
typedef PIRect<uint> PIRectu;
typedef PIRect<float> PIRectf;
typedef PIRect<double> PIRectd;