Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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 <iosfwd>
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 std::ostream& operator<<(std::ostream& out, Displacement const& value);
61 
62 inline Displacement operator+(Displacement const& lhs, Displacement const& rhs)
63 {
64  return Displacement{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
65 }
66 
67 inline Displacement operator-(Displacement const& lhs, Displacement const& rhs)
68 {
69  return Displacement{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
70 }
71 
72 inline Point operator+(Point const& lhs, Displacement const& rhs)
73 {
74  return Point{lhs.x + rhs.dx, lhs.y + rhs.dy};
75 }
76 
77 inline Point operator-(Point const& lhs, Displacement const& rhs)
78 {
79  return Point{lhs.x - rhs.dx, lhs.y - rhs.dy};
80 }
81 
82 inline Displacement operator-(Point const& lhs, Point const& rhs)
83 {
84  return Displacement{lhs.x - rhs.x, lhs.y - rhs.y};
85 }
86 
87 inline bool operator<(Displacement const& lhs, Displacement const& rhs)
88 {
89  return lhs.length_squared() < rhs.length_squared();
90 }
91 
92 }
93 }
94 
95 #endif /* MIR_GEOMETRY_DISPLACEMENT_H_ */
DeltaY dy
Definition: displacement.h:47
X x
Definition: point.h:39
float as_float() const
Definition: dimensions.h:55
DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:121
DeltaX dx
Definition: displacement.h:46
Definition: point.h:30
Y y
Definition: point.h:40
bool operator!=(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:55
bool operator==(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:50
Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement.h:39
std::ostream & operator<<(std::ostream &out, Displacement const &value)
Definition: ostream.cpp:29
Definition: displacement.h:32
Displacement & operator=(Displacement const &)=default
DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
float length_squared() const
Definition: displacement.h:41
bool operator<(Displacement const &lhs, Displacement const &rhs)
Definition: displacement.h:87
Displacement()
Definition: displacement.h:34

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Apr 11 21:14:53 UTC 2014