DGtal  0.9.2
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 
51  Point( double xc, double yc ):x(xc),y(yc) { }
52 
58  inline Point & rotate( double angle );
59 
66  inline void get( double & xout, double & yout ) const;
67 
75  inline Point rotated( double angle ) const;
76 
77  inline Point & rotate( double angle, const Point & center );
78 
79  inline Point rotated( double angle, const Point & center ) const;
80 
81  inline Point & operator+=( const Point & other );
82 
83  inline Point & operator-=( const Point & other );
84 
85  inline Point & operator*=( double s );
86 
87  inline Point & operator/=( double s );
88 
89  inline Point operator-();
90 
91  inline double norm() const;
92 
93 };
94 
95 inline void
96 Point::get( double & xout, double & yout ) const
97 {
98  xout = x;
99  yout = y;
100 }
101 
102 inline Point
103 operator+( const Point & a, const Point & b )
104 {
105  return Point( a.x + b.x, a.y + b.y );
106 }
107 
108 inline Point
109 operator-( const Point & a, const Point & b )
110 {
111  return Point( a.x - b.x, a.y - b.y );
112 }
113 
114 inline double
115 operator*( const Point & a, const Point & b )
116 {
117  return a.x * b.x + a.y * b.y;
118 }
119 
120 inline Point
121 operator*( const Point & p, double s )
122 {
123  return Point( p.x * s, p.y * s );
124 }
125 
126 inline Point
127 operator*( double s, const Point & p )
128 {
129  return Point( s * p.x, s * p.y );
130 }
131 
132 inline Point
133 operator/( const Point & p, double s )
134 {
135  return Point( p.x / s, p.y / s );
136 }
137 
138 inline Point &
139 Point::operator+=( const Point & other )
140 {
141  x += other.x;
142  y += other.y;
143  return *this;
144 }
145 
146 inline Point &
147 Point::operator-=( const Point & other )
148 {
149  x -= other.x;
150  y -= other.y;
151  return *this;
152 }
153 
154 inline Point &
155 Point::operator*=( double s )
156 {
157  x *= s;
158  y *= s;
159  return *this;
160 }
161 
162 inline Point &
163 Point::operator/=( double s )
164 {
165  x /= s;
166  y /= s;
167  return *this;
168 }
169 
170 inline bool
171 operator==( const Point & a, const Point & b )
172 {
173  return ( a.x == b.x ) && ( a.y == b.y ) ;
174 }
175 
176 inline bool
177 operator!=( const Point & a, const Point & b )
178 {
179  return ( a.x != b.x ) || ( a.y != b.y ) ;
180 }
181 
182 Point &
183 Point::rotate( double angle )
184 {
185  double newx = cos( angle ) * Point::x - sin( angle ) * Point::y;
186  double newy = sin( angle ) * Point::x + cos( angle ) * Point::y;
187  x = newx;
188  y = newy;
189  return *this;
190 }
191 
192 Point
193 Point::rotated( double angle ) const
194 {
195  return Point(*this).rotate( angle );
196 }
197 
198 Point &
199 Point::rotate( double angle, const Point & center )
200 {
201  (*this) -= center;
202  (*this).rotate( angle );
203  (*this) += center;
204  return *this;
205 }
206 
207 Point
208 Point::rotated( double angle, const Point & center ) const
209 {
210  return Point(*this).rotate( angle, center );
211 }
212 
213 double
214 Point::norm() const
215 {
216  return sqrt( x*x + y*y );
217 }
218 
220 {
221  return Point( -x, -y );
222 }
223 
224 } // mamespace BoardLib
225 
226 #endif // _POINT_H_
227 
Point & rotate(double angle)
Definition: Point.h:183
Point operator/(const Point &p, double s)
Definition: Point.h:133
Point(const Point &other)
Definition: Point.h:43
double operator*(const Point &a, const Point &b)
Definition: Point.h:115
Point operator-()
Definition: Point.h:219
Struct representing a 2D point.
Definition: Point.h:27
Point rotated(double angle) const
Definition: Point.h:193
Point(double xc, double yc)
Definition: Point.h:51
double x
Definition: Point.h:29
Point & operator+=(const Point &other)
Definition: Point.h:139
Point & operator*=(double s)
Definition: Point.h:155
void get(double &xout, double &yout) const
Definition: Point.h:96
double norm() const
Definition: Point.h:214
Point & operator-=(const Point &other)
Definition: Point.h:147
Point operator-(const Point &a, const Point &b)
Definition: Point.h:109
bool operator!=(const Point &a, const Point &b)
Definition: Point.h:177
bool operator==(const Point &a, const Point &b)
Definition: Point.h:171
Point & operator/=(double s)
Definition: Point.h:163
double y
Definition: Point.h:30
Point operator+(const Point &a, const Point &b)
Definition: Point.h:103