Skip to content

Commit 6e5931f

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

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

include/scratchcpp/rect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class LIBSCRATCHCPP_EXPORT Rect
3232
double width() const;
3333
double height() const;
3434

35+
void clamp(double left, double top, double right, double bottom);
3536
void snapToInt();
3637

3738
bool intersects(const Rect &rect) const;

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
@@ -78,6 +78,72 @@ TEST(RectTest, Height)
7878
ASSERT_EQ(rect.height(), 35.272);
7979
}
8080

81+
TEST(RectTest, Clamp)
82+
{
83+
{
84+
Rect rect(-50, 25, 150, -75);
85+
rect.clamp(-40, 30, 160, -80);
86+
ASSERT_EQ(rect.left(), -40);
87+
ASSERT_EQ(rect.top(), 25);
88+
ASSERT_EQ(rect.right(), 150);
89+
ASSERT_EQ(rect.bottom(), -75);
90+
}
91+
92+
{
93+
Rect rect(-50, 25, 150, -75);
94+
rect.clamp(-50, 24, 160, -75);
95+
ASSERT_EQ(rect.left(), -50);
96+
ASSERT_EQ(rect.top(), 24);
97+
ASSERT_EQ(rect.right(), 150);
98+
ASSERT_EQ(rect.bottom(), -75);
99+
}
100+
101+
{
102+
Rect rect(-50, 25, 150, -75);
103+
rect.clamp(-50, 25, 145, -75);
104+
ASSERT_EQ(rect.left(), -50);
105+
ASSERT_EQ(rect.top(), 25);
106+
ASSERT_EQ(rect.right(), 145);
107+
ASSERT_EQ(rect.bottom(), -75);
108+
}
109+
110+
{
111+
Rect rect(-50, 25, 150, -75);
112+
rect.clamp(-50, 25, 150, -68);
113+
ASSERT_EQ(rect.left(), -50);
114+
ASSERT_EQ(rect.top(), 25);
115+
ASSERT_EQ(rect.right(), 150);
116+
ASSERT_EQ(rect.bottom(), -68);
117+
}
118+
119+
{
120+
Rect rect(-50, 25, 150, -75);
121+
rect.clamp(-100, 10, 50, -120);
122+
ASSERT_EQ(rect.left(), -50);
123+
ASSERT_EQ(rect.top(), 10);
124+
ASSERT_EQ(rect.right(), 50);
125+
ASSERT_EQ(rect.bottom(), -75);
126+
}
127+
128+
{
129+
Rect rect(-50, 25, 150, -75);
130+
rect.clamp(-100, 30, 160, -120);
131+
ASSERT_EQ(rect.left(), -50);
132+
ASSERT_EQ(rect.top(), 25);
133+
ASSERT_EQ(rect.right(), 150);
134+
ASSERT_EQ(rect.bottom(), -75);
135+
}
136+
137+
{
138+
Rect rect(-50, 25, 150, -75);
139+
rect.clamp(-50, 25, 150, -75);
140+
ASSERT_EQ(rect.left(), -50);
141+
ASSERT_EQ(rect.top(), 25);
142+
ASSERT_EQ(rect.right(), 150);
143+
ASSERT_EQ(rect.bottom(), -75);
144+
}
145+
}
146+
81147
TEST(RectTest, SnapToInt)
82148
{
83149
{

0 commit comments

Comments
 (0)