Skip to content

Commit cead8ff

Browse files
committed
Add intersects() method to Rect
1 parent e7ea00f commit cead8ff

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

include/scratchcpp/rect.h

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

35+
bool intersects(const Rect &rect) const;
36+
3537
private:
3638
spimpl::impl_ptr<RectPrivate> impl;
3739
};

src/rect.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,22 @@ double Rect::height() const
7777
{
7878
return std::abs(impl->top - impl->bottom);
7979
}
80+
81+
/*! Returns true if the rectangle intersects the given rectangle. */
82+
bool Rect::intersects(const Rect &rect) const
83+
{
84+
// Get the sorted coordinates for the current rectangle
85+
double x1 = std::min(impl->left, impl->right);
86+
double x2 = std::max(impl->left, impl->right);
87+
double y1 = std::min(impl->top, impl->bottom);
88+
double y2 = std::max(impl->top, impl->bottom);
89+
90+
// Get the sorted coordinates for the other rectangle
91+
double rectX1 = std::min(rect.impl->left, rect.impl->right);
92+
double rectX2 = std::max(rect.impl->left, rect.impl->right);
93+
double rectY1 = std::min(rect.impl->top, rect.impl->bottom);
94+
double rectY2 = std::max(rect.impl->top, rect.impl->bottom);
95+
96+
// Check if the rectangles intersect
97+
return !(x2 < rectX1 || rectX2 < x1 || y2 < rectY1 || rectY2 < y1);
98+
}

test/rect/rect_test.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,135 @@ TEST(RectTest, Height)
7777
rect.setBottom(-58.162);
7878
ASSERT_EQ(rect.height(), 35.272);
7979
}
80+
81+
TEST(RectTest, Intersects)
82+
{
83+
Rect rect1(-50, 25, 150, -75);
84+
Rect rect1_ydown(-50, -75, 150, 25);
85+
ASSERT_TRUE(rect1.intersects(rect1));
86+
ASSERT_TRUE(rect1_ydown.intersects(rect1_ydown));
87+
88+
// left
89+
{
90+
Rect rect2(-75, 0, 125, -50);
91+
ASSERT_TRUE(rect1.intersects(rect2));
92+
ASSERT_TRUE(rect2.intersects(rect1));
93+
94+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
95+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
96+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
97+
}
98+
99+
{
100+
Rect rect2(-100, 10, -50, -90);
101+
ASSERT_TRUE(rect1.intersects(rect2));
102+
ASSERT_TRUE(rect2.intersects(rect1));
103+
104+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
105+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
106+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
107+
}
108+
109+
{
110+
Rect rect2(-150, 10, -100, -90);
111+
ASSERT_FALSE(rect1.intersects(rect2));
112+
ASSERT_FALSE(rect2.intersects(rect1));
113+
114+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
115+
ASSERT_FALSE(rect1_ydown.intersects(rect2_ydown));
116+
ASSERT_FALSE(rect2_ydown.intersects(rect1_ydown));
117+
}
118+
119+
// top
120+
{
121+
Rect rect2(-25, 50, 125, 10);
122+
ASSERT_TRUE(rect1.intersects(rect2));
123+
ASSERT_TRUE(rect2.intersects(rect1));
124+
125+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
126+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
127+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
128+
}
129+
130+
{
131+
Rect rect2(-100, 50, 200, 25);
132+
ASSERT_TRUE(rect1.intersects(rect2));
133+
ASSERT_TRUE(rect2.intersects(rect1));
134+
135+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
136+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
137+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
138+
}
139+
140+
{
141+
Rect rect2(-100, 75, 200, 50);
142+
ASSERT_FALSE(rect1.intersects(rect2));
143+
ASSERT_FALSE(rect2.intersects(rect1));
144+
145+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
146+
ASSERT_FALSE(rect1_ydown.intersects(rect2_ydown));
147+
ASSERT_FALSE(rect2_ydown.intersects(rect1_ydown));
148+
}
149+
150+
// right
151+
{
152+
Rect rect2(125, 0, 200, -50);
153+
ASSERT_TRUE(rect1.intersects(rect2));
154+
ASSERT_TRUE(rect2.intersects(rect1));
155+
156+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
157+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
158+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
159+
}
160+
161+
{
162+
Rect rect2(150, 10, 200, -90);
163+
ASSERT_TRUE(rect1.intersects(rect2));
164+
ASSERT_TRUE(rect2.intersects(rect1));
165+
166+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
167+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
168+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
169+
}
170+
171+
{
172+
Rect rect2(175, 10, 200, -90);
173+
ASSERT_FALSE(rect1.intersects(rect2));
174+
ASSERT_FALSE(rect2.intersects(rect1));
175+
176+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
177+
ASSERT_FALSE(rect1_ydown.intersects(rect2_ydown));
178+
ASSERT_FALSE(rect2_ydown.intersects(rect1_ydown));
179+
}
180+
181+
// bottom
182+
{
183+
Rect rect2(-25, -50, 125, -100);
184+
ASSERT_TRUE(rect1.intersects(rect2));
185+
ASSERT_TRUE(rect2.intersects(rect1));
186+
187+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
188+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
189+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
190+
}
191+
192+
{
193+
Rect rect2(-100, -75, 200, -100);
194+
ASSERT_TRUE(rect1.intersects(rect2));
195+
ASSERT_TRUE(rect2.intersects(rect1));
196+
197+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
198+
ASSERT_TRUE(rect1_ydown.intersects(rect2_ydown));
199+
ASSERT_TRUE(rect2_ydown.intersects(rect1_ydown));
200+
}
201+
202+
{
203+
Rect rect2(-100, -100, 200, -125);
204+
ASSERT_FALSE(rect1.intersects(rect2));
205+
ASSERT_FALSE(rect2.intersects(rect1));
206+
207+
Rect rect2_ydown(rect2.left(), rect2.bottom(), rect2.right(), rect2.top());
208+
ASSERT_FALSE(rect1_ydown.intersects(rect2_ydown));
209+
ASSERT_FALSE(rect2_ydown.intersects(rect1_ydown));
210+
}
211+
}

0 commit comments

Comments
 (0)