Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
displacement.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012 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 
22 #include "dimensions.h"
23 #include "point.h"
24 
25 #include <ostream>
26 
27 namespace mir
28 {
29 namespace geometry
30 {
31 
33 {
35  Displacement(Displacement const&) = default;
36  Displacement& operator=(Displacement const&) = default;
37 
38  template<typename DeltaXType, typename DeltaYType>
39  Displacement(DeltaXType&& dx, DeltaYType&& dy) : dx{dx}, dy{dy} {}
40 
41  float length_squared() const
42  {
43  return dx.as_float() * dx.as_float() + dy.as_float() * dy.as_float();
44  }
45 
48 };
49 
50 inline bool operator==(Displacement const& lhs, Displacement const& rhs)
51 {
52  return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
53 }
54 
55 inline bool operator!=(Displacement const& lhs, Displacement const& rhs)
56 {
57  return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
58 }
59 
60 inline std::ostream& operator<<(std::ostream& out, Displacement const& value)
61 {
62  out << '(' << value.dx << ", " << value.dy << ')';
63  return out;
64 }
65 
66 inline Displacement operator+(Displacement const& lhs, Displacement const& rhs)
67 {
68  return Displacement{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
69 }
70 
71 inline Displacement operator-(Displacement const& lhs, Displacement const& rhs)
72 {
73  return Displacement{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
74 }
75 
76 inline Point operator+(Point const& lhs, Displacement const& rhs)
77 {
78  return Point{lhs.x + rhs.dx, lhs.y + rhs.dy};
79 }
80 
81 inline Point operator-(Point const& lhs, Displacement const& rhs)
82 {
83  return Point{lhs.x - rhs.dx, lhs.y - rhs.dy};
84 }
85 
86 inline Displacement operator-(Point const& lhs, Point const& rhs)
87 {
88  return Displacement{lhs.x - rhs.x, lhs.y - rhs.y};
89 }
90 
91 inline bool operator<(Displacement const& lhs, Displacement const& rhs)
92 {
93  return lhs.length_squared() < rhs.length_squared();
94 }
95 
96 }
97 }
98 
99 #endif /* MIR_GEOMETRY_DISPLACEMENT_H_ */

Copyright © 2012,2013 Canonical Ltd.
Generated on Wed Oct 30 18:52:19 UTC 2013