From ca3433db3fc412fe048f3cecc14eeb32a4d30376 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 16 Feb 2022 11:53:49 -0800 Subject: [PATCH] Fix Rect::Compare bugs (#10) --- impeller/geometry/geometry_unittests.cc | 25 +++++++++++++++++++++++++ impeller/geometry/rect.h | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index fdd36133691f5..a567475c789e9 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -310,5 +310,30 @@ TEST(GeometryTest, RectIntersection) { } } +TEST(GeometryTest, RectContainsPoint) { + { + // Origin is inclusive + Rect r(100, 100, 100, 100); + Point p(100, 100); + ASSERT_TRUE(r.Contains(p)); + } + { + // Size is exclusive + Rect r(100, 100, 100, 100); + Point p(200, 200); + ASSERT_FALSE(r.Contains(p)); + } + { + Rect r(100, 100, 100, 100); + Point p(99, 99); + ASSERT_FALSE(r.Contains(p)); + } + { + Rect r(100, 100, 100, 100); + Point p(199, 199); + ASSERT_TRUE(r.Contains(p)); + } +} + } // namespace testing } // namespace impeller diff --git a/impeller/geometry/rect.h b/impeller/geometry/rect.h index 4517eba06e5db..00a0ae6ddd255 100644 --- a/impeller/geometry/rect.h +++ b/impeller/geometry/rect.h @@ -80,8 +80,8 @@ struct TRect { } constexpr bool Contains(const TPoint& p) const { - return p.x >= origin.x && p.x <= size.width && p.y >= origin.y && - p.y <= size.height; + return p.x >= origin.x && p.x < origin.x + size.width && p.y >= origin.y && + p.y < origin.y + size.height; } constexpr bool IsZero() const { return size.IsZero(); }