diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index fe8c7726883fe..61702c55afe19 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -393,6 +393,66 @@ TEST(GeometryTest, SizeCoercesToPoint) { } } +TEST(GeometryTest, CanUsePointAssignmentOperators) { + // Point on RHS + { + IPoint p(1, 2); + p += IPoint(1, 2); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 4u); + } + + { + IPoint p(3, 6); + p -= IPoint(1, 2); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 4u); + } + + { + IPoint p(1, 2); + p *= IPoint(2, 3); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 6u); + } + + { + IPoint p(2, 6); + p /= IPoint(2, 3); + ASSERT_EQ(p.x, 1u); + ASSERT_EQ(p.y, 2u); + } + + // Size on RHS + { + IPoint p(1, 2); + p += ISize(1, 2); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 4u); + } + + { + IPoint p(3, 6); + p -= ISize(1, 2); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 4u); + } + + { + IPoint p(1, 2); + p *= ISize(2, 3); + ASSERT_EQ(p.x, 2u); + ASSERT_EQ(p.y, 6u); + } + + { + IPoint p(2, 6); + p /= ISize(2, 3); + ASSERT_EQ(p.x, 1u); + ASSERT_EQ(p.y, 2u); + } +} + TEST(GeometryTest, CanConvertBetweenDegressAndRadians) { { auto deg = Degrees{90.0}; diff --git a/impeller/geometry/point.h b/impeller/geometry/point.h index 8d183b7771e76..ad30904fdad4a 100644 --- a/impeller/geometry/point.h +++ b/impeller/geometry/point.h @@ -45,6 +45,62 @@ struct TPoint { return p.x != x || p.y != y; } + template + inline TPoint operator+=(const TPoint& p) { + x += static_cast(p.x); + y += static_cast(p.y); + return *this; + } + + template + inline TPoint operator+=(const TSize& s) { + x += static_cast(s.width); + y += static_cast(s.height); + return *this; + } + + template + inline TPoint operator-=(const TPoint& p) { + x -= static_cast(p.x); + y -= static_cast(p.y); + return *this; + } + + template + inline TPoint operator-=(const TSize& s) { + x -= static_cast(s.width); + y -= static_cast(s.height); + return *this; + } + + template + inline TPoint operator*=(const TPoint& p) { + x *= static_cast(p.x); + y *= static_cast(p.y); + return *this; + } + + template + inline TPoint operator*=(const TSize& s) { + x *= static_cast(s.width); + y *= static_cast(s.height); + return *this; + } + + template + inline TPoint operator/=(const TPoint& p) { + x /= static_cast(p.x); + y /= static_cast(p.y); + return *this; + } + + template + inline TPoint operator/=(const TSize& s) { + x /= static_cast(s.width); + y /= static_cast(s.height); + return *this; + } + constexpr TPoint operator-() const { return {-x, -y}; } constexpr TPoint operator+(const TPoint& p) const {