Add assignment operators to point (flutter/engine#26)

This commit is contained in:
Brandon DeRosier 2022-02-24 15:01:11 -08:00 committed by Dan Field
parent f22879f695
commit 55b7b49549
2 changed files with 116 additions and 0 deletions

View File

@ -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};

View File

@ -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 {