Skip to content

Commit

Permalink
Add assignment operators to point (flutter#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent b26c6ac commit bafa73b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
60 changes: 60 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
56 changes: 56 additions & 0 deletions impeller/geometry/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,62 @@ struct TPoint {
return p.x != x || p.y != y;
}

template <class U>
inline TPoint operator+=(const TPoint<U>& p) {
x += static_cast<Type>(p.x);
y += static_cast<Type>(p.y);
return *this;
}

template <class U>
inline TPoint operator+=(const TSize<U>& s) {
x += static_cast<Type>(s.width);
y += static_cast<Type>(s.height);
return *this;
}

template <class U>
inline TPoint operator-=(const TPoint<U>& p) {
x -= static_cast<Type>(p.x);
y -= static_cast<Type>(p.y);
return *this;
}

template <class U>
inline TPoint operator-=(const TSize<U>& s) {
x -= static_cast<Type>(s.width);
y -= static_cast<Type>(s.height);
return *this;
}

template <class U>
inline TPoint operator*=(const TPoint<U>& p) {
x *= static_cast<Type>(p.x);
y *= static_cast<Type>(p.y);
return *this;
}

template <class U>
inline TPoint operator*=(const TSize<U>& s) {
x *= static_cast<Type>(s.width);
y *= static_cast<Type>(s.height);
return *this;
}

template <class U>
inline TPoint operator/=(const TPoint<U>& p) {
x /= static_cast<Type>(p.x);
y /= static_cast<Type>(p.y);
return *this;
}

template <class U>
inline TPoint operator/=(const TSize<U>& s) {
x /= static_cast<Type>(s.width);
y /= static_cast<Type>(s.height);
return *this;
}

constexpr TPoint operator-() const { return {-x, -y}; }

constexpr TPoint operator+(const TPoint& p) const {
Expand Down

0 comments on commit bafa73b

Please sign in to comment.