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 constexpr Point& operator+=(Point& lhs, Displacement const& rhs)
95 {
96  return lhs = lhs + rhs;
97 }
98 
99 inline constexpr Point& operator-=(Point& lhs, Displacement const& rhs)
100 {
101  return lhs = lhs - rhs;
102 }
103 
104 inline bool operator<(Displacement const& lhs, Displacement const& rhs)
105 {
106  return lhs.length_squared() < rhs.length_squared();
107 }
108 
109 template<typename Scalar>
110 inline constexpr Displacement operator*(Scalar scale, Displacement const& disp)
111 {
112  return Displacement{scale*disp.dx, scale*disp.dy};
113 }
114 
115 template<typename Scalar>
116 inline constexpr Displacement operator*(Displacement const& disp, Scalar scale)
117 {
118  return scale*disp;
119 }
120 
121 inline constexpr Displacement as_displacement(Size const& size)
122 {
123  return Displacement{size.width.as_int(), size.height.as_int()};
124 }
125 
126 inline constexpr Size as_size(Displacement const& disp)
127 {
128  return Size{disp.dx.as_int(), disp.dy.as_int()};
129 }
130 
131 inline constexpr Displacement as_displacement(Point const& point)
132 {
133  return Displacement{point.x.as_int(), point.y.as_int()};
134 }
135 
136 inline constexpr Point as_point(Displacement const& disp)
137 {
138  return Point{disp.dx.as_int(), disp.dy.as_int()};
139 }
140 }
141 }
142 
143 #endif /* MIR_GEOMETRY_DISPLACEMENT_H_ */
mir::geometry::operator+=
DeltaX & operator+=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:121
point.h
mir::geometry::operator-
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
mir::geometry::detail::IntWrapper< struct DeltaXTag >
mir::geometry::Displacement::dy
DeltaY dy
Definition: displacement.h:49
mir::geometry::operator<
bool operator<(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:104
mir::geometry::Displacement::dx
DeltaX dx
Definition: displacement.h:48
mir::geometry::Point::x
X x
Definition: point.h:39
mir::geometry::operator-=
DeltaX & operator-=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:123
mir::geometry::operator+
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
mir::geometry::operator==
constexpr bool operator==(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:52
mir
Definition: splash_session.h:24
mir::geometry::Displacement::length_squared
long long length_squared() const
Definition: displacement.h:42
mir::geometry::Size::height
Height height
Definition: size.h:41
mir::geometry::as_displacement
constexpr Displacement as_displacement(Size const &size)
Definition: displacement.h:121
mir::geometry::operator*
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:162
mir::geometry::operator!=
constexpr bool operator!=(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:57
mir::geometry::Displacement::Displacement
constexpr Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement.h:40
mir::geometry::Point::y
Y y
Definition: point.h:40
mir::geometry::as_point
constexpr Point as_point(Displacement const &disp)
Definition: displacement.h:136
mir::geometry::Displacement
Definition: displacement.h:33
mir::geometry::as_size
constexpr Size as_size(Displacement const &disp)
Definition: displacement.h:126
mir::geometry::operator<<
std::ostream & operator<<(std::ostream &out, Displacement const &value)
mir::geometry::Size::width
Width width
Definition: size.h:40
mir::geometry::Point
Definition: point.h:30
dimensions.h
mir::geometry::detail::IntWrapper::as_int
constexpr int as_int() const
Definition: dimensions.h:53
mir::geometry::Displacement::Displacement
constexpr Displacement()
Definition: displacement.h:35
mir::geometry::Size
Definition: size.h:31
size.h
mir::geometry::Displacement::operator=
Displacement & operator=(Displacement const &)=default

Copyright © 2012-2021 Canonical Ltd.
Generated on Thu Nov 25 02:27:04 UTC 2021
This documentation is licensed under the GPL version 2 or 3.