Skip to content

Commit 5f58edc

Browse files
committed
Rect: Add clamp() method
Resolves: #527
1 parent 00234a6 commit 5f58edc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/rect.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ void Rect::snapToInt()
8888
impl->top = std::ceil(impl->top);
8989
}
9090

91+
/*! Clamps the rectangle to the given bounds. */
92+
void Rect::clamp(double left, double top, double right, double bottom)
93+
{
94+
// https://github.com/scratchfoundation/scratch-render/blob/c3ede9c3d54769730c7b023021511e2aba167b1f/src/Rectangle.js#L121-L131
95+
impl->left = std::max(impl->left, left);
96+
impl->right = std::min(impl->right, right);
97+
impl->bottom = std::max(impl->bottom, bottom);
98+
impl->top = std::min(impl->top, top);
99+
100+
impl->left = std::min(impl->left, right);
101+
impl->right = std::max(impl->right, left);
102+
impl->bottom = std::min(impl->bottom, top);
103+
impl->top = std::max(impl->top, bottom);
104+
}
105+
91106
/*! Returns true if the rectangle intersects the given rectangle. */
92107
bool Rect::intersects(const Rect &rect) const
93108
{

test/rect/rect_test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,69 @@ TEST(RectTest, Contains)
268268
ASSERT_TRUE(rect_ydown.contains(0, -40));
269269
ASSERT_TRUE(rect_ydown.contains(5, 24));
270270
}
271+
272+
TEST(RectTest, Clamp)
273+
{
274+
{
275+
Rect rect(-50, 25, 150, -75);
276+
rect.clamp(-40, 30, 160, -80);
277+
ASSERT_EQ(rect.left(), -40);
278+
ASSERT_EQ(rect.top(), 25);
279+
ASSERT_EQ(rect.right(), 150);
280+
ASSERT_EQ(rect.bottom(), -75);
281+
}
282+
283+
{
284+
Rect rect(-50, 25, 150, -75);
285+
rect.clamp(-50, 24, 160, -75);
286+
ASSERT_EQ(rect.left(), -50);
287+
ASSERT_EQ(rect.top(), 24);
288+
ASSERT_EQ(rect.right(), 150);
289+
ASSERT_EQ(rect.bottom(), -75);
290+
}
291+
292+
{
293+
Rect rect(-50, 25, 150, -75);
294+
rect.clamp(-50, 25, 145, -75);
295+
ASSERT_EQ(rect.left(), -50);
296+
ASSERT_EQ(rect.top(), 25);
297+
ASSERT_EQ(rect.right(), 145);
298+
ASSERT_EQ(rect.bottom(), -75);
299+
}
300+
301+
{
302+
Rect rect(-50, 25, 150, -75);
303+
rect.clamp(-50, 25, 150, -68);
304+
ASSERT_EQ(rect.left(), -50);
305+
ASSERT_EQ(rect.top(), 25);
306+
ASSERT_EQ(rect.right(), 150);
307+
ASSERT_EQ(rect.bottom(), -68);
308+
}
309+
310+
{
311+
Rect rect(-50, 25, 150, -75);
312+
rect.clamp(-100, 10, 50, -120);
313+
ASSERT_EQ(rect.left(), -50);
314+
ASSERT_EQ(rect.top(), 10);
315+
ASSERT_EQ(rect.right(), 50);
316+
ASSERT_EQ(rect.bottom(), -75);
317+
}
318+
319+
{
320+
Rect rect(-50, 25, 150, -75);
321+
rect.clamp(-100, 30, 160, -120);
322+
ASSERT_EQ(rect.left(), -50);
323+
ASSERT_EQ(rect.top(), 25);
324+
ASSERT_EQ(rect.right(), 150);
325+
ASSERT_EQ(rect.bottom(), -75);
326+
}
327+
328+
{
329+
Rect rect(-50, 25, 150, -75);
330+
rect.clamp(-50, 25, 150, -75);
331+
ASSERT_EQ(rect.left(), -50);
332+
ASSERT_EQ(rect.top(), 25);
333+
ASSERT_EQ(rect.right(), 150);
334+
ASSERT_EQ(rect.bottom(), -75);
335+
}
336+
}

0 commit comments

Comments
 (0)