DGtal  1.2.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 
Point operator+(const Point &a, const Point &b)
Definition: Point.h:110
Point operator-(const Point &a, const Point &b)
Definition: Point.h:116
Point operator/(const Point &p, double s)
Definition: Point.h:140
bool operator!=(const Point &a, const Point &b)
Definition: Point.h:184
bool operator==(const Point &a, const Point &b)
Definition: Point.h:178
double operator*(const Point &a, const Point &b)
Definition: Point.h:122
Struct representing a 2D point.
Definition: Point.h:27
Point & operator-=(const Point &other)
Definition: Point.h:154
Point operator-()
Definition: Point.h:226
Point & operator+=(const Point &other)
Definition: Point.h:146
Point & operator/=(double s)
Definition: Point.h:170
double y
Definition: Point.h:30
void get(double &xout, double &yout) const
Definition: Point.h:103
Point & operator*=(double s)
Definition: Point.h:162
Point rotated(double angle) const
Definition: Point.h:200
Point(double xc, double yc)
Definition: Point.h:58
Point(const Point &other)
Definition: Point.h:43
double x
Definition: Point.h:29
Point & rotate(double angle)
Definition: Point.h:190
double norm() const
Definition: Point.h:221
Point & operator=(const Point &other)=default
MyPointD Point
Definition: testClone2.cpp:383