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

Copyright © 2012-2022 Canonical Ltd.
Generated on Sat 3 Dec 14:36:51 UTC 2022
This documentation is licensed under the GPL version 2 or 3.