| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /** @file Rect.h
- @author Jukka Jylänki
- This work is released to Public Domain, do whatever you want with it.
- */
- #pragma once
- #include <cassert>
- //#undef assert
- //#define assert(x) DEBUG_ASSERT(x, "rects")
- #if DEBUG
- #define debug_assert(x) assert(x)
- #else
- #define debug_assert(x)
- #endif
- struct RectSize
- {
- int width;
- int height;
- };
- struct RectSizeIndex : RectSize
- {
- int index;
- };
- struct Rect
- {
- int x;
- int y;
- int width;
- int height;
- };
- struct RectIndex : Rect
- {
- int index;
- };
- /// Performs a lexicographic compare on (rect short side, rect long side).
- /// @return -1 if the smaller side of a is shorter than the smaller side of b, 1 if the other way around.
- /// If they are equal, the larger side length is used as a tie-breaker.
- /// If the rectangles are of same size, returns 0.
- int CompareRectShortSide(const Rect &a, const Rect &b);
- /// Performs a lexicographic compare on (x, y, width, height).
- int NodeSortCmp(const Rect &a, const Rect &b);
- /// Returns true if a is contained in b.
- bool IsContainedIn(const Rect &a, const Rect &b);
- #if DEBUG
- class DisjointRectCollection
- {
- public:
- Memc<Rect> rects;
- bool Add(const Rect &r)
- {
- // Degenerate rectangles are ignored.
- if (r.width == 0 || r.height == 0)
- return true;
- if (!Disjoint(r))
- return false;
- rects.add(r);
- return true;
- }
- void Clear()
- {
- rects.clear();
- }
- bool Disjoint(const Rect &r) const
- {
- // Degenerate rectangles are ignored.
- if (r.width == 0 || r.height == 0)
- return true;
- FREPA(rects)
- if (!Disjoint(rects[i], r))
- return false;
- return true;
- }
- static bool Disjoint(const Rect &a, const Rect &b)
- {
- if (a.x + a.width <= b.x ||
- b.x + b.width <= a.x ||
- a.y + a.height <= b.y ||
- b.y + b.height <= a.y)
- return true;
- return false;
- }
- };
- #endif
|