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