]> git.saurik.com Git - wxWidgets.git/blob - include/wx/geometry.h
added container class files
[wxWidgets.git] / include / wx / geometry.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: geometry.h
3 // Purpose: Common Geometry Classes
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 08/05/99
7 // RCS-ID:
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_GEOMETRY_H_
13 #define _WX_GEOMETRY_H_
14
15 #ifdef __GNUG__
16 #pragma interface "geometry.cpp"
17 #endif
18
19 #include "wx/defs.h"
20
21 #ifndef wxUSE_GEOMETRY
22 #define wxUSE_GEOMETRY 0
23 #endif
24
25 #if wxUSE_GEOMETRY
26
27 #include "wx/utils.h"
28 #include "wx/gdicmn.h"
29 #include <math.h>
30
31 #ifdef __WXMSW__
32 #define wxMulDivInt32( a , b , c ) ::MulDiv( a , b , c )
33 #elif defined( __WXMAC__ )
34 #ifndef __DARWIN__
35 #include "Math64.h"
36 #endif
37 #define wxMulDivInt32( a , b , c ) S32Set( S64Div( S64Multiply( S64Set(a) , S64Set(b) ) , S64Set(c) ) )
38 #else
39 #define wxMulDivInt32( a , b , c ) ((wxInt32)((a)*(((wxDouble)b)/((wxDouble)c))))
40 #endif
41
42 class wxDataInputStream ;
43 class wxDataOutputStream ;
44
45 // clipping from Cohen-Sutherland
46
47 enum wxOutCode
48 {
49 wxInside = 0x00 ,
50 wxOutLeft = 0x01 ,
51 wxOutRight = 0x02 ,
52 wxOutTop = 0x08 ,
53 wxOutBottom = 0x04
54 } ;
55
56 // wxPoint2Ds represent a point or a vector in a 2d coordinate system
57
58 class WXDLLEXPORT wxPoint2DDouble
59 {
60 public :
61 inline wxPoint2DDouble();
62 inline wxPoint2DDouble( wxDouble x , wxDouble y ) ;
63 inline wxPoint2DDouble( const wxPoint2DDouble &pt ) ;
64
65 // two different conversions to integers, floor and rounding
66 inline void GetFloor( wxInt32 *x , wxInt32 *y ) ;
67 inline void GetRounded( wxInt32 *x , wxInt32 *y ) ;
68
69 inline wxDouble GetVectorLength() ;
70 inline wxDouble GetVectorAngle() ;
71 void SetVectorLength( wxDouble length ) ;
72 void SetVectorAngle( wxDouble degrees ) ;
73 void SetPolarCoordinates( wxDouble angle , wxDouble length ) ;
74 // set the vector length to 1.0, preserving the angle
75 void Normalize() ;
76
77 inline wxDouble GetDistance( const wxPoint2DDouble &pt ) ;
78 inline wxDouble GetDistanceSquare( const wxPoint2DDouble &pt ) ;
79 inline wxDouble GetDotProduct( const wxPoint2DDouble &vec ) ;
80 inline wxDouble GetCrossProduct( const wxPoint2DDouble &vec ) ;
81
82 // the reflection of this point
83 inline wxPoint2DDouble operator-() ;
84
85 inline wxPoint2DDouble& operator=(const wxPoint2DDouble& pt) ;
86 inline wxPoint2DDouble& operator+=(const wxPoint2DDouble& pt) ;
87 inline wxPoint2DDouble& operator-=(const wxPoint2DDouble& pt) ;
88 inline wxPoint2DDouble& operator*=(const wxPoint2DDouble& pt) ;
89 inline wxPoint2DDouble& operator*=(wxDouble n) ;
90 inline wxPoint2DDouble& operator*=(wxInt32 n) ;
91 inline wxPoint2DDouble& operator/=(const wxPoint2DDouble& pt) ;
92 inline wxPoint2DDouble& operator/=(wxDouble n) ;
93 inline wxPoint2DDouble& operator/=(wxInt32 n) ;
94
95 inline bool operator==(const wxPoint2DDouble& pt) const ;
96 inline bool operator!=(const wxPoint2DDouble& pt) const ;
97
98 wxDouble m_x ;
99 wxDouble m_y ;
100 } ;
101
102 wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
103 wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
104 wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
105 wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt) ;
106 wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt) ;
107 wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n) ;
108 wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n) ;
109 wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
110 wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n) ;
111 wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n) ;
112
113 inline wxPoint2DDouble::wxPoint2DDouble()
114 {
115 m_x = 0.0 ;
116 m_y = 0.0 ;
117 }
118
119 inline wxPoint2DDouble::wxPoint2DDouble( wxDouble x , wxDouble y )
120 {
121 m_x = x ;
122 m_y = y ;
123 }
124
125 inline wxPoint2DDouble::wxPoint2DDouble( const wxPoint2DDouble &pt )
126 {
127 m_x = pt.m_x ;
128 m_y = pt.m_y ;
129 }
130
131 inline void wxPoint2DDouble::GetFloor( wxInt32 *x , wxInt32 *y )
132 {
133 *x = (wxInt32) floor( m_x ) ;
134 *y = (wxInt32) floor( m_y ) ;
135 }
136
137 inline void wxPoint2DDouble::GetRounded( wxInt32 *x , wxInt32 *y )
138 {
139 *x = (wxInt32) floor( m_x + 0.5 ) ;
140 *y = (wxInt32) floor( m_y + 0.5) ;
141 }
142
143 inline wxDouble wxPoint2DDouble::GetDistance( const wxPoint2DDouble &pt )
144 {
145 return sqrt( GetDistanceSquare( pt ) );
146 }
147
148 inline wxDouble wxPoint2DDouble::GetDistanceSquare( const wxPoint2DDouble &pt )
149 {
150 return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) ) ;
151 }
152
153 inline wxDouble wxPoint2DDouble::GetDotProduct( const wxPoint2DDouble &vec )
154 {
155 return ( m_x * vec.m_x + m_y * vec.m_y ) ;
156 }
157
158 inline wxDouble wxPoint2DDouble::GetCrossProduct( const wxPoint2DDouble &vec )
159 {
160 return ( m_x * vec.m_y - vec.m_x * m_y ) ;
161 }
162
163 inline wxPoint2DDouble wxPoint2DDouble::operator-()
164 {
165 return wxPoint2DDouble( -m_x, -m_y);
166 }
167
168 inline wxPoint2DDouble& wxPoint2DDouble::operator=(const wxPoint2DDouble& pt)
169 {
170 m_x = pt.m_x ;
171 m_y = pt.m_y;
172 return *this ;
173 }
174
175 inline wxPoint2DDouble& wxPoint2DDouble::operator+=(const wxPoint2DDouble& pt)
176 {
177 m_x = m_x + pt.m_x ;
178 m_y = m_y + pt.m_y;
179 return *this ;
180 }
181
182 inline wxPoint2DDouble& wxPoint2DDouble::operator-=(const wxPoint2DDouble& pt)
183 {
184 m_x = m_x - pt.m_x ;
185 m_y = m_y - pt.m_y;
186 return *this ;
187 }
188
189 inline wxPoint2DDouble& wxPoint2DDouble::operator*=(const wxPoint2DDouble& pt)
190 {
191 m_x = m_x * pt.m_x ;
192 m_y = m_y * pt.m_y;
193 return *this ;
194 }
195
196 inline wxPoint2DDouble& wxPoint2DDouble::operator/=(const wxPoint2DDouble& pt)
197 {
198 m_x = m_x / pt.m_x ;
199 m_y = m_y / pt.m_y;
200 return *this ;
201 }
202
203 inline bool wxPoint2DDouble::operator==(const wxPoint2DDouble& pt) const
204 {
205 return m_x == pt.m_x && m_y == pt.m_y;
206 }
207
208 inline bool wxPoint2DDouble::operator!=(const wxPoint2DDouble& pt) const
209 {
210 return m_x != pt.m_x || m_y != pt.m_y;
211 }
212
213 inline wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
214 {
215 return wxPoint2DDouble( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y ) ;
216 }
217
218 inline wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
219 {
220 return wxPoint2DDouble( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y ) ;
221 }
222
223
224 inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
225 {
226 return wxPoint2DDouble( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y ) ;
227 }
228
229 inline wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt)
230 {
231 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
232 }
233
234 inline wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt)
235 {
236 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
237 }
238
239 inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n)
240 {
241 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
242 }
243
244 inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n)
245 {
246 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
247 }
248
249 inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
250 {
251 return wxPoint2DDouble( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y ) ;
252 }
253
254 inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n)
255 {
256 return wxPoint2DDouble( pt.m_x / n , pt.m_y / n ) ;
257 }
258
259 inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n)
260 {
261 return wxPoint2DDouble( pt.m_x / n , pt.m_y / n ) ;
262 }
263
264 // wxRect2Ds are a axis-aligned rectangles, each side of the rect is parallel to the x- or m_y- axis. The rectangle is either defined by the
265 // top left and bottom right corner, or by the top left corner and size. A point is contained within the rectangle if
266 // left <= x < right and top <= m_y < bottom , thus it is a half open interval.
267
268 class WXDLLEXPORT wxRect2DDouble
269 {
270 public:
271 wxRect2DDouble()
272 { m_x = m_y = m_width = m_height = 0 ; }
273 wxRect2DDouble(wxDouble x, wxDouble y, wxDouble w, wxDouble h)
274 { m_x = x ; m_y = y ; m_width = w ; m_height = h ; }
275 /*
276 wxRect2DDouble(const wxPoint2DDouble& topLeft, const wxPoint2DDouble& bottomRight);
277 wxRect2DDouble(const wxPoint2DDouble& pos, const wxSize& size);
278 wxRect2DDouble(const wxRect2DDouble& rect);
279 */
280 // single attribute accessors
281
282 inline wxPoint2DDouble GetPosition()
283 { return wxPoint2DDouble(m_x, m_y); }
284 inline wxSize GetSize()
285 { return wxSize((int) m_width, (int) m_height); }
286
287 // for the edge and corner accessors there are two setters conterparts, the Set.. functions keep the other corners at their
288 // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners apropriately
289
290 inline wxDouble GetLeft() const { return m_x; }
291 inline void SetLeft( wxDouble n ) { m_width += m_x - n ; m_x = n ; }
292 inline void MoveLeftTo( wxDouble n ) { m_x = n ; }
293 inline wxDouble GetTop() const { return m_y; }
294 inline void SetTop( wxDouble n ) { m_height += m_y - n ; m_y = n ; }
295 inline void MoveTopTo( wxDouble n ) { m_y = n ; }
296 inline wxDouble GetBottom() const { return m_y + m_height; }
297 inline void SetBottom( wxDouble n ) { m_height += n - (m_y+m_height) ;}
298 inline void MoveBottomTo( wxDouble n ) { m_y = n - m_height ; }
299 inline wxDouble GetRight() const { return m_x + m_width; }
300 inline void SetRight( wxDouble n ) { m_width += n - (m_x+m_width) ; }
301 inline void MoveRightTo( wxDouble n ) { m_x = n - m_width ; }
302
303 inline wxPoint2DDouble GetLeftTop() const
304 { return wxPoint2DDouble( m_x , m_y ) ; }
305 inline void SetLeftTop( const wxPoint2DDouble &pt )
306 { m_width += m_x - pt.m_x ; m_height += m_y - pt.m_y ; m_x = pt.m_x ; m_y = pt.m_y ; }
307 inline void MoveLeftTopTo( const wxPoint2DDouble &pt )
308 { m_x = pt.m_x ; m_y = pt.m_y ; }
309 inline wxPoint2DDouble GetLeftBottom() const
310 { return wxPoint2DDouble( m_x , m_y + m_height ) ; }
311 inline void SetLeftBottom( const wxPoint2DDouble &pt )
312 { m_width += m_x - pt.m_x ; m_height += pt.m_y - (m_y+m_height) ; m_x = pt.m_x ; }
313 inline void MoveLeftBottomTo( const wxPoint2DDouble &pt )
314 { m_x = pt.m_x ; m_y = pt.m_y - m_height; }
315 inline wxPoint2DDouble GetRightTop() const
316 { return wxPoint2DDouble( m_x+m_width , m_y ) ; }
317 inline void SetRightTop( const wxPoint2DDouble &pt )
318 { m_width += pt.m_x - ( m_x + m_width ) ; m_height += m_y - pt.m_y ; m_y = pt.m_y ; }
319 inline void MoveRightTopTo( const wxPoint2DDouble &pt )
320 { m_x = pt.m_x - m_width ; m_y = pt.m_y ; }
321 inline wxPoint2DDouble GetRightBottom() const
322 { return wxPoint2DDouble( m_x+m_width , m_y + m_height ) ; }
323 inline void SetRightBottom( const wxPoint2DDouble &pt )
324 { m_width += pt.m_x - ( m_x + m_width ) ; m_height += pt.m_y - (m_y+m_height) ;}
325 inline void MoveRightBottomTo( const wxPoint2DDouble &pt )
326 { m_x = pt.m_x - m_width ; m_y = pt.m_y - m_height; }
327 inline wxPoint2DDouble GetCentre() const
328 { return wxPoint2DDouble( m_x+m_width/2 , m_y+m_height/2 ) ; }
329 inline void SetCentre( const wxPoint2DDouble &pt )
330 { MoveCentreTo( pt ) ; } // since this is impossible without moving...
331 inline void MoveCentreTo( const wxPoint2DDouble &pt )
332 { m_x += pt.m_x - (m_x+m_width/2) , m_y += pt.m_y -(m_y+m_height/2) ; }
333 inline wxOutCode GetOutcode( const wxPoint2DDouble &pt ) const
334 { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
335 ( ( pt.m_x >= m_x + m_width ) ? wxOutRight : 0 ) +
336 ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
337 ( ( pt.m_y >= m_y + m_height ) ? wxOutBottom : 0 )) ; }
338 inline bool Contains( const wxPoint2DDouble &pt ) const
339 { return GetOutcode( pt ) == wxInside ; }
340 inline bool Contains( const wxRect2DDouble &rect ) const
341 { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
342 ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ) ; }
343 inline bool IsEmpty() const
344 { return ( m_width <= 0 || m_height <= 0 ) ; }
345 inline bool HaveEqualSize( const wxRect2DDouble &rect ) const
346 { return ( rect.m_width == m_width && rect.m_height == m_height ) ; }
347
348 inline void Inset( wxDouble x , wxDouble y )
349 { m_x += x ; m_y += y ; m_width -= 2 * x ; m_height -= 2 * y ; }
350 inline void Inset( wxDouble left , wxDouble top ,wxDouble right , wxDouble bottom )
351 { m_x += left ; m_y += top ; m_width -= left + right ; m_height -= top + bottom ;}
352 inline void Offset( const wxPoint2DDouble &pt )
353 { m_x += pt.m_x ; m_y += pt.m_y ; }
354
355 void ConstrainTo( const wxRect2DDouble &rect );
356
357 inline wxPoint2DDouble Interpolate( wxInt32 widthfactor , wxInt32 heightfactor )
358 { return wxPoint2DDouble( m_x + m_width * widthfactor , m_y + m_height * heightfactor ) ; }
359
360 static void Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) ;
361 inline void Intersect( const wxRect2DDouble &otherRect )
362 { Intersect( *this , otherRect , this ) ; }
363 inline wxRect2DDouble CreateIntersection( const wxRect2DDouble &otherRect ) const
364 { wxRect2DDouble result ; Intersect( *this , otherRect , &result) ; return result ; }
365 bool Intersects( const wxRect2DDouble &rect ) const ;
366
367 static void Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) ;
368 void Union( const wxRect2DDouble &otherRect )
369 { Union( *this , otherRect , this ) ; }
370 void Union( const wxPoint2DDouble &pt ) ;
371 inline wxRect2DDouble CreateUnion( const wxRect2DDouble &otherRect ) const
372 { wxRect2DDouble result ; Union( *this , otherRect , &result) ; return result ; }
373
374 inline void Scale( wxDouble f )
375 { m_x *= f ; m_y *= f ; m_width *= f ; m_height *= f ;}
376 inline void Scale( wxInt32 num , wxInt32 denum )
377 { m_x *= ((wxDouble)num)/((wxDouble)denum) ; m_y *= ((wxDouble)num)/((wxDouble)denum) ;
378 m_width *= ((wxDouble)num)/((wxDouble)denum) ; m_height *= ((wxDouble)num)/((wxDouble)denum) ;}
379
380 /*
381 wxRect2DDouble& operator = (const wxRect2DDouble& rect);
382 bool operator == (const wxRect2DDouble& rect);
383 bool operator != (const wxRect2DDouble& rect);
384 */
385
386 wxDouble m_x;
387 wxDouble m_y ;
388 wxDouble m_width;
389 wxDouble m_height;
390 };
391
392 class WXDLLEXPORT wxPoint2DInt
393 {
394 public :
395 inline wxPoint2DInt();
396 inline wxPoint2DInt( wxInt32 x , wxInt32 y ) ;
397 inline wxPoint2DInt( const wxPoint2DInt &pt ) ;
398 inline wxPoint2DInt( const wxPoint &pt ) ;
399
400 // two different conversions to integers, floor and rounding
401 inline void GetFloor( wxInt32 *x , wxInt32 *y ) ;
402 inline void GetRounded( wxInt32 *x , wxInt32 *y ) ;
403
404 inline wxDouble GetVectorLength() ;
405 wxDouble GetVectorAngle() ;
406 inline void SetVectorLength( wxDouble length ) ;
407 void SetVectorAngle( wxDouble degrees ) ;
408 void SetPolarCoordinates( wxInt32 angle , wxInt32 length ) ;
409 // set the vector length to 1.0, preserving the angle
410 inline void Normalize() ;
411
412 inline wxDouble GetDistance( const wxPoint2DInt &pt ) const ;
413 inline wxDouble GetDistanceSquare( const wxPoint2DInt &pt ) const;
414 inline wxInt32 GetDotProduct( const wxPoint2DInt &vec ) const;
415 inline wxInt32 GetCrossProduct( const wxPoint2DInt &vec ) const;
416
417 // the reflection of this point
418 inline wxPoint2DInt operator-() ;
419
420 inline wxPoint2DInt& operator=(const wxPoint2DInt& pt) ;
421 inline wxPoint2DInt& operator+=(const wxPoint2DInt& pt) ;
422 inline wxPoint2DInt& operator-=(const wxPoint2DInt& pt) ;
423 inline wxPoint2DInt& operator*=(const wxPoint2DInt& pt) ;
424 inline wxPoint2DInt& operator*=(wxDouble n) ;
425 inline wxPoint2DInt& operator*=(wxInt32 n) ;
426 inline wxPoint2DInt& operator/=(const wxPoint2DInt& pt) ;
427 inline wxPoint2DInt& operator/=(wxDouble n) ;
428 inline wxPoint2DInt& operator/=(wxInt32 n) ;
429 inline operator wxPoint() const ;
430 inline bool operator==(const wxPoint2DInt& pt) const ;
431 inline bool operator!=(const wxPoint2DInt& pt) const ;
432
433 void WriteTo( wxDataOutputStream &stream ) const ;
434 void ReadFrom( wxDataInputStream &stream ) ;
435
436 wxInt32 m_x ;
437 wxInt32 m_y ;
438 } ;
439
440 wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
441 wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
442 wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
443 wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
444 wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
445 wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
446 wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
447 wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
448 wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
449 wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
450
451 inline wxPoint2DInt::wxPoint2DInt()
452 {
453 m_x = 0 ;
454 m_y = 0 ;
455 }
456
457 inline wxPoint2DInt::wxPoint2DInt( wxInt32 x , wxInt32 y )
458 {
459 m_x = x ;
460 m_y = y ;
461 }
462
463 inline wxPoint2DInt::wxPoint2DInt( const wxPoint2DInt &pt )
464 {
465 m_x = pt.m_x ;
466 m_y = pt.m_y ;
467 }
468
469 inline wxPoint2DInt::wxPoint2DInt( const wxPoint &pt )
470 {
471 m_x = pt.x ;
472 m_y = pt.y ;
473 }
474
475 inline void wxPoint2DInt::GetFloor( wxInt32 *x , wxInt32 *y )
476 {
477 *x = (wxInt32) floor( m_x ) ;
478 *y = (wxInt32) floor( m_y ) ;
479 }
480
481 inline void wxPoint2DInt::GetRounded( wxInt32 *x , wxInt32 *y )
482 {
483 *x = (wxInt32) floor( m_x + 0.5 ) ;
484 *y = (wxInt32) floor( m_y + 0.5) ;
485 }
486
487 inline wxDouble wxPoint2DInt::GetVectorLength()
488 {
489 return sqrt( (m_x)*(m_x) + (m_y)*(m_y) ) ;
490 }
491
492 inline void wxPoint2DInt::SetVectorLength( wxDouble length )
493 {
494 wxDouble before = GetVectorLength() ;
495 m_x = (wxInt32)(m_x * length / before) ;
496 m_y = (wxInt32)(m_y * length / before) ;
497 }
498
499 inline void wxPoint2DInt::Normalize()
500 {
501 SetVectorLength( 1 ) ;
502 }
503
504 inline wxDouble wxPoint2DInt::GetDistance( const wxPoint2DInt &pt ) const
505 {
506 return sqrt( GetDistanceSquare( pt ) );
507 }
508
509 inline wxDouble wxPoint2DInt::GetDistanceSquare( const wxPoint2DInt &pt ) const
510 {
511 return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) ) ;
512 }
513
514 inline wxInt32 wxPoint2DInt::GetDotProduct( const wxPoint2DInt &vec ) const
515 {
516 return ( m_x * vec.m_x + m_y * vec.m_y ) ;
517 }
518
519 inline wxInt32 wxPoint2DInt::GetCrossProduct( const wxPoint2DInt &vec ) const
520 {
521 return ( m_x * vec.m_y - vec.m_x * m_y ) ;
522 }
523
524 inline wxPoint2DInt::operator wxPoint() const
525 {
526 return wxPoint( m_x, m_y);
527 }
528
529 inline wxPoint2DInt wxPoint2DInt::operator-()
530 {
531 return wxPoint2DInt( -m_x, -m_y);
532 }
533
534 inline wxPoint2DInt& wxPoint2DInt::operator=(const wxPoint2DInt& pt)
535 {
536 m_x = pt.m_x ;
537 m_y = pt.m_y;
538 return *this ;
539 }
540
541 inline wxPoint2DInt& wxPoint2DInt::operator+=(const wxPoint2DInt& pt)
542 {
543 m_x = m_x + pt.m_x ;
544 m_y = m_y + pt.m_y;
545 return *this ;
546 }
547
548 inline wxPoint2DInt& wxPoint2DInt::operator-=(const wxPoint2DInt& pt)
549 {
550 m_x = m_x - pt.m_x ;
551 m_y = m_y - pt.m_y;
552 return *this ;
553 }
554
555 inline wxPoint2DInt& wxPoint2DInt::operator*=(const wxPoint2DInt& pt)
556 {
557 m_x = m_x + pt.m_x ;
558 m_y = m_y + pt.m_y;
559 return *this ;
560 }
561
562 inline wxPoint2DInt& wxPoint2DInt::operator/=(const wxPoint2DInt& pt)
563 {
564 m_x = m_x - pt.m_x ;
565 m_y = m_y - pt.m_y;
566 return *this ;
567 }
568
569 inline bool wxPoint2DInt::operator==(const wxPoint2DInt& pt) const
570 {
571 return m_x == pt.m_x && m_y == pt.m_y;
572 }
573
574 inline bool wxPoint2DInt::operator!=(const wxPoint2DInt& pt) const
575 {
576 return m_x != pt.m_x || m_y != pt.m_y;
577 }
578
579 inline wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
580 {
581 return wxPoint2DInt( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y ) ;
582 }
583
584 inline wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
585 {
586 return wxPoint2DInt( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y ) ;
587 }
588
589
590 inline wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
591 {
592 return wxPoint2DInt( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y ) ;
593 }
594
595 inline wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt)
596 {
597 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
598 }
599
600 inline wxPoint2DInt operator*(wxDouble n , const wxPoint2DInt& pt)
601 {
602 return wxPoint2DInt( (int) (pt.m_x * n) , (int) (pt.m_y * n) ) ;
603 }
604
605 inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n)
606 {
607 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
608 }
609
610 inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxDouble n)
611 {
612 return wxPoint2DInt( (int) (pt.m_x * n) , (int) (pt.m_y * n) ) ;
613 }
614
615 inline wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
616 {
617 return wxPoint2DInt( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y ) ;
618 }
619
620 inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n)
621 {
622 return wxPoint2DInt( pt.m_x / n , pt.m_y / n ) ;
623 }
624
625 inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxDouble n)
626 {
627 return wxPoint2DInt( (int) (pt.m_x / n) , (int) (pt.m_y / n) ) ;
628 }
629
630 // wxRect2Ds are a axis-aligned rectangles, each side of the rect is parallel to the x- or m_y- axis. The rectangle is either defined by the
631 // top left and bottom right corner, or by the top left corner and size. A point is contained within the rectangle if
632 // left <= x < right and top <= m_y < bottom , thus it is a half open interval.
633
634 class WXDLLEXPORT wxRect2DInt
635 {
636 public:
637 wxRect2DInt() { m_x = m_y = m_width = m_height = 0 ; }
638 wxRect2DInt(wxInt32 x, wxInt32 y, wxInt32 w, wxInt32 h) { m_x = x ; m_y = y ; m_width = w ; m_height = h ; }
639 wxRect2DInt(const wxPoint2DInt& topLeft, const wxPoint2DInt& bottomRight);
640 inline wxRect2DInt(const wxPoint2DInt& pos, const wxSize& size);
641 inline wxRect2DInt(const wxRect2DInt& rect);
642
643 // single attribute accessors
644
645 inline wxPoint2DInt GetPosition() { return wxPoint2DInt(m_x, m_y); }
646 inline wxSize GetSize() { return wxSize(m_width, m_height); }
647
648 // for the edge and corner accessors there are two setters conterparts, the Set.. functions keep the other corners at their
649 // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners apropriately
650
651 inline wxInt32 GetLeft() const { return m_x; }
652 inline void SetLeft( wxInt32 n ) { m_width += m_x - n ; m_x = n ; }
653 inline void MoveLeftTo( wxInt32 n ) { m_x = n ; }
654 inline wxInt32 GetTop() const { return m_y; }
655 inline void SetTop( wxInt32 n ) { m_height += m_y - n ; m_y = n ; }
656 inline void MoveTopTo( wxInt32 n ) { m_y = n ; }
657 inline wxInt32 GetBottom() const { return m_y + m_height; }
658 inline void SetBottom( wxInt32 n ) { m_height += n - (m_y+m_height) ;}
659 inline void MoveBottomTo( wxInt32 n ) { m_y = n - m_height ; }
660 inline wxInt32 GetRight() const { return m_x + m_width; }
661 inline void SetRight( wxInt32 n ) { m_width += n - (m_x+m_width) ; }
662 inline void MoveRightTo( wxInt32 n ) { m_x = n - m_width ; }
663
664 inline wxPoint2DInt GetLeftTop() const { return wxPoint2DInt( m_x , m_y ) ; }
665 inline void SetLeftTop( const wxPoint2DInt &pt ) { m_width += m_x - pt.m_x ; m_height += m_y - pt.m_y ; m_x = pt.m_x ; m_y = pt.m_y ; }
666 inline void MoveLeftTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x ; m_y = pt.m_y ; }
667 inline wxPoint2DInt GetLeftBottom() const { return wxPoint2DInt( m_x , m_y + m_height ) ; }
668 inline void SetLeftBottom( const wxPoint2DInt &pt ) { m_width += m_x - pt.m_x ; m_height += pt.m_y - (m_y+m_height) ; m_x = pt.m_x ; }
669 inline void MoveLeftBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x ; m_y = pt.m_y - m_height; }
670 inline wxPoint2DInt GetRightTop() const { return wxPoint2DInt( m_x+m_width , m_y ) ; }
671 inline void SetRightTop( const wxPoint2DInt &pt ) { m_width += pt.m_x - ( m_x + m_width ) ; m_height += m_y - pt.m_y ; m_y = pt.m_y ; }
672 inline void MoveRightTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y ; }
673 inline wxPoint2DInt GetRightBottom() const { return wxPoint2DInt( m_x+m_width , m_y + m_height ) ; }
674 inline void SetRightBottom( const wxPoint2DInt &pt ) { m_width += pt.m_x - ( m_x + m_width ) ; m_height += pt.m_y - (m_y+m_height) ;}
675 inline void MoveRightBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y - m_height; }
676 inline wxPoint2DInt GetCentre() const { return wxPoint2DInt( m_x+m_width/2 , m_y+m_height/2 ) ; }
677 inline void SetCentre( const wxPoint2DInt &pt ) { MoveCentreTo( pt ) ; } // since this is impossible without moving...
678 inline void MoveCentreTo( const wxPoint2DInt &pt ) { m_x += pt.m_x - (m_x+m_width/2) , m_y += pt.m_y -(m_y+m_height/2) ; }
679 inline wxOutCode GetOutcode( const wxPoint2DInt &pt ) const
680 { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
681 ( ( pt.m_x >= m_x + m_width ) ? wxOutRight : 0 ) +
682 ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
683 ( ( pt.m_y >= m_y + m_height ) ? wxOutBottom : 0 )) ; }
684 inline bool Contains( const wxPoint2DInt &pt ) const
685 { return GetOutcode( pt ) == wxInside ; }
686 inline bool Contains( const wxRect2DInt &rect ) const
687 { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
688 ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ) ; }
689 inline bool IsEmpty() const
690 { return ( m_width <= 0 || m_height <= 0 ) ; }
691 inline bool HaveEqualSize( const wxRect2DInt &rect ) const
692 { return ( rect.m_width == m_width && rect.m_height == m_height ) ; }
693
694 inline void Inset( wxInt32 x , wxInt32 y ) { m_x += x ; m_y += y ; m_width -= 2 * x ; m_height -= 2 * y ; }
695 inline void Inset( wxInt32 left , wxInt32 top ,wxInt32 right , wxInt32 bottom )
696 { m_x += left ; m_y += top ; m_width -= left + right ; m_height -= top + bottom ;}
697 inline void Offset( const wxPoint2DInt &pt ) { m_x += pt.m_x ; m_y += pt.m_y ; }
698 void ConstrainTo( const wxRect2DInt &rect ) ;
699 inline wxPoint2DInt Interpolate( wxInt32 widthfactor , wxInt32 heightfactor ) { return wxPoint2DInt( m_x + m_width * widthfactor , m_y + m_height * heightfactor ) ; }
700
701 static void Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) ;
702 inline void Intersect( const wxRect2DInt &otherRect ) { Intersect( *this , otherRect , this ) ; }
703 inline wxRect2DInt CreateIntersection( const wxRect2DInt &otherRect ) const { wxRect2DInt result ; Intersect( *this , otherRect , &result) ; return result ; }
704 bool Intersects( const wxRect2DInt &rect ) const ;
705
706 static void Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) ;
707 void Union( const wxRect2DInt &otherRect ) { Union( *this , otherRect , this ) ; }
708 void Union( const wxPoint2DInt &pt ) ;
709 inline wxRect2DInt CreateUnion( const wxRect2DInt &otherRect ) const { wxRect2DInt result ; Union( *this , otherRect , &result) ; return result ; }
710
711 inline void Scale( wxInt32 f ) { m_x *= f ; m_y *= f ; m_width *= f ; m_height *= f ;}
712 inline void Scale( wxInt32 num , wxInt32 denum )
713 { m_x *= ((wxInt32)num)/((wxInt32)denum) ; m_y *= ((wxInt32)num)/((wxInt32)denum) ;
714 m_width *= ((wxInt32)num)/((wxInt32)denum) ; m_height *= ((wxInt32)num)/((wxInt32)denum) ;}
715
716 wxRect2DInt& operator = (const wxRect2DInt& rect);
717 bool operator == (const wxRect2DInt& rect);
718 bool operator != (const wxRect2DInt& rect);
719
720 void WriteTo( wxDataOutputStream &stream ) const ;
721 void ReadFrom( wxDataInputStream &stream ) ;
722
723 wxInt32 m_x ;
724 wxInt32 m_y ;
725 wxInt32 m_width;
726 wxInt32 m_height;
727 };
728
729 inline wxRect2DInt::wxRect2DInt( const wxRect2DInt &r )
730 {
731 m_x = r.m_x ;
732 m_y = r.m_y ;
733 m_width = r.m_width ;
734 m_height = r.m_height ;
735 }
736
737 inline wxRect2DInt::wxRect2DInt( const wxPoint2DInt &a , const wxPoint2DInt &b)
738 {
739 m_x = wxMin( a.m_x , b.m_x ) ;
740 m_y = wxMin( a.m_y , b.m_y ) ;
741 m_width = abs( a.m_x - b.m_x ) ;
742 m_height = abs( a.m_y - b.m_y ) ;
743 }
744
745 class wxTransform2D
746 {
747 public :
748 virtual void Transform( wxPoint2DInt* pt )const = 0 ;
749 virtual void Transform( wxRect2DInt* r ) const ;
750 virtual wxPoint2DInt Transform( const wxPoint2DInt &pt ) const ;
751 virtual wxRect2DInt Transform( const wxRect2DInt &r ) const ;
752
753 virtual void InverseTransform( wxPoint2DInt* pt ) const = 0;
754 virtual void InverseTransform( wxRect2DInt* r ) const ;
755 virtual wxPoint2DInt InverseTransform( const wxPoint2DInt &pt ) const ;
756 virtual wxRect2DInt InverseTransform( const wxRect2DInt &r ) const ;
757 } ;
758
759 inline void wxTransform2D::Transform( wxRect2DInt* r ) const
760 { wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; Transform( &a ) ; Transform( &b ) ; *r = wxRect2DInt( a , b ) ; }
761
762 inline wxPoint2DInt wxTransform2D::Transform( const wxPoint2DInt &pt ) const
763 { wxPoint2DInt res = pt ; Transform( &res ) ; return res ; }
764
765 inline wxRect2DInt wxTransform2D::Transform( const wxRect2DInt &r ) const
766 { wxRect2DInt res = r ; Transform( &res ) ; return res ; }
767
768 inline void wxTransform2D::InverseTransform( wxRect2DInt* r ) const
769 { wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; InverseTransform( &a ) ; InverseTransform( &b ) ; *r = wxRect2DInt( a , b ) ; }
770
771 inline wxPoint2DInt wxTransform2D::InverseTransform( const wxPoint2DInt &pt ) const
772 { wxPoint2DInt res = pt ; InverseTransform( &res ) ; return res ; }
773
774 inline wxRect2DInt wxTransform2D::InverseTransform( const wxRect2DInt &r ) const
775 { wxRect2DInt res = r ; InverseTransform( &res ) ; return res ; }
776
777
778 #endif // wxUSE_GEOMETRY
779
780 #endif // _WX_GEOMETRY_H_