Skip to content

Commit 9911185

Browse files
committed
Rect: Add intersection() method
Resolves: #418
1 parent 6e5931f commit 9911185

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

include/scratchcpp/rect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class LIBSCRATCHCPP_EXPORT Rect
3838
bool intersects(const Rect &rect) const;
3939
bool contains(double x, double y) const;
4040

41+
static void intersection(const Rect &a, const Rect &b, Rect &dst);
42+
4143
private:
4244
spimpl::impl_ptr<RectPrivate> impl;
4345
};

src/rect.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,12 @@ bool Rect::contains(double x, double y) const
137137

138138
return (x >= impl->left && x <= impl->right && y >= bottom && y <= top);
139139
}
140+
141+
/*! Saves the intersection of the given rectangles (in Scratch space) to dst. */
142+
void Rect::intersection(const Rect &a, const Rect &b, Rect &dst)
143+
{
144+
dst.impl->left = std::max(a.impl->left, b.impl->left);
145+
dst.impl->right = std::min(a.impl->right, b.impl->right);
146+
dst.impl->top = std::min(a.impl->top, b.impl->top);
147+
dst.impl->bottom = std::max(a.impl->bottom, b.impl->bottom);
148+
}

test/rect/rect_test.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,91 @@ TEST(RectTest, Contains)
334334
ASSERT_TRUE(rect_ydown.contains(0, -40));
335335
ASSERT_TRUE(rect_ydown.contains(5, 24));
336336
}
337+
338+
TEST(RectTest, Intersection)
339+
{
340+
Rect rect1(-50, 25, 150, -75);
341+
Rect dst;
342+
343+
Rect::intersection(rect1, rect1, dst);
344+
ASSERT_EQ(dst.left(), rect1.left());
345+
ASSERT_EQ(dst.top(), rect1.top());
346+
ASSERT_EQ(dst.right(), rect1.right());
347+
ASSERT_EQ(dst.bottom(), rect1.bottom());
348+
349+
// left
350+
{
351+
Rect rect2(-75, 0, 125, -50);
352+
Rect::intersection(rect1, rect2, dst);
353+
ASSERT_EQ(dst.left(), -50);
354+
ASSERT_EQ(dst.top(), 0);
355+
ASSERT_EQ(dst.right(), 125);
356+
ASSERT_EQ(dst.bottom(), -50);
357+
}
358+
359+
{
360+
Rect rect2(-100, 10, -50, -90);
361+
Rect::intersection(rect1, rect2, dst);
362+
ASSERT_EQ(dst.left(), -50);
363+
ASSERT_EQ(dst.top(), 10);
364+
ASSERT_EQ(dst.right(), -50);
365+
ASSERT_EQ(dst.bottom(), -75);
366+
}
367+
368+
// top
369+
{
370+
Rect rect2(-25, 50, 125, 10);
371+
Rect::intersection(rect1, rect2, dst);
372+
ASSERT_EQ(dst.left(), -25);
373+
ASSERT_EQ(dst.top(), 25);
374+
ASSERT_EQ(dst.right(), 125);
375+
ASSERT_EQ(dst.bottom(), 10);
376+
}
377+
378+
{
379+
Rect rect2(-100, 50, 200, 25);
380+
Rect::intersection(rect1, rect2, dst);
381+
ASSERT_EQ(dst.left(), -50);
382+
ASSERT_EQ(dst.top(), 25);
383+
ASSERT_EQ(dst.right(), 150);
384+
ASSERT_EQ(dst.bottom(), 25);
385+
}
386+
387+
// right
388+
{
389+
Rect rect2(125, 0, 200, -50);
390+
Rect::intersection(rect1, rect2, dst);
391+
ASSERT_EQ(dst.left(), 125);
392+
ASSERT_EQ(dst.top(), 0);
393+
ASSERT_EQ(dst.right(), 150);
394+
ASSERT_EQ(dst.bottom(), -50);
395+
}
396+
397+
{
398+
Rect rect2(150, 10, 200, -90);
399+
Rect::intersection(rect1, rect2, dst);
400+
ASSERT_EQ(dst.left(), 150);
401+
ASSERT_EQ(dst.top(), 10);
402+
ASSERT_EQ(dst.right(), 150);
403+
ASSERT_EQ(dst.bottom(), -75);
404+
}
405+
406+
// bottom
407+
{
408+
Rect rect2(-25, -50, 125, -100);
409+
Rect::intersection(rect1, rect2, dst);
410+
ASSERT_EQ(dst.left(), -25);
411+
ASSERT_EQ(dst.top(), -50);
412+
ASSERT_EQ(dst.right(), 125);
413+
ASSERT_EQ(dst.bottom(), -75);
414+
}
415+
416+
{
417+
Rect rect2(-100, -75, 200, -100);
418+
Rect::intersection(rect1, rect2, dst);
419+
ASSERT_EQ(dst.left(), -50);
420+
ASSERT_EQ(dst.top(), -75);
421+
ASSERT_EQ(dst.right(), 150);
422+
ASSERT_EQ(dst.bottom(), -75);
423+
}
424+
}

0 commit comments

Comments
 (0)