113 lines
6.2 KiB
C++
113 lines
6.2 KiB
C++
#ifndef PIGEOMETRY_H
|
|
#define PIGEOMETRY_H
|
|
|
|
#include "pimath.h"
|
|
|
|
template<typename Type>
|
|
class PIPoint {
|
|
public:
|
|
Type x;
|
|
Type y;
|
|
|
|
PIPoint() {x = y = 0;};
|
|
PIPoint(Type x_, Type y_) {set(x_, 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);}
|
|
|
|
inline PIPoint<Type> operator +(const PIPoint<Type> & p) {return PIPoint<Type>(x + p.x, y + p.y);}
|
|
inline PIPoint<Type> operator +(const Type & p) {return PIPoint<Type>(x + p, y + p);}
|
|
inline PIPoint<Type> operator -(const PIPoint<Type> & p) {return PIPoint<Type>(x - p.x, y - p.y);}
|
|
inline PIPoint<Type> operator -(const Type & p) {return PIPoint<Type>(x - p, y - p);}
|
|
inline PIPoint<Type> operator -() {return PIPoint<Type>(-x, -y);}
|
|
inline PIPoint<Type> operator *(const Type & d) {return PIPoint<Type>(x * d, y * d);}
|
|
inline PIPoint<Type> operator /(const Type & d) {return PIPoint<Type>(x / d, y / d);}
|
|
inline bool operator ==(const PIPoint<Type> & p) const {return (x == p.x && y == p.y);}
|
|
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:
|
|
Type x0;
|
|
Type y0;
|
|
Type x1;
|
|
Type y1;
|
|
|
|
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 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);}
|
|
inline PIRect<Type> & translate(Type x, Type y) {x0 += x; x1 += x; y0 += y; y1 += y; return *this;}
|
|
inline PIRect<Type> & translate(const PIPoint<Type> & p) {x0 += p.x; x1 += p.x; y0 += p.y; y1 += p.y; return *this;}
|
|
inline PIRect<Type> translated(Type x, Type y) {PIRect<Type> r(*this); r.translate(x, y); return r;}
|
|
inline PIRect<Type> translated(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.translate(p); return r;}
|
|
inline PIRect<Type> & scale(Type x, Type y) {setWidth(width() * x); setHeight(height() * y); return *this;}
|
|
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 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;}
|
|
inline Type bottom() const {return y1;}
|
|
inline Type width() const {return x1 - x0;}
|
|
inline Type height() const {return y1 - y0;}
|
|
inline PIPoint<Type> topLeft() {return PIPoint<Type>(x0, y0);}
|
|
inline PIPoint<Type> topRigth() {return PIPoint<Type>(x1, y0);}
|
|
inline PIPoint<Type> bottomLeft() {return PIPoint<Type>(x0, y1);}
|
|
inline PIPoint<Type> bottomRight() {return PIPoint<Type>(x1, y1);}
|
|
inline void setTop(Type v) {y0 = v;}
|
|
inline void setLeft(Type v) {x0 = v;}
|
|
inline void setRigth(Type v) {x1 = v;}
|
|
inline void setBottom(Type v) {y1 = v;}
|
|
inline void setWidth(Type v) {x1 = x0 + v;}
|
|
inline void setHeight(Type v) {y1 = y0 + v;}
|
|
|
|
inline PIRect<Type> operator -() {return PIRect<Type>(-x0, -y0, -width(), -height());}
|
|
inline void operator +=(Type x) {translate(x, x);}
|
|
inline void operator +=(const PIPoint<Type> & p) {translate(p);}
|
|
inline void operator -=(Type x) {translate(-x, -x);}
|
|
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;
|
|
|
|
#endif // PIGEOMETRY_H
|