#ifndef PIGEOMETRY_H #define PIGEOMETRY_H #include "pimath.h" template class PIPoint { public: Type x; Type y; PIPoint() {x = y = 0;}; PIPoint(Type x_, Type y_) {set(x_, y_);} inline PIPoint & set(Type x_, Type y_) {x = x_; y = y_; return *this;} inline PIPoint & move(Type x_, Type y_) {x += x_; y += y_; return *this;} inline PIPoint & move(const PIPoint & 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 toPolar(bool isDeg = false) const {return PIPoint(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());} inline static PIPoint fromPolar(const PIPoint & p) {return PIPoint(p.y * cos(p.x), p.y * sin(p.x));} inline PIPoint operator +(const PIPoint & p) {return PIPoint(x + p.x, y + p.y);} inline PIPoint operator +(const Type & p) {return PIPoint(x + p, y + p);} inline PIPoint operator -(const PIPoint & p) {return PIPoint(x - p.x, y - p.y);} inline PIPoint operator -(const Type & p) {return PIPoint(x - p, y - p);} inline PIPoint operator -() {return PIPoint(-x, -y);} inline PIPoint operator *(const Type & d) {return PIPoint(x * d, y * d);} inline PIPoint operator /(const Type & d) {return PIPoint(x / d, y / d);} inline bool operator ==(const PIPoint & p) const {return (x == p.x && y == p.y);} inline bool operator !=(const PIPoint & p) const {return (x != p.x || y != p.y);} }; template inline std::ostream & operator <<(std::ostream & s, const PIPoint & v) {s << '{' << v.x << ", " << v.y << '}'; return s;} template 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 & tl, const PIPoint & br) {set(tl.x, tl.y, br.x, br.y);} PIRect(const PIPoint & p0, const PIPoint & p1, const PIPoint & p2) {set(piMin(p0.x, p1.x, p2.x), piMin(p0.y, p1.y, p2.y), piMax(p0.x, p1.x, p2.x), piMax(p0.y, p1.y, p2.y));} inline PIRect & 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 & p) const {return pointIn(p.x, p.y);} inline bool isEmpty() const {return (x1 - x0 == 0 && y1 - y0 == 0);} inline PIRect & translate(Type x, Type y) {x0 += x; x1 += x; y0 += y; y1 += y; return *this;} inline PIRect & translate(const PIPoint & p) {x0 += p.x; x1 += p.x; y0 += p.y; y1 += p.y; return *this;} inline PIRect translated(Type x, Type y) {PIRect r(*this); r.translate(x, y); return r;} inline PIRect translated(const PIPoint & p) {PIRect r(*this); r.translate(p); return r;} inline PIRect & scale(Type x, Type y) {setWidth(width() * x); setHeight(height() * y); return *this;} inline PIRect & scale(const PIPoint & p) {setWidth(width() * p.x); setHeight(height() * p.y); return *this;} inline PIRect scaled(Type x, Type y) {PIRect r(*this); r.scale(x, y); return r;} inline PIRect scaled(const PIPoint & p) {PIRect r(*this); r.scale(p); return r;} inline PIRect & normalize() {if (x0 > x1) piSwap(x0, x1); if (y0 > y1) piSwap(y0, y1); return *this;} inline PIRect normalized() {PIRect r(*this); r.normalize(); return r;} inline PIRect & unite(const PIRect & r) {x0 = piMin(x0, r.x0); y0 = piMin(y0, r.y0); x1 = piMax(x1, r.x1); y1 = piMax(y1, r.y1); return *this;} inline PIRect united(const PIRect & rect) {PIRect r(*this); r.unite(rect); return r;} inline PIRect & intersect(const PIRect & r) {x0 = piMax(x0, r.x0); y0 = piMax(y0, r.y0); x1 = piMin(x1, r.x1); y1 = piMin(y1, r.y1); if (x0 > x1 || y0 > y1) x0 = x1 = y0 = y1 = Type(0); return *this;} inline PIRect intersected(const PIRect & rect) {PIRect 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 topLeft() {return PIPoint(x0, y0);} inline PIPoint topRigth() {return PIPoint(x1, y0);} inline PIPoint bottomLeft() {return PIPoint(x0, y1);} inline PIPoint bottomRight() {return PIPoint(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 operator -() {return PIRect(-x0, -y0, -width(), -height());} inline void operator +=(Type x) {translate(x, x);} inline void operator +=(const PIPoint & p) {translate(p);} inline void operator -=(Type x) {translate(-x, -x);} inline void operator -=(const PIPoint & 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 & r) {unite(r);} inline void operator &=(const PIRect & r) {intersect(r);} inline PIRect operator +(const PIPoint & p) {return PIRect(*this).translated(p);} inline PIRect operator -(const PIPoint & p) {return PIRect(*this).translated(-p);} inline PIRect operator |(const PIRect & r) {return PIRect(*this).united(r);} inline PIRect operator &(const PIRect & r) {return PIRect(*this).intersected(r);} }; template inline std::ostream & operator <<(std::ostream & s, const PIRect & v) {s << '{' << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << '}'; return s;} typedef PIPoint PIPointi; typedef PIPoint PIPointu; typedef PIPoint PIPointf; typedef PIPoint PIPointd; typedef PIRect PIRecti; typedef PIRect PIRectu; typedef PIRect PIRectf; typedef PIRect PIRectd; #endif // PIGEOMETRY_H