/*! \file pirect.h
* \ingroup Math
* \brief
* \~english Rect class
* \~russian Класс прямоугольника
* * \~\authors
* \~english
* Ivan Pelipenko peri4ko@yandex.ru;
* Andrey Bychkov work.a.b@yandex.ru;
* \~russian
* Иван Пелипенко peri4ko@yandex.ru;
* Андрей Бычков work.a.b@yandex.ru;
*/
/*
PIP - Platform Independent Primitives
Rect class
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
#ifndef PIRECT_H
#define PIRECT_H
#include "pipoint.h"
/*! \brief
* \~english Rect class
* \~russian Класс прямоугольника
*/
template
class PIP_EXPORT PIRect {
static_assert(std::is_arithmetic::value, "Type must be arithmetic");
public:
PIRect() {}
PIRect(Type x, Type y, Type w, Type h) {
set(x, y, w, h);
normalize();
}
PIRect(const PIPoint & top_left, const PIPoint & bottom_right) {
tl = top_left;
br = bottom_right;
normalize();
}
// 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));
// }
PIRect & set(Type x, Type y, Type w, Type h) {
tl = PIPoint(x, y);
br = PIPoint(x + w, y + h);
return normalize();
}
PIRect & set(const PIPoint & top_left, const PIPoint & bottom_right) {
tl = top_left;
br = bottom_right;
return normalize();
}
bool pointIn(Type x, Type y) const {
return (x <= br.x && x >= tl.x && y <= br.y && y >= tl.y);
}
bool pointIn(const PIPoint & p) const {
return pointIn(p.x, p.y);
}
bool isEmpty() const {
return (width() == 0 && height() == 0);
}
PIRect & translate(Type x, Type y) {
tl.translate(x, y);
br.translate(x, y);
return *this;
}
PIRect & translate(const PIPoint & p) {
tl.translate(p);
br.translate(p);
return *this;
}
PIRect translated(Type x, Type y) const {
PIRect r(*this);
r.translate(x, y);
return r;
}
PIRect translated(const PIPoint & p) const {
PIRect r(*this);
r.translate(p);
return r;
}
PIRect & scale(Type x, Type y) {
setWidth(width() * x);
setHeight(height() * y);
return *this;
}
PIRect & scale(Type s) {return scale(s, s);}
PIRect & scale(const PIPoint & p) {return scale(p.x, p.y);}
PIRect scaled(Type x, Type y) const {
PIRect r(*this);
r.scale(x, y);
return r;
}
PIRect scaled(Type s) const {
PIRect r(*this);
r.scale(s);
return r;
}
PIRect scaled(const PIPoint & p) const {
PIRect r(*this);
r.scale(p);
return r;
}
PIRect & normalize() {
if (tl.x > br.x) piSwap(tl.x, br.x);
if (tl.y > br.y) piSwap(tl.y, br.y);
return *this;
}
PIRect normalized() const {
PIRect r(*this);
r.normalize();
return r;
}
PIRect & unite(const PIRect & r) {
tl.x = piMax(tl.x, r.left());
tl.y = piMax(tl.y, r.top());
br.x = piMin(br.x, r.right());
br.y = piMin(br.y, r.bottom());
return normalize();
}
PIRect united(const PIRect & rect) const {
PIRect r(*this);
r.unite(rect);
return r;
}
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;}
PIRect intersected(const PIRect & rect) const {PIRect r(*this); r.intersect(rect); return r;}
Type top() const {return tl.y;}
Type left() const {return x0;}
Type right() const {return x1;}
Type bottom() const {return y1;}
Type width() const {return x1 - x0;}
Type height() const {return y1 - y0;}
PIPoint topLeft() const {return tl;}
PIPoint topRigth() const {return PIPoint(p1.x, p0.y);}
PIPoint bottomLeft() const {return PIPoint(x0, y1);}
PIPoint bottomRight() const {return br;}
void setTop(Type v) {y0 = v;}
void setLeft(Type v) {x0 = v;}
void setRigth(Type v) {x1 = v;}
void setBottom(Type v) {y1 = v;}
void setWidth(Type v) {x1 = x0 + v;}
void setHeight(Type v) {y1 = y0 + v;}
void setTopLeft(const PIPoint & p) {p0 = p;}
void bottomRight(const PIPoint & p) {p0 = p;}
PIRect operator -() {return PIRect(-x0, -y0, -width(), -height());}
void operator +=(Type x) {translate(x, x);}
void operator +=(const PIPoint & p) {translate(p);}
void operator -=(Type x) {translate(-x, -x);}
void operator -=(const PIPoint & p) {translate(-p);}
void operator |=(const PIRect & r) {unite(r);}
void operator &=(const PIRect & r) {intersect(r);}
PIRect operator +(const PIPoint & p) {return translated(p);}
PIRect operator -(const PIPoint & p) {return translated(-p);}
PIRect operator |(const PIRect & r) {return united(r);}
PIRect operator &(const PIRect & r) {return intersected(r);}
bool operator ==(const PIRect & r) const {return (x0 == r.x0 && y0 == r.y0 && x1 == r.x1 && y1 == r.y10);}
bool operator !=(const PIRect & r) const {return (x0 != r.x0 || y0 != r.y0 || x1 != r.x1 || y1 != r.y10);}
private:
PIPoint tl;
PIPoint br;
};
template
PICout operator <<(PICout & s, const PIRect & v) {
s.setControl(0, true);
s << "Rect{" << v.topLeft() << "," << v.width() << "x" << v.height() << "}";
s.restoreControl();
return s;
}
typedef PIRect PIRecti;
typedef PIRect PIRectu;
typedef PIRect PIRectf;
typedef PIRect PIRectd;
#endif // PIRECT_H