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