Point.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2010 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_POINT_H
32 #define PT_GFX_POINT_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/Math.h>
36 #include <Pt/Types.h>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
44 class Point
45 {
46  public:
47  Point()
48  : _x(0), _y(0)
49  {}
50 
51  Point(Float x, Float y)
52  : _x(x), _y(y)
53  {}
54 
55  Point(const Point& pt)
56  : _x(pt._x), _y(pt._y)
57  {}
58 
59  void clear()
60  {
61  _x = 0;
62  _y = 0;
63  }
64 
65  void set(Float xpos, Float ypos)
66  {
67  _x = xpos;
68  _y = ypos;
69  }
70 
71  void setX(Float xpos)
72  { _x = xpos; }
73 
74  void setY(Float ypos)
75  { _y = ypos; }
76 
77  Float x() const
78  { return _x; }
79 
80  Float y() const
81  { return _y; }
82 
83  const Point& addX(Float x)
84  {
85  _x += x;
86  return *this;
87  }
88 
89  const Point& subX(Float x)
90  {
91  _x -= x;
92  return *this;
93  }
94 
95  const Point& addY(Float y)
96  {
97  _y += y;
98  return *this;
99  }
100 
101  const Point& subY(Float y)
102  {
103  _y -= y;
104  return *this;
105  }
106 
107  const Point& move(Float dx, Float dy)
108  {
109  _x += dx;
110  _y += dy;
111  return *this;
112  }
113 
114  Float calcDistance(const Point& other) const
115  {
116  if(*this == other)
117  return 0;
118 
119  return hypot(_x - other._x, _y - other._y);
120  }
121 
122  Point& operator=(const Point& pt)
123  {
124  _x = pt._x;
125  _y = pt._y;
126  return *this;
127  }
128 
129  bool operator==(const Point& pt) const
130  { return (_x == pt._x && _y == pt._y); }
131 
132  bool operator!=(const Point& pt) const
133  { return (_x != pt._x || _y != pt._y); }
134 
135  Point& operator+=(const Point& pt)
136  {
137  _x += pt._x;
138  _y += pt._y;
139  return *this;
140  }
141 
142  Point operator+(const Point& pt) const
143  {
144  return Point(_x + pt._x, _y + pt._y);
145  }
146 
147  Point& operator-=(const Point& pt)
148  {
149  _x -= pt._x;
150  _y -= pt._y;
151  return *this;
152  }
153 
154  Point operator-(const Point& pt) const
155  {
156  return Point(_x - pt._x, _y - pt._y);
157  }
158 
159  Point operator*(Float factor) const
160  {
161  return Point(_x * factor, _y * factor);
162  }
163 
164  Point operator/(Float factor) const
165  {
166  return Point(_x / factor, _y / factor);
167  }
168 
169  Point operator+(Float factor) const
170  {
171  return Point(_x + factor, _y + factor);
172  }
173 
174  Point operator-(Float factor) const
175  {
176  return Point(_x - factor, _y - factor);
177  }
178 
179  Point& operator*=(Float factor)
180  {
181  _x *= factor;
182  _y *= factor;
183  return *this;
184  }
185 
186  Point& operator/=(Float factor)
187  {
188  _x /= factor;
189  _y /= factor;
190  return *this;
191  }
192 
193  private:
194  Float _x;
195  Float _y;
196 };
197 
198 typedef Point PointF;
199 
202 class PointI
203 {
204  public:
205  PointI()
206  : _x(0), _y(0)
207  {}
208 
209  PointI(Int x, Int y)
210  : _x(x), _y(y)
211  {}
212 
213  PointI(const PointI& pt)
214  : _x(pt._x), _y(pt._y)
215  {}
216 
217  void clear()
218  {
219  _x = 0;
220  _y = 0;
221  }
222 
223  void set(Int xpos, Int ypos)
224  {
225  _x = xpos;
226  _y = ypos;
227  }
228 
229  void setX(Int xpos)
230  { _x = xpos; }
231 
232  void setY(Int ypos)
233  { _y = ypos; }
234 
235  Int x() const
236  { return _x; }
237 
238  Int y() const
239  { return _y; }
240 
241  const PointI& addX(Int x)
242  {
243  _x += x;
244  return *this;
245  }
246 
247  const PointI& subX(Int x)
248  {
249  _x -= x;
250  return *this;
251  }
252 
253  const PointI& addY(Int y)
254  {
255  _y += y;
256  return *this;
257  }
258 
259  const PointI& subY(Int y)
260  {
261  _y -= y;
262  return *this;
263  }
264 
265  const PointI& move(Int dx, Int dy)
266  {
267  _x += dx;
268  _y += dy;
269  return *this;
270  }
271 
272  PointI& operator=(const PointI& pt)
273  {
274  _x = pt._x;
275  _y = pt._y;
276  return *this;
277  }
278 
279  bool operator==(const PointI& pt) const
280  { return (_x == pt._x && _y == pt._y); }
281 
282  bool operator!=(const PointI& pt) const
283  { return (_x != pt._x || _y != pt._y); }
284 
285  PointI& operator+=(const PointI& pt)
286  {
287  _x += pt._x;
288  _y += pt._y;
289  return *this;
290  }
291 
292  PointI operator+(const PointI& pt) const
293  {
294  return PointI(_x + pt._x, _y + pt._y);
295  }
296 
297  PointI& operator-=(const PointI& pt)
298  {
299  _x -= pt._x;
300  _y -= pt._y;
301  return *this;
302  }
303 
304  PointI operator-(const PointI& pt) const
305  {
306  return PointI(_x - pt._x, _y - pt._y);
307  }
308 
309  PointI operator*(Int factor) const
310  {
311  return PointI(_x * factor, _y * factor);
312  }
313 
314  PointI operator+(Int offset) const
315  {
316  return PointI(_x + offset, _y + offset);
317  }
318 
319  PointI operator-(Int offset) const
320  {
321  return PointI(_x - offset, _y - offset);
322  }
323 
324  PointI& operator*=(Int factor)
325  {
326  _x *= factor;
327  _y *= factor;
328  return *this;
329  }
330 
331  private:
332  Int _x;
333  Int _y;
334 };
335 
336 } // namespace
337 
338 } // namespace
339 
340 #endif
341 
Core module.
Definition: pt-gfx-images.dox:14
Point with floating-point X and Y coordinates.
Definition: Point.h:45
Point with integer X and Y coordinates.
Definition: Point.h:203
double hypot(double x, double y)
Return the euclidean distance of the given values.
Definition: Math.h:275