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 2 or 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 #include "mir/geometry/size.h"
25 
26 #include <iosfwd>
27 
28 namespace mir
29 {
30 namespace geometry
31 {
32 
34 {
35  constexpr Displacement() {}
36  constexpr Displacement(Displacement const&) = default;
37  Displacement& operator=(Displacement const&) = default;
38 
39  template<typename DeltaXType, typename DeltaYType>
40  constexpr Displacement(DeltaXType&& dx, DeltaYType&& dy) : dx{dx}, dy{dy} {}
41 
42  long long length_squared() const
43  {
44  long long x = dx.as_int(), y = dy.as_int();
45  return x * x + y * y;
46  }
47 
50 };
51 
52 inline constexpr bool operator==(Displacement const& lhs, Displacement const& rhs)
53 {
54  return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
55 }
56 
57 inline constexpr bool operator!=(Displacement const& lhs, Displacement const& rhs)
58 {
59  return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
60 }
61 
62 std::ostream& operator<<(std::ostream& out, Displacement const& value);
63 
64 inline constexpr Displacement operator+(Displacement const& lhs, Displacement const& rhs)
65 {
66  return Displacement{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
67 }
68 
69 inline constexpr Displacement operator-(Displacement const& lhs, Displacement const& rhs)
70 {
71  return Displacement{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
72 }
73 
74 inline constexpr Point operator+(Point const& lhs, Displacement const& rhs)
75 {
76  return Point{lhs.x + rhs.dx, lhs.y + rhs.dy};
77 }
78 
79 inline constexpr Point operator+(Displacement const& lhs, Point const& rhs)
80 {
81  return Point{rhs.x + lhs.dx, rhs.y + lhs.dy};
82 }
83 
84 inline constexpr Point operator-(Point const& lhs, Displacement const& rhs)
85 {
86  return Point{lhs.x - rhs.dx, lhs.y - rhs.dy};
87 }
88 
89 inline constexpr Displacement operator-(Point const& lhs, Point const& rhs)
90 {
91  return Displacement{lhs.x - rhs.x, lhs.y - rhs.y};
92 }
93 
94 inline bool operator<(Displacement const& lhs, Displacement const& rhs)
95 {
96  return lhs.length_squared() < rhs.length_squared();
97 }
98 
99 template<typename Scalar>
100 inline constexpr Displacement operator*(Scalar scale, Displacement const& disp)
101 {
102  return Displacement{scale*disp.dx, scale*disp.dy};
103 }
104 
105 template<typename Scalar>
106 inline constexpr Displacement operator*(Displacement const& disp, Scalar scale)
107 {
108  return scale*disp;
109 }
110 
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 
121 inline constexpr Displacement as_displacement(Point const& point)
122 {
123  return Displacement{point.x.as_int(), point.y.as_int()};
124 }
125 
126 inline constexpr Point as_point(Displacement const& disp)
127 {
128  return Point{disp.dx.as_int(), disp.dy.as_int()};
129 }
130 }
131 }
132 
133 #endif /* MIR_GEOMETRY_DISPLACEMENT_H_ */
Definition: size.h:31
Definition: splash_session.h:24
constexpr Point as_point(Displacement const &disp)
Definition: displacement.h:126
DeltaY dy
Definition: displacement.h:49
X x
Definition: point.h:39
DeltaX dx
Definition: displacement.h:48
Definition: point.h:30
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
Y y
Definition: point.h:40
long long length_squared() const
Definition: displacement.h:42
constexpr Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement.h:40
constexpr int as_int() const
Definition: dimensions.h:53
constexpr Displacement as_displacement(Size const &size)
Definition: displacement.h:111
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:156
Definition: displacement.h:33
constexpr Size as_size(Displacement const &disp)
Definition: displacement.h:116
Displacement & operator=(Displacement const &)=default
Height height
Definition: size.h:41
constexpr Displacement()
Definition: displacement.h:35
std::ostream & operator<<(std::ostream &out, Displacement const &value)
Width width
Definition: size.h:40
bool operator<(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:94
constexpr bool operator==(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:52
constexpr bool operator!=(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:57

Copyright © 2012-2019 Canonical Ltd.
Generated on Wed Aug 28 00:46:47 UTC 2019
This documentation is licensed under the GPL version 2 or 3.