Skip to content

Commit 00234a6

Browse files
committed
Rect: Add contains() method
Resolves: #419
1 parent a72f067 commit 00234a6

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

include/scratchcpp/rect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class LIBSCRATCHCPP_EXPORT Rect
3535
void snapToInt();
3636

3737
bool intersects(const Rect &rect) const;
38+
bool contains(double x, double y) const;
3839

3940
private:
4041
spimpl::impl_ptr<RectPrivate> impl;

src/rect.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,19 @@ bool Rect::intersects(const Rect &rect) const
106106
// Check if the rectangles intersect
107107
return !(x2 < rectX1 || rectX2 < x1 || y2 < rectY1 || rectY2 < y1);
108108
}
109+
110+
/*! Returns true if the given point is inside or on the edge of the rectangle. */
111+
bool Rect::contains(double x, double y) const
112+
{
113+
double bottom, top;
114+
115+
if (impl->top > impl->bottom) {
116+
bottom = impl->bottom;
117+
top = impl->top;
118+
} else {
119+
bottom = impl->top;
120+
top = impl->bottom;
121+
}
122+
123+
return (x >= impl->left && x <= impl->right && y >= bottom && y <= top);
124+
}

test/rect/rect_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,41 @@ TEST(RectTest, Intersects)
230230
ASSERT_FALSE(rect2_ydown.intersects(rect1_ydown));
231231
}
232232
}
233+
234+
TEST(RectTest, Contains)
235+
{
236+
Rect rect(-50, 25, 150, -75);
237+
Rect rect_ydown(-50, -75, 150, 25);
238+
239+
ASSERT_FALSE(rect.contains(-75, 30));
240+
ASSERT_FALSE(rect.contains(-75, 10));
241+
ASSERT_FALSE(rect.contains(-45, 30));
242+
ASSERT_FALSE(rect.contains(151, -76));
243+
ASSERT_FALSE(rect.contains(149, -76));
244+
ASSERT_FALSE(rect.contains(151, -74));
245+
246+
ASSERT_FALSE(rect_ydown.contains(-75, 30));
247+
ASSERT_FALSE(rect_ydown.contains(-75, 10));
248+
ASSERT_FALSE(rect_ydown.contains(-45, 30));
249+
ASSERT_FALSE(rect_ydown.contains(151, -76));
250+
ASSERT_FALSE(rect_ydown.contains(149, -76));
251+
ASSERT_FALSE(rect_ydown.contains(151, -74));
252+
253+
ASSERT_TRUE(rect.contains(-50, -75));
254+
ASSERT_TRUE(rect.contains(150, -75));
255+
ASSERT_TRUE(rect.contains(150, 25));
256+
ASSERT_TRUE(rect.contains(-50, 25));
257+
ASSERT_TRUE(rect.contains(-40, -70));
258+
ASSERT_TRUE(rect.contains(100, 0));
259+
ASSERT_TRUE(rect.contains(0, -40));
260+
ASSERT_TRUE(rect.contains(5, 24));
261+
262+
ASSERT_TRUE(rect_ydown.contains(-50, -75));
263+
ASSERT_TRUE(rect_ydown.contains(150, -75));
264+
ASSERT_TRUE(rect_ydown.contains(150, 25));
265+
ASSERT_TRUE(rect_ydown.contains(-50, 25));
266+
ASSERT_TRUE(rect_ydown.contains(-40, -70));
267+
ASSERT_TRUE(rect_ydown.contains(100, 0));
268+
ASSERT_TRUE(rect_ydown.contains(0, -40));
269+
ASSERT_TRUE(rect_ydown.contains(5, 24));
270+
}

0 commit comments

Comments
 (0)