Skip to content

Commit 73795b0

Browse files
committed
Add united() method to Rect
1 parent 9911185 commit 73795b0

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

include/scratchcpp/rect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class LIBSCRATCHCPP_EXPORT Rect
3939
bool contains(double x, double y) const;
4040

4141
static void intersection(const Rect &a, const Rect &b, Rect &dst);
42+
static void united(const Rect &a, const Rect &b, Rect &dst);
4243

4344
private:
4445
spimpl::impl_ptr<RectPrivate> impl;

src/rect.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,12 @@ void Rect::intersection(const Rect &a, const Rect &b, Rect &dst)
146146
dst.impl->top = std::min(a.impl->top, b.impl->top);
147147
dst.impl->bottom = std::max(a.impl->bottom, b.impl->bottom);
148148
}
149+
150+
/*! Saves the union of the given rectangles (in Scratch space) to dst. */
151+
void Rect::united(const Rect &a, const Rect &b, Rect &dst)
152+
{
153+
dst.impl->left = std::min(a.impl->left, b.impl->left);
154+
dst.impl->right = std::max(a.impl->right, b.impl->right);
155+
dst.impl->top = std::max(a.impl->top, b.impl->top);
156+
dst.impl->bottom = std::min(a.impl->bottom, b.impl->bottom);
157+
}

test/rect/rect_test.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,91 @@ TEST(RectTest, Intersection)
422422
ASSERT_EQ(dst.bottom(), -75);
423423
}
424424
}
425+
426+
TEST(RectTest, United)
427+
{
428+
Rect rect1(-50, 25, 150, -75);
429+
Rect dst;
430+
431+
Rect::united(rect1, rect1, dst);
432+
ASSERT_EQ(dst.left(), rect1.left());
433+
ASSERT_EQ(dst.top(), rect1.top());
434+
ASSERT_EQ(dst.right(), rect1.right());
435+
ASSERT_EQ(dst.bottom(), rect1.bottom());
436+
437+
// left
438+
{
439+
Rect rect2(-75, 0, 125, -50);
440+
Rect::united(rect1, rect2, dst);
441+
ASSERT_EQ(dst.left(), -75);
442+
ASSERT_EQ(dst.top(), 25);
443+
ASSERT_EQ(dst.right(), 150);
444+
ASSERT_EQ(dst.bottom(), -75);
445+
}
446+
447+
{
448+
Rect rect2(-100, 10, -50, -90);
449+
Rect::united(rect1, rect2, dst);
450+
ASSERT_EQ(dst.left(), -100);
451+
ASSERT_EQ(dst.top(), 25);
452+
ASSERT_EQ(dst.right(), 150);
453+
ASSERT_EQ(dst.bottom(), -90);
454+
}
455+
456+
// top
457+
{
458+
Rect rect2(-25, 50, 125, 10);
459+
Rect::united(rect1, rect2, dst);
460+
ASSERT_EQ(dst.left(), -50);
461+
ASSERT_EQ(dst.top(), 50);
462+
ASSERT_EQ(dst.right(), 150);
463+
ASSERT_EQ(dst.bottom(), -75);
464+
}
465+
466+
{
467+
Rect rect2(-100, 50, 200, 25);
468+
Rect::united(rect1, rect2, dst);
469+
ASSERT_EQ(dst.left(), -100);
470+
ASSERT_EQ(dst.top(), 50);
471+
ASSERT_EQ(dst.right(), 200);
472+
ASSERT_EQ(dst.bottom(), -75);
473+
}
474+
475+
// right
476+
{
477+
Rect rect2(125, 0, 200, -50);
478+
Rect::united(rect1, rect2, dst);
479+
ASSERT_EQ(dst.left(), -50);
480+
ASSERT_EQ(dst.top(), 25);
481+
ASSERT_EQ(dst.right(), 200);
482+
ASSERT_EQ(dst.bottom(), -75);
483+
}
484+
485+
{
486+
Rect rect2(150, 10, 200, -90);
487+
Rect::united(rect1, rect2, dst);
488+
ASSERT_EQ(dst.left(), -50);
489+
ASSERT_EQ(dst.top(), 25);
490+
ASSERT_EQ(dst.right(), 200);
491+
ASSERT_EQ(dst.bottom(), -90);
492+
}
493+
494+
// bottom
495+
{
496+
Rect rect2(-25, -50, 125, -100);
497+
Rect::united(rect1, rect2, dst);
498+
ASSERT_EQ(dst.left(), -50);
499+
ASSERT_EQ(dst.top(), 25);
500+
ASSERT_EQ(dst.right(), 150);
501+
ASSERT_EQ(dst.bottom(), -100);
502+
}
503+
504+
{
505+
Rect rect2(-100, -75, 200, -100);
506+
Rect::united(rect1, rect2, dst);
507+
ASSERT_EQ(dst.left(), -100);
508+
ASSERT_EQ(dst.top(), 25);
509+
ASSERT_EQ(dst.right(), 200);
510+
ASSERT_EQ(dst.bottom(), -100);
511+
}
512+
}

0 commit comments

Comments
 (0)