Browse Source

Modernised RectangleList

tags/2021-05-28
jules 8 years ago
parent
commit
5c1e707824
1 changed files with 71 additions and 80 deletions
  1. +71
    -80
      modules/juce_graphics/geometry/juce_RectangleList.h

+ 71
- 80
modules/juce_graphics/geometry/juce_RectangleList.h View File

@@ -80,7 +80,7 @@ public:
//============================================================================== //==============================================================================
/** Returns true if the region is empty. */ /** Returns true if the region is empty. */
bool isEmpty() const noexcept { return rects.size() == 0; }
bool isEmpty() const noexcept { return rects.isEmpty(); }
/** Returns the number of rectangles in the list. */ /** Returns the number of rectangles in the list. */
int getNumRectangles() const noexcept { return rects.size(); } int getNumRectangles() const noexcept { return rects.size(); }
@@ -112,7 +112,7 @@ public:
if (! rect.isEmpty()) if (! rect.isEmpty())
{ {
if (rects.size() == 0)
if (isEmpty())
{ {
rects.add (rect); rects.add (rect);
} }
@@ -122,7 +122,7 @@ public:
for (int j = rects.size(); --j >= 0;) for (int j = rects.size(); --j >= 0;)
{ {
RectangleType& ourRect = rects.getReference (j);
auto& ourRect = rects.getReference (j);
if (rect.intersects (ourRect)) if (rect.intersects (ourRect))
{ {
@@ -133,19 +133,17 @@ public:
} }
} }
if (anyOverlaps && rects.size() > 0)
if (anyOverlaps && ! isEmpty())
{ {
RectangleList r (rect); RectangleList r (rect);
for (int i = rects.size(); --i >= 0;)
for (auto& ourRect : rects)
{ {
const RectangleType& ourRect = rects.getReference (i);
if (rect.intersects (ourRect)) if (rect.intersects (ourRect))
{ {
r.subtract (ourRect); r.subtract (ourRect);
if (r.rects.size() == 0)
if (r.isEmpty())
return; return;
} }
} }
@@ -193,7 +191,7 @@ public:
*/ */
void add (const RectangleList& other) void add (const RectangleList& other)
{ {
for (const RectangleType* r = other.begin(), * const e = other.end(); r != e; ++r)
for (auto& r : other)
add (*r); add (*r);
} }
@@ -204,23 +202,21 @@ public:
*/ */
void subtract (const RectangleType& rect) void subtract (const RectangleType& rect)
{ {
const int originalNumRects = rects.size();
if (originalNumRects > 0)
if (auto numRects = rects.size())
{ {
const ValueType x1 = rect.getX();
const ValueType y1 = rect.getY();
const ValueType x2 = x1 + rect.getWidth();
const ValueType y2 = y1 + rect.getHeight();
auto x1 = rect.getX();
auto y1 = rect.getY();
auto x2 = x1 + rect.getWidth();
auto y2 = y1 + rect.getHeight();
for (int i = getNumRectangles(); --i >= 0;)
for (int i = numRects; --i >= 0;)
{ {
RectangleType& r = rects.getReference (i);
auto& r = rects.getReference (i);
const ValueType rx1 = r.getX();
const ValueType ry1 = r.getY();
const ValueType rx2 = rx1 + r.getWidth();
const ValueType ry2 = ry1 + r.getHeight();
auto rx1 = r.getX();
auto ry1 = r.getY();
auto rx2 = rx1 + r.getWidth();
auto ry2 = ry1 + r.getHeight();
if (! (x2 <= rx1 || x1 >= rx2 || y2 <= ry1 || y1 >= ry2)) if (! (x2 <= rx1 || x1 >= rx2 || y2 <= ry1 || y1 >= ry2))
{ {
@@ -294,10 +290,15 @@ public:
*/ */
bool subtract (const RectangleList& otherList) bool subtract (const RectangleList& otherList)
{ {
for (int i = otherList.rects.size(); --i >= 0 && rects.size() > 0;)
subtract (otherList.rects.getReference (i));
for (auto& r : otherList)
{
if (isEmpty())
return false;
subtract (r);
}
return rects.size() > 0;
return ! isEmpty();
} }
/** Removes any areas of the region that lie outside a given rectangle. /** Removes any areas of the region that lie outside a given rectangle.
@@ -323,7 +324,7 @@ public:
{ {
for (int i = rects.size(); --i >= 0;) for (int i = rects.size(); --i >= 0;)
{ {
RectangleType& r = rects.getReference (i);
auto& r = rects.getReference (i);
if (! rect.intersectRectangle (r)) if (! rect.intersectRectangle (r))
rects.remove (i); rects.remove (i);
@@ -347,15 +348,13 @@ public:
template <typename OtherValueType> template <typename OtherValueType>
bool clipTo (const RectangleList<OtherValueType>& other) bool clipTo (const RectangleList<OtherValueType>& other)
{ {
if (rects.size() == 0)
if (isEmpty())
return false; return false;
RectangleList result; RectangleList result;
for (int j = 0; j < rects.size(); ++j)
for (auto& rect : rects)
{ {
auto& rect = rects.getReference (j);
for (auto& r : other) for (auto& r : other)
{ {
auto clipped = r.template toType<ValueType>(); auto clipped = r.template toType<ValueType>();
@@ -385,17 +384,11 @@ public:
destRegion.clear(); destRegion.clear();
if (! rect.isEmpty()) if (! rect.isEmpty())
{
for (int i = rects.size(); --i >= 0;)
{
auto r = rects.getReference (i);
for (auto& r : rects)
if (rect.intersectRectangle (r)) if (rect.intersectRectangle (r))
destRegion.rects.add (r); destRegion.rects.add (r);
}
}
return destRegion.rects.size() > 0;
return ! destRegion.isEmpty();
} }
/** Swaps the contents of this and another list. /** Swaps the contents of this and another list.
@@ -414,8 +407,8 @@ public:
*/ */
bool containsPoint (Point<ValueType> point) const noexcept bool containsPoint (Point<ValueType> point) const noexcept
{ {
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (r->contains (point))
for (auto& r : rects)
if (r.contains (point))
return true; return true;
return false; return false;
@@ -441,15 +434,15 @@ public:
{ {
RectangleList r (rectangleToCheck); RectangleList r (rectangleToCheck);
for (int i = rects.size(); --i >= 0;)
for (auto& rect : rects)
{ {
r.subtract (rects.getReference (i));
r.subtract (rect);
if (r.rects.size() == 0)
if (r.isEmpty())
return true; return true;
} }
} }
else if (rects.size() > 0)
else if (! isEmpty())
{ {
return rects.getReference (0).contains (rectangleToCheck); return rects.getReference (0).contains (rectangleToCheck);
} }
@@ -465,8 +458,8 @@ public:
*/ */
bool intersectsRectangle (const RectangleType& rectangleToCheck) const noexcept bool intersectsRectangle (const RectangleType& rectangleToCheck) const noexcept
{ {
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (r->intersects (rectangleToCheck))
for (auto& r : rects)
if (r.intersects (rectangleToCheck))
return true; return true;
return false; return false;
@@ -478,8 +471,8 @@ public:
*/ */
bool intersects (const RectangleList& other) const noexcept bool intersects (const RectangleList& other) const noexcept
{ {
for (const RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
if (other.intersectsRectangle (*r))
for (auto& r : rects)
if (other.intersectsRectangle (r))
return true; return true;
return false; return false;
@@ -489,24 +482,22 @@ public:
/** Returns the smallest rectangle that can enclose the whole of this region. */ /** Returns the smallest rectangle that can enclose the whole of this region. */
RectangleType getBounds() const noexcept RectangleType getBounds() const noexcept
{ {
if (rects.size() <= 1)
{
if (rects.size() == 0)
return RectangleType();
if (isEmpty())
return {};
return rects.getReference (0);
}
auto& r = rects.getReference (0);
const RectangleType& r = rects.getReference (0);
if (rects.size() == 1)
return r;
ValueType minX = r.getX();
ValueType minY = r.getY();
ValueType maxX = minX + r.getWidth();
ValueType maxY = minY + r.getHeight();
auto minX = r.getX();
auto minY = r.getY();
auto maxX = minX + r.getWidth();
auto maxY = minY + r.getHeight();
for (int i = rects.size(); --i > 0;) for (int i = rects.size(); --i > 0;)
{ {
const RectangleType& r2 = rects.getReference (i);
auto& r2 = rects.getReference (i);
minX = jmin (minX, r2.getX()); minX = jmin (minX, r2.getX());
minY = jmin (minY, r2.getY()); minY = jmin (minY, r2.getY());
@@ -514,7 +505,7 @@ public:
maxY = jmax (maxY, r2.getBottom()); maxY = jmax (maxY, r2.getBottom());
} }
return RectangleType (minX, minY, maxX - minX, maxY - minY);
return { minX, minY, maxX - minX, maxY - minY };
} }
/** Optimises the list into a minimum number of constituent rectangles. /** Optimises the list into a minimum number of constituent rectangles.
@@ -527,19 +518,19 @@ public:
{ {
for (int i = 0; i < rects.size() - 1; ++i) for (int i = 0; i < rects.size() - 1; ++i)
{ {
RectangleType& r = rects.getReference (i);
const ValueType rx1 = r.getX();
const ValueType ry1 = r.getY();
const ValueType rx2 = rx1 + r.getWidth();
const ValueType ry2 = ry1 + r.getHeight();
auto& r = rects.getReference (i);
auto rx1 = r.getX();
auto ry1 = r.getY();
auto rx2 = rx1 + r.getWidth();
auto ry2 = ry1 + r.getHeight();
for (int j = rects.size(); --j > i;) for (int j = rects.size(); --j > i;)
{ {
RectangleType& r2 = rects.getReference (j);
const ValueType jrx1 = r2.getX();
const ValueType jry1 = r2.getY();
const ValueType jrx2 = jrx1 + r2.getWidth();
const ValueType jry2 = jry1 + r2.getHeight();
auto& r2 = rects.getReference (j);
auto jrx1 = r2.getX();
auto jry1 = r2.getY();
auto jrx2 = jrx1 + r2.getWidth();
auto jry2 = jry1 + r2.getHeight();
// if the vertical edges of any blocks are touching and their horizontals don't // if the vertical edges of any blocks are touching and their horizontals don't
// line up, split them horizontally.. // line up, split them horizontally..
@@ -580,7 +571,7 @@ public:
for (int i = 0; i < rects.size() - 1; ++i) for (int i = 0; i < rects.size() - 1; ++i)
{ {
RectangleType& r = rects.getReference (i);
auto& r = rects.getReference (i);
for (int j = rects.size(); --j > i;) for (int j = rects.size(); --j > i;)
{ {
@@ -597,8 +588,8 @@ public:
/** Adds an x and y value to all the coordinates. */ /** Adds an x and y value to all the coordinates. */
void offsetAll (Point<ValueType> offset) noexcept void offsetAll (Point<ValueType> offset) noexcept
{ {
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r += offset;
for (auto& r : rects)
r += offset;
} }
/** Adds an x and y value to all the coordinates. */ /** Adds an x and y value to all the coordinates. */
@@ -611,8 +602,8 @@ public:
template <typename ScaleType> template <typename ScaleType>
void scaleAll (ScaleType scaleFactor) noexcept void scaleAll (ScaleType scaleFactor) noexcept
{ {
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r *= scaleFactor;
for (auto& r : rects)
r *= scaleFactor;
} }
/** Applies a transform to all the rectangles. /** Applies a transform to all the rectangles.
@@ -621,8 +612,8 @@ public:
*/ */
void transformAll (const AffineTransform& transform) noexcept void transformAll (const AffineTransform& transform) noexcept
{ {
for (RectangleType* r = rects.begin(), * const e = rects.end(); r != e; ++r)
*r = r->transformedBy (transform);
for (auto& r : rects)
r = r.transformedBy (transform);
} }
//============================================================================== //==============================================================================
@@ -631,8 +622,8 @@ public:
{ {
Path p; Path p;
for (int i = 0; i < rects.size(); ++i)
p.addRectangle (rects.getReference (i));
for (auto& r : rects)
p.addRectangle (r);
return p; return p;
} }


Loading…
Cancel
Save