Skip to content

Commit 7e6f127

Browse files
committed
Point arithmetic
1 parent 142ebc7 commit 7e6f127

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

include/mapbox/geometry.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#include <mapbox/geometry/multi_polygon.hpp>
99
#include <mapbox/geometry/geometry.hpp>
1010
#include <mapbox/geometry/feature.hpp>
11+
#include <mapbox/geometry/point_arithmetic.hpp>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#pragma once
2+
3+
namespace mapbox { namespace geometry {
4+
5+
template <typename T>
6+
point<T> operator+(point<T> const& lhs, point<T> const& rhs)
7+
{
8+
return point<T>(lhs.x + rhs.x, lhs.y + rhs.y);
9+
}
10+
11+
template <typename T>
12+
point<T> operator+(point<T> const& lhs, T const& rhs)
13+
{
14+
return point<T>(lhs.x + rhs, lhs.y + rhs);
15+
}
16+
17+
template <typename T>
18+
point<T> operator-(point<T> const& lhs, point<T> const& rhs)
19+
{
20+
return point<T>(lhs.x - rhs.x, lhs.y - rhs.y);
21+
}
22+
23+
template <typename T>
24+
point<T> operator-(point<T> const& lhs, T const& rhs)
25+
{
26+
return point<T>(lhs.x - rhs, lhs.y - rhs);
27+
}
28+
29+
template <typename T>
30+
point<T> operator*(point<T> const& lhs, point<T> const& rhs)
31+
{
32+
return point<T>(lhs.x * rhs.x, lhs.y * rhs.y);
33+
}
34+
35+
template <typename T>
36+
point<T> operator*(point<T> const& lhs, T const& rhs)
37+
{
38+
return point<T>(lhs.x * rhs, lhs.y * rhs);
39+
}
40+
41+
template <typename T>
42+
point<T> operator/(point<T> const& lhs, point<T> const& rhs)
43+
{
44+
return point<T>(lhs.x / rhs.x, lhs.y / rhs.y);
45+
}
46+
47+
template <typename T>
48+
point<T> operator/(point<T> const& lhs, T const& rhs)
49+
{
50+
return point<T>(lhs.x / rhs, lhs.y / rhs);
51+
}
52+
53+
template <typename T>
54+
point<T>& operator+=(point<T>& lhs, point<T> const& rhs)
55+
{
56+
lhs.x += rhs.x;
57+
lhs.y += rhs.y;
58+
return lhs;
59+
}
60+
61+
template <typename T>
62+
point<T>& operator+=(point<T>& lhs, T const& rhs)
63+
{
64+
lhs.x += rhs;
65+
lhs.y += rhs;
66+
return lhs;
67+
}
68+
69+
template <typename T>
70+
point<T>& operator-=(point<T>& lhs, point<T> const& rhs)
71+
{
72+
lhs.x -= rhs.x;
73+
lhs.y -= rhs.y;
74+
return lhs;
75+
}
76+
77+
template <typename T>
78+
point<T>& operator-=(point<T>& lhs, T const& rhs)
79+
{
80+
lhs.x -= rhs;
81+
lhs.y -= rhs;
82+
return lhs;
83+
}
84+
85+
template <typename T>
86+
point<T>& operator*=(point<T>& lhs, point<T> const& rhs)
87+
{
88+
lhs.x *= rhs.x;
89+
lhs.y *= rhs.y;
90+
return lhs;
91+
}
92+
93+
template <typename T>
94+
point<T>& operator*=(point<T>& lhs, T const& rhs)
95+
{
96+
lhs.x *= rhs;
97+
lhs.y *= rhs;
98+
return lhs;
99+
}
100+
101+
template <typename T>
102+
point<T>& operator/=(point<T>& lhs, point<T> const& rhs)
103+
{
104+
lhs.x /= rhs.x;
105+
lhs.y /= rhs.y;
106+
return lhs;
107+
}
108+
109+
template <typename T>
110+
point<T>& operator/=(point<T>& lhs, T const& rhs)
111+
{
112+
lhs.x /= rhs;
113+
lhs.y /= rhs;
114+
return lhs;
115+
}
116+
117+
}}

tests/test.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,30 @@
55
using namespace mapbox::geometry;
66

77
static void testPoint() {
8-
point<double> p;
9-
assert(int(p.x) == 0);
10-
assert(int(p.y) == 0);
8+
point<double> p1;
9+
assert(int(p1.x) == 0);
10+
assert(int(p1.y) == 0);
11+
12+
point<uint32_t> p2(2, 3);
13+
point<uint32_t> p3(4, 6);
14+
15+
assert((p2 + p3) == point<uint32_t>(6, 9));
16+
assert((p2 + 1u) == point<uint32_t>(3, 4));
17+
assert((p3 - p2) == point<uint32_t>(2, 3));
18+
assert((p3 - 1u) == point<uint32_t>(3, 5));
19+
assert((p3 * p2) == point<uint32_t>(8, 18));
20+
assert((p2 * 2u) == point<uint32_t>(4, 6));
21+
assert((p3 / p2) == point<uint32_t>(2, 2));
22+
assert((p3 / 2u) == point<uint32_t>(2, 3));
23+
24+
{ point<uint32_t> p(2, 3); assert((p += p3) == point<uint32_t>(6, 9)); }
25+
{ point<uint32_t> p(2, 3); assert((p += 1u) == point<uint32_t>(3, 4)); }
26+
{ point<uint32_t> p(4, 6); assert((p -= p2) == point<uint32_t>(2, 3)); }
27+
{ point<uint32_t> p(4, 6); assert((p -= 1u) == point<uint32_t>(3, 5)); }
28+
{ point<uint32_t> p(4, 6); assert((p *= p2) == point<uint32_t>(8, 18)); }
29+
{ point<uint32_t> p(2, 3); assert((p *= 2u) == point<uint32_t>(4, 6)); }
30+
{ point<uint32_t> p(4, 6); assert((p /= p2) == point<uint32_t>(2, 2)); }
31+
{ point<uint32_t> p(4, 6); assert((p /= 2u) == point<uint32_t>(2, 3)); }
1132
}
1233

1334
static void testMultiPoint() {

0 commit comments

Comments
 (0)