Skip to content

Commit bafa73b

Browse files
bderodnfield
authored andcommitted
Add assignment operators to point (#26)
1 parent b26c6ac commit bafa73b

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

impeller/geometry/geometry_unittests.cc

+60
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,66 @@ TEST(GeometryTest, SizeCoercesToPoint) {
393393
}
394394
}
395395

396+
TEST(GeometryTest, CanUsePointAssignmentOperators) {
397+
// Point on RHS
398+
{
399+
IPoint p(1, 2);
400+
p += IPoint(1, 2);
401+
ASSERT_EQ(p.x, 2u);
402+
ASSERT_EQ(p.y, 4u);
403+
}
404+
405+
{
406+
IPoint p(3, 6);
407+
p -= IPoint(1, 2);
408+
ASSERT_EQ(p.x, 2u);
409+
ASSERT_EQ(p.y, 4u);
410+
}
411+
412+
{
413+
IPoint p(1, 2);
414+
p *= IPoint(2, 3);
415+
ASSERT_EQ(p.x, 2u);
416+
ASSERT_EQ(p.y, 6u);
417+
}
418+
419+
{
420+
IPoint p(2, 6);
421+
p /= IPoint(2, 3);
422+
ASSERT_EQ(p.x, 1u);
423+
ASSERT_EQ(p.y, 2u);
424+
}
425+
426+
// Size on RHS
427+
{
428+
IPoint p(1, 2);
429+
p += ISize(1, 2);
430+
ASSERT_EQ(p.x, 2u);
431+
ASSERT_EQ(p.y, 4u);
432+
}
433+
434+
{
435+
IPoint p(3, 6);
436+
p -= ISize(1, 2);
437+
ASSERT_EQ(p.x, 2u);
438+
ASSERT_EQ(p.y, 4u);
439+
}
440+
441+
{
442+
IPoint p(1, 2);
443+
p *= ISize(2, 3);
444+
ASSERT_EQ(p.x, 2u);
445+
ASSERT_EQ(p.y, 6u);
446+
}
447+
448+
{
449+
IPoint p(2, 6);
450+
p /= ISize(2, 3);
451+
ASSERT_EQ(p.x, 1u);
452+
ASSERT_EQ(p.y, 2u);
453+
}
454+
}
455+
396456
TEST(GeometryTest, CanConvertBetweenDegressAndRadians) {
397457
{
398458
auto deg = Degrees{90.0};

impeller/geometry/point.h

+56
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,62 @@ struct TPoint {
4545
return p.x != x || p.y != y;
4646
}
4747

48+
template <class U>
49+
inline TPoint operator+=(const TPoint<U>& p) {
50+
x += static_cast<Type>(p.x);
51+
y += static_cast<Type>(p.y);
52+
return *this;
53+
}
54+
55+
template <class U>
56+
inline TPoint operator+=(const TSize<U>& s) {
57+
x += static_cast<Type>(s.width);
58+
y += static_cast<Type>(s.height);
59+
return *this;
60+
}
61+
62+
template <class U>
63+
inline TPoint operator-=(const TPoint<U>& p) {
64+
x -= static_cast<Type>(p.x);
65+
y -= static_cast<Type>(p.y);
66+
return *this;
67+
}
68+
69+
template <class U>
70+
inline TPoint operator-=(const TSize<U>& s) {
71+
x -= static_cast<Type>(s.width);
72+
y -= static_cast<Type>(s.height);
73+
return *this;
74+
}
75+
76+
template <class U>
77+
inline TPoint operator*=(const TPoint<U>& p) {
78+
x *= static_cast<Type>(p.x);
79+
y *= static_cast<Type>(p.y);
80+
return *this;
81+
}
82+
83+
template <class U>
84+
inline TPoint operator*=(const TSize<U>& s) {
85+
x *= static_cast<Type>(s.width);
86+
y *= static_cast<Type>(s.height);
87+
return *this;
88+
}
89+
90+
template <class U>
91+
inline TPoint operator/=(const TPoint<U>& p) {
92+
x /= static_cast<Type>(p.x);
93+
y /= static_cast<Type>(p.y);
94+
return *this;
95+
}
96+
97+
template <class U>
98+
inline TPoint operator/=(const TSize<U>& s) {
99+
x /= static_cast<Type>(s.width);
100+
y /= static_cast<Type>(s.height);
101+
return *this;
102+
}
103+
48104
constexpr TPoint operator-() const { return {-x, -y}; }
49105

50106
constexpr TPoint operator+(const TPoint& p) const {

0 commit comments

Comments
 (0)