Mir
displacement.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17  */
18 
19 #ifndef MIR_GEOMETRY_DISPLACEMENT_H_
20 #define MIR_GEOMETRY_DISPLACEMENT_H_
21 
23 #include "mir/geometry/point.h"
24 
25 #include <iosfwd>
26 
27 namespace mir
28 {
29 namespace geometry
30 {
31 
33 {
34  constexpr Displacement() {}
35  constexpr Displacement(Displacement const&) = default;
36  Displacement& operator=(Displacement const&) = default;
37 
38  template<typename DeltaXType, typename DeltaYType>
39  constexpr Displacement(DeltaXType&& dx, DeltaYType&& dy) : dx{dx}, dy{dy} {}
40 
41  long long length_squared() const
42  {
43  long long x = dx.as_int(), y = dy.as_int();
44  return x * x + y * y;
45  }
46 
49 };
50 
51 inline constexpr bool operator==(Displacement const& lhs, Displacement const& rhs)
52 {
53  return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
54 }
55 
56 inline constexpr bool operator!=(Displacement const& lhs, Displacement const& rhs)
57 {
58  return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
59 }
60 
61 std::ostream& operator<<(std::ostream& out, Displacement const& value);
62 
63 inline constexpr Displacement operator+(Displacement const& lhs, Displacement const& rhs)
64 {
65  return Displacement{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
66 }
67 
68 inline constexpr Displacement operator-(Displacement const& lhs, Displacement const& rhs)
69 {
70  return Displacement{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
71 }
72 
73 inline constexpr Point operator+(Point const& lhs, Displacement const& rhs)
74 {
75  return Point{lhs.x + rhs.dx, lhs.y + rhs.dy};
76 }
77 
78 inline constexpr Point operator+(Displacement const& lhs, Point const& rhs)
79 {
80  return Point{rhs.x + lhs.dx, rhs.y + lhs.dy};
81 }
82 
83 inline constexpr Point operator-(Point const& lhs, Displacement const& rhs)
84 {
85  return Point{lhs.x - rhs.dx, lhs.y - rhs.dy};
86 }
87 
88 inline constexpr Displacement operator-(Point const& lhs, Point const& rhs)
89 {
90  return Displacement{lhs.x - rhs.x, lhs.y - rhs.y};
91 }
92 
93 inline bool operator<(Displacement const& lhs, Displacement const& rhs)
94 {
95  return lhs.length_squared() < rhs.length_squared();
96 }
97 
98 template<typename Scalar>
99 inline constexpr Displacement operator*(Scalar scale, Displacement const& disp)
100 {
101  return Displacement{scale*disp.dx, scale*disp.dy};
102 }
103 
104 template<typename Scalar>
105 inline constexpr Displacement operator*(Displacement const& disp, Scalar scale)
106 {
107  return scale*disp;
108 }
109 
110 #ifdef MIR_GEOMETRY_SIZE_H_
111 inline constexpr Displacement as_displacement(Size const& size)
112 {
113  return Displacement{size.width.as_int(), size.height.as_int()};
114 }
115 
116 inline constexpr Size as_size(Displacement const& disp)
117 {
118  return Size{disp.dx.as_int(), disp.dy.as_int()};
119 }
120 #endif
121 }
122 }
123 
124 #endif /* MIR_GEOMETRY_DISPLACEMENT_H_ */
Definition: size.h:30
Definition: as_render_target.h:27
DeltaY dy
Definition: displacement.h:48
X x
Definition: point.h:39
DeltaX dx
Definition: displacement.h:47
constexpr int as_int() const
Definition: dimensions.h:53
Definition: point.h:30
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
Y y
Definition: point.h:40
constexpr Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement.h:39
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:148
Definition: displacement.h:32
Displacement & operator=(Displacement const &)=default
Height height
Definition: size.h:40
constexpr Displacement()
Definition: displacement.h:34
std::ostream & operator<<(std::ostream &out, Displacement const &value)
Width width
Definition: size.h:39
bool operator<(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:93
constexpr bool operator==(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:51
constexpr bool operator!=(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:56
long long length_squared() const
Definition: displacement.h:41

Copyright © 2012-2016 Canonical Ltd.
Generated on Mon Jun 5 11:05:03 UTC 2017