DGtal  1.1.0
Point.h
1 /* -*- mode: c++ -*- */
9 /*
10  * \@copyright This File is part of the Board library which is
11  * licensed under the terms of the GNU Lesser General Public Licence.
12  * See the LICENCE file for further details.
13  */
14 #ifndef _BOARD_POINT_H_
15 #define _BOARD_POINT_H_
16
17 #include <cmath>
18 #include "DGtal/io/Color.h"
19
20 namespace LibBoard {
21
22
27 struct Point {
28
29  double x;
30  double y;
36  Point():x(0.0),y(0.0) { }
37
43  Point( const Point & other ):x(other.x),y(other.y) { }
44
50  Point &operator=( const Point & other ) = default;
51
58  Point( double xc, double yc ):x(xc),y(yc) { }
59
65  inline Point & rotate( double angle );
66
73  inline void get( double & xout, double & yout ) const;
74
82  inline Point rotated( double angle ) const;
83
84  inline Point & rotate( double angle, const Point & center );
85
86  inline Point rotated( double angle, const Point & center ) const;
87
88  inline Point & operator+=( const Point & other );
89
90  inline Point & operator-=( const Point & other );
91
92  inline Point & operator*=( double s );
93
94  inline Point & operator/=( double s );
95
96  inline Point operator-();
97
98  inline double norm() const;
99
100 };
101
102 inline void
103 Point::get( double & xout, double & yout ) const
104 {
105  xout = x;
106  yout = y;
107 }
108
109 inline Point
110 operator+( const Point & a, const Point & b )
111 {
112  return Point( a.x + b.x, a.y + b.y );
113 }
114
115 inline Point
116 operator-( const Point & a, const Point & b )
117 {
118  return Point( a.x - b.x, a.y - b.y );
119 }
120
121 inline double
122 operator*( const Point & a, const Point & b )
123 {
124  return a.x * b.x + a.y * b.y;
125 }
126
127 inline Point
128 operator*( const Point & p, double s )
129 {
130  return Point( p.x * s, p.y * s );
131 }
132
133 inline Point
134 operator*( double s, const Point & p )
135 {
136  return Point( s * p.x, s * p.y );
137 }
138
139 inline Point
140 operator/( const Point & p, double s )
141 {
142  return Point( p.x / s, p.y / s );
143 }
144
145 inline Point &
146 Point::operator+=( const Point & other )
147 {
148  x += other.x;
149  y += other.y;
150  return *this;
151 }
152
153 inline Point &
154 Point::operator-=( const Point & other )
155 {
156  x -= other.x;
157  y -= other.y;
158  return *this;
159 }
160
161 inline Point &
162 Point::operator*=( double s )
163 {
164  x *= s;
165  y *= s;
166  return *this;
167 }
168
169 inline Point &
170 Point::operator/=( double s )
171 {
172  x /= s;
173  y /= s;
174  return *this;
175 }
176
177 inline bool
178 operator==( const Point & a, const Point & b )
179 {
180  return ( a.x == b.x ) && ( a.y == b.y ) ;
181 }
182
183 inline bool
184 operator!=( const Point & a, const Point & b )
185 {
186  return ( a.x != b.x ) || ( a.y != b.y ) ;
187 }
188
189 Point &
190 Point::rotate( double angle )
191 {
192  double newx = cos( angle ) * Point::x - sin( angle ) * Point::y;
193  double newy = sin( angle ) * Point::x + cos( angle ) * Point::y;
194  x = newx;
195  y = newy;
196  return *this;
197 }
198
199 Point
200 Point::rotated( double angle ) const
201 {
202  return Point(*this).rotate( angle );
203 }
204
205 Point &
206 Point::rotate( double angle, const Point & center )
207 {
208  (*this) -= center;
209  (*this).rotate( angle );
210  (*this) += center;
211  return *this;
212 }
213
214 Point
215 Point::rotated( double angle, const Point & center ) const
216 {
217  return Point(*this).rotate( angle, center );
218 }
219
220 double
221 Point::norm() const
222 {
223  return sqrt( x*x + y*y );
224 }
225
227 {
228  return Point( -x, -y );
229 }
230
231 } // mamespace BoardLib
232
233 #endif // _POINT_H_
234
LibBoard::operator==
bool operator==(const Point &a, const Point &b)
Definition: Point.h:178
LibBoard::operator/
Point operator/(const Point &p, double s)
Definition: Point.h:140
LibBoard::Point::operator/=
Point & operator/=(double s)
Definition: Point.h:170
LibBoard::Point::Point
Point(const Point &other)
Definition: Point.h:43
LibBoard::Point::rotate
Point & rotate(double angle)
Definition: Point.h:190
LibBoard::Point::operator-
Point operator-()
Definition: Point.h:226
LibBoard::Point::x
double x
Definition: Point.h:29
LibBoard::Point
Struct representing a 2D point.
Definition: Point.h:27
LibBoard::Point::operator+=
Point & operator+=(const Point &other)
Definition: Point.h:146
LibBoard::Point::Point
Point(double xc, double yc)
Definition: Point.h:58
LibBoard::Point::operator-=
Point & operator-=(const Point &other)
Definition: Point.h:154
LibBoard
Definition: Board.cpp:87
LibBoard::operator*
double operator*(const Point &a, const Point &b)
Definition: Point.h:122
LibBoard::Point::get
void get(double &xout, double &yout) const
Definition: Point.h:103
LibBoard::Point::operator=
Point & operator=(const Point &other)=default
LibBoard::Point::operator*=
Point & operator*=(double s)
Definition: Point.h:162
LibBoard::operator-
Point operator-(const Point &a, const Point &b)
Definition: Point.h:116
LibBoard::Point::norm
double norm() const
Definition: Point.h:221
LibBoard::Point::Point
Point()
Definition: Point.h:36
LibBoard::operator+
Point operator+(const Point &a, const Point &b)
Definition: Point.h:110
LibBoard::Point::rotated
Point rotated(double angle) const
Definition: Point.h:200
Point
MyPointD Point
Definition: testClone2.cpp:383
LibBoard::Point::y
double y
Definition: Point.h:30
LibBoard::operator!=
bool operator!=(const Point &a, const Point &b)
Definition: Point.h:184