--- /dev/null
+/* -----------------------------------------------------------------------------
+ *
+ * geompp - Basic geometrical utilities for C++.
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2021 Giovanni A. Zuliani | Monocasual Laboratories
+ *
+ * This file is part of geompp - Basic geometrical utilities for C++.
+ *
+ * geompp - Basic geometrical utilities for C++ is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * geompp - Basic geometrical utilities for C++ 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with geompp - Basic geometrical utilities for C++. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * -------------------------------------------------------------------------- */
+
+#ifndef GEOMPP_LINE_HH
+#define GEOMPP_LINE_HH
+
+namespace geompp
+{
+template <typename T>
+class Line
+{
+public:
+ /* Line (1) - invalid. */
+
+ constexpr Line()
+ : Line(0, 0, 0, 0)
+ {
+ }
+
+ /* Line (2) - normal. */
+
+ constexpr Line(T x1, T y1, T x2, T y2)
+ : x1(x1)
+ , y1(y1)
+ , x2(x2)
+ , y2(y2)
+ {
+ }
+
+ /* shiftX
+ Horizontally shifts the line of a certain amount. */
+
+ void shiftX(T amount)
+ {
+ x1 += amount;
+ x2 += amount;
+ }
+
+ /* with[...]
+ Returns a copy of this Line with new coordintates. */
+
+ Line withX1(T v) const { return {v, y1, x2, y2}; }
+ Line withY1(T v) const { return {x1, v, x2, y2}; }
+ Line withX2(T v) const { return {x1, y1, v, y2}; }
+ Line withY2(T v) const { return {x1, y1, x2, v}; }
+ Line withX(T v) const { return {v, y1, v, y2}; }
+ Line withY(T v) const { return {x1, v, x2, v}; }
+
+ /* withShiftedX
+ Returns a new Line horizontally shifted by a certain amount. */
+
+ Line withShiftedX(T amount) const { return {x1 + amount, y1, x2 + amount, y2}; }
+
+ T x1, y1, x2, y2;
+};
+} // namespace geompp
+
+#endif
\ No newline at end of file
--- /dev/null
+/* -----------------------------------------------------------------------------
+ *
+ * geompp - Basic geometrical utilities for C++.
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2021 Giovanni A. Zuliani | Monocasual Laboratories
+ *
+ * This file is part of geompp - Basic geometrical utilities for C++.
+ *
+ * geompp - Basic geometrical utilities for C++ is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * geompp - Basic geometrical utilities for C++ 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with geompp - Basic geometrical utilities for C++. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * -------------------------------------------------------------------------- */
+
+#ifndef GEOMPP_POINT_HH
+#define GEOMPP_POINT_HH
+
+#include <cassert>
+
+namespace geompp
+{
+template <typename T>
+class Point
+{
+public:
+ constexpr Point()
+ : Point(0, 0)
+ {
+ }
+
+ constexpr Point(T x, T y)
+ : x(x)
+ , y(y)
+ {
+ }
+
+ bool operator==(const Point<T>& o) const
+ {
+ return x == o.x && y == o.y;
+ }
+
+ bool operator!=(const Point<T>& o) const
+ {
+ return !(*this == o);
+ }
+
+ Point<T> operator+(const Point<T>& o) const
+ {
+ return {x + o.x, y + o.y};
+ }
+
+ Point<T> operator-(const Point<T>& o) const
+ {
+ return {x - o.x, y - o.y};
+ }
+
+ /* with[...]
+ Returns a copy of this Point with a new coordinate. */
+
+ Point<T> withX(T v) const { return {v, y}; }
+ Point<T> withY(T v) const { return {x, v}; }
+
+ T x;
+ T y;
+};
+} // namespace geompp
+
+#endif
--- /dev/null
+/* -----------------------------------------------------------------------------
+ *
+ * geompp - Basic geometrical utilities for C++.
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2021 Giovanni A. Zuliani | Monocasual Laboratories
+ *
+ * This file is part of geompp - Basic geometrical utilities for C++.
+ *
+ * geompp - Basic geometrical utilities for C++ is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * geompp - Basic geometrical utilities for C++ 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with geompp - Basic geometrical utilities for C++. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * -------------------------------------------------------------------------- */
+
+#ifndef GEOMPP_RANGE_HH
+#define GEOMPP_RANGE_HH
+
+#include <cassert>
+
+namespace geompp
+{
+template <typename T>
+class Range
+{
+public:
+ constexpr Range()
+ : Range(0, 0)
+ {
+ }
+
+ constexpr Range(T a, T b)
+ : a(a)
+ , b(b)
+ {
+ assert(a < b);
+ }
+
+ T getLength() const { return b - a; }
+
+ bool contains(T t) const
+ {
+ return t >= a && t < b;
+ }
+
+ T a, b;
+};
+} // namespace geompp
+
+#endif
--- /dev/null
+/* -----------------------------------------------------------------------------
+ *
+ * geompp - Basic geometrical utilities for C++.
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2021 Giovanni A. Zuliani | Monocasual Laboratories
+ *
+ * This file is part of geompp - Basic geometrical utilities for C++.
+ *
+ * geompp - Basic geometrical utilities for C++ is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * geompp - Basic geometrical utilities for C++ 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with geompp - Basic geometrical utilities for C++. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * -------------------------------------------------------------------------- */
+
+#ifndef GEOMPP_RECT_HH
+#define GEOMPP_RECT_HH
+
+#include "line.hpp"
+#include "point.hpp"
+#include "range.hpp"
+
+namespace geompp
+{
+template <typename T>
+class Rect
+{
+public:
+ /* Rect (1)
+ Invalid rectangle. */
+
+ constexpr Rect()
+ : Rect(0, 0, 0, 0)
+ {
+ }
+
+ /* Rect (2)
+ Normal rectangle. */
+
+ constexpr Rect(T x, T y, T w, T h)
+ : x(x)
+ , y(y)
+ , w(w)
+ , h(h)
+ , xw(x + w)
+ , yh(y + h)
+ {
+ }
+
+ void setX(T v)
+ {
+ x = v;
+ xw = x + w;
+ }
+
+ void setY(T v)
+ {
+ y = v;
+ yh = y + h;
+ }
+
+ void setW(T v)
+ {
+ w = v;
+ xw = x + w;
+ }
+
+ void setH(T v)
+ {
+ h = v;
+ yh = y + h;
+ }
+
+ /* setPosition
+ Sets the top-left corner to Point 'p'. */
+
+ void setPosition(Point<T> p)
+ {
+ setX(p.x);
+ setY(p.y);
+ }
+
+ /* shift[...]
+ Shifts the rectangle by a certain amount. */
+
+ void shiftX(T amount)
+ {
+ x += amount;
+ xw = x + w;
+ }
+
+ void shiftY(T amount)
+ {
+ y += amount;
+ yh = y + h;
+ }
+
+ /* trim[..]
+ Removes 'amount' from the one of the edges. */
+
+ void trimLeft(T amount)
+ {
+ x += amount;
+ w -= amount;
+ }
+
+ void trimRight(T amount)
+ {
+ w -= amount;
+ xw = x + w;
+ }
+
+ void trimTop(T amount)
+ {
+ y += amount;
+ h -= amount;
+ }
+
+ void trimBottom(T amount)
+ {
+ h -= amount;
+ yh = y + h;
+ }
+
+ /* reduce (1)
+ Reduces width and height by a certain amount around the center point. */
+
+ void reduce(T amountX, T amountY)
+ {
+ x += amountX;
+ y += amountY;
+ w -= amountX * 2;
+ h -= amountY * 2;
+ xw = x + w;
+ yh = y + h;
+ }
+
+ /* reduce (2)
+ Reduces all four sides by a certain amount around the center point. */
+
+ void reduce(T amount) { reduce(amount, amount); }
+
+ /* isValid
+ True if this Rect has size greater than zero. */
+
+ bool isValid() const { return w > 0 && h > 0; }
+
+ /* with[...]
+ Returns a copy of this Rect with a new position/size. */
+
+ Rect<T> withX(T v) const { return {v, y, w, h}; }
+ Rect<T> withY(T v) const { return {x, v, w, h}; }
+ Rect<T> withW(T v) const { return {x, y, v, h}; }
+ Rect<T> withH(T v) const { return {x, y, w, v}; }
+
+ /* withPosition
+ Returns a copy of this Rect with a new top-left corner given by Point 'p'. */
+
+ Rect<T> withPosition(Point<T> p) const { return {p.x, p.y, w, h}; }
+
+ /* withShifted[...]
+ Returns a copy of this Rect shifted by a certain amount. */
+
+ Rect<T> withShiftedX(T amount) const { return {x + amount, y, w, h}; }
+ Rect<T> withShiftedY(T amount) const { return {x, y + amount, w, h}; }
+
+ /* withTrimmed[...]
+ Returns a copy of this Rect with 'amount' removed from the one of the
+ edges. */
+
+ Rect<T> withTrimmedLeft(T amount) const { return {x + amount, y, w - amount, h}; }
+ Rect<T> withTrimmedRight(T amount) const { return {x, y, w - amount, h}; }
+ Rect<T> withTrimmedTop(T amount) const { return {x, y + amount, w, h - amount}; }
+ Rect<T> withTrimmedBottom(T amount) const { return {x, y, w, h - amount}; }
+
+ /* get[Width|Height]AsLine
+ Returns width or height as a new Line object. */
+
+ Line<T> getWidthAsLine() const { return Line(x, y, xw - 1, y); }
+ Line<T> getHeightAsLine() const { return Line(x, y, x, yh - 1); }
+
+ Point<T> getPosition() const { return Point(x, y); }
+
+ /* reduced (1)
+ Returns a copy of this Rect with width and height reduced by a certain
+ amount. */
+
+ Rect<T> reduced(T amountX, T amountY) const
+ {
+ Rect r = *this;
+ r.reduce(amountX, amountY);
+ return r;
+ }
+
+ /* reduced (2)
+ Returns a copy of this Rect with all four sides reduced by a certain amount
+ around the center point. */
+
+ Rect<T> reduced(T amount) const
+ {
+ return reduced(amount, amount);
+ }
+
+ /* contains (1)
+ Returns true if Point p is inside this Rect. */
+
+ bool contains(Point<T> p) const
+ {
+ return p.x >= x && p.y >= y && p.x < xw && p.y < yh;
+ }
+
+ /* contains (2)
+ Returns true if Line l is inside this Rect. */
+
+ bool contains(Line<T> l) const
+ {
+ return l.x1 >= x && l.x1 < xw && l.x2 >= x && l.x2 < xw &&
+ l.y1 >= y && l.y1 < yh && l.y2 >= y && l.y2 < yh;
+ }
+
+ T x, y, w, h, xw, yh;
+};
+} // namespace geompp
+
+#endif
\ No newline at end of file