]> git.saurik.com Git - wxWidgets.git/blob - include/wx/geometry.h
Applied some of the SGI fixes. Don't know about the
[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__
13 #define _WX_GEOMETRY__
14
15 #ifdef __GNUG__
16 #pragma interface "geometry.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/utils.h"
21 #include "wx/gdicmn.h"
22 #include <math.h>
23
24 #ifdef __WXMSW__
25 #define wxMulDivInt32( a , b , c ) ::MulDiv( a , b , c )
26 #elif defined( __WXMAC__ )
27 #include "Math64.h"
28 #define wxMulDivInt32( a , b , c ) S32Set( S64Div( S64Multiply( S64Set(a) , S64Set(b) ) , S64Set(c) ) )
29 #else
30 #define wxMulDivInt32( a , b , c ) ((wxInt32)((a)*(((wxDouble)b)/((wxDouble)c))))
31 #endif
32
33 class wxDataInputStream ;
34 class wxDataOutputStream ;
35
36 // clipping from Cohen-Sutherland
37
38 enum wxOutCode
39 {
40 wxInside = 0x00 ,
41 wxOutLeft = 0x01 ,
42 wxOutRight = 0x02 ,
43 wxOutTop = 0x08 ,
44 wxOutBottom = 0x04
45 } ;
46
47 // wxPoint2Ds represent a point or a vector in a 2d coordinate system
48
49 class WXDLLEXPORT wxPoint2DDouble
50 {
51 public :
52 wxPoint2DDouble();
53 wxPoint2DDouble( wxDouble x , wxDouble y ) ;
54 wxPoint2DDouble( const wxPoint2DDouble &pt ) ;
55
56 // two different conversions to integers, floor and rounding
57 void GetFloor( wxInt32 *x , wxInt32 *y ) ;
58 void GetRounded( wxInt32 *x , wxInt32 *y ) ;
59
60 wxDouble GetVectorLength() ;
61 wxDouble GetVectorAngle() ;
62 void SetVectorLength( wxDouble length ) ;
63 void SetVectorAngle( wxDouble degrees ) ;
64 void SetPolarCoordinates( wxDouble angle , wxDouble length ) ;
65 // set the vector length to 1.0, preserving the angle
66 void Normalize() ;
67
68 wxDouble GetDistance( const wxPoint2DDouble &pt ) ;
69 wxDouble GetDistanceSquare( const wxPoint2DDouble &pt ) ;
70 wxDouble GetDotProduct( const wxPoint2DDouble &vec ) ;
71 wxDouble GetCrossProduct( const wxPoint2DDouble &vec ) ;
72
73 // the reflection of this point
74 wxPoint2DDouble operator-() ;
75
76 wxPoint2DDouble& operator=(const wxPoint2DDouble& pt) ;
77 wxPoint2DDouble& operator+=(const wxPoint2DDouble& pt) ;
78 wxPoint2DDouble& operator-=(const wxPoint2DDouble& pt) ;
79 wxPoint2DDouble& operator*=(const wxPoint2DDouble& pt) ;
80 wxPoint2DDouble& operator*=(wxDouble n) ;
81 wxPoint2DDouble& operator*=(wxInt32 n) ;
82 wxPoint2DDouble& operator/=(const wxPoint2DDouble& pt) ;
83 wxPoint2DDouble& operator/=(wxDouble n) ;
84 wxPoint2DDouble& operator/=(wxInt32 n) ;
85
86 bool operator==(const wxPoint2DDouble& pt) const ;
87 bool operator!=(const wxPoint2DDouble& pt) const ;
88
89 wxDouble m_x ;
90 wxDouble m_y ;
91 } ;
92
93 wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
94 wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
95 wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
96 wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt) ;
97 wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt) ;
98 wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n) ;
99 wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n) ;
100 wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
101 wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n) ;
102 wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n) ;
103
104 inline wxPoint2DDouble::wxPoint2DDouble()
105 {
106 m_x = 0.0 ;
107 m_y = 0.0 ;
108 }
109
110 inline wxPoint2DDouble::wxPoint2DDouble( wxDouble x , wxDouble y )
111 {
112 m_x = x ;
113 m_y = y ;
114 }
115
116 inline wxPoint2DDouble::wxPoint2DDouble( const wxPoint2DDouble &pt )
117 {
118 m_x = pt.m_x ;
119 m_y = pt.m_y ;
120 }
121
122 inline void wxPoint2DDouble::GetFloor( wxInt32 *x , wxInt32 *y )
123 {
124 *x = (wxInt32) floor( m_x ) ;
125 *y = (wxInt32) floor( m_y ) ;
126 }
127
128 inline void wxPoint2DDouble::GetRounded( wxInt32 *x , wxInt32 *y )
129 {
130 *x = (wxInt32) floor( m_x + 0.5 ) ;
131 *y = (wxInt32) floor( m_y + 0.5) ;
132 }
133
134 inline wxDouble wxPoint2DDouble::GetVectorLength() ;
135 inline void wxPoint2DDouble::SetVectorLength( wxDouble length ) ;
136 inline wxDouble wxPoint2DDouble::GetVectorAngle() ;
137 inline void wxPoint2DDouble::SetVectorAngle( wxDouble degrees ) ;
138 inline void wxPoint2DDouble::SetPolarCoordinates( wxDouble angle , wxDouble length ) ;
139 inline void wxPoint2DDouble::Normalize() ;
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() { m_x = m_y = m_width = m_height = 0 ; }
270 wxRect2DDouble(wxDouble x, wxDouble y, wxDouble w, wxDouble h) { m_x = x ; m_y = y ; m_width = w ; m_height = h ; }
271 wxRect2DDouble(const wxPoint2DDouble& topLeft, const wxPoint2DDouble& bottomRight);
272 wxRect2DDouble(const wxPoint2DDouble& pos, const wxSize& size);
273 wxRect2DDouble(const wxRect2DDouble& rect);
274
275 // single attribute accessors
276
277 inline wxPoint2DDouble GetPosition() { return wxPoint2DDouble(m_x, m_y); }
278 inline wxSize GetSize() { return wxSize(m_width, m_height); }
279
280 // for the edge and corner accessors there are two setters conterparts, the Set.. functions keep the other corners at their
281 // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners apropriately
282
283 inline wxDouble GetLeft() const { return m_x; }
284 inline void SetLeft( wxDouble n ) { m_width += m_x - n ; m_x = n ; }
285 inline void MoveLeftTo( wxDouble n ) { m_x = n ; }
286 inline wxDouble GetTop() const { return m_y; }
287 inline void SetTop( wxDouble n ) { m_height += m_y - n ; m_y = n ; }
288 inline void MoveTopTo( wxDouble n ) { m_y = n ; }
289 inline wxDouble GetBottom() const { return m_y + m_height; }
290 inline void SetBottom( wxDouble n ) { m_height += n - (m_y+m_height) ;}
291 inline void MoveBottomTo( wxDouble n ) { m_y = n - m_height ; }
292 inline wxDouble GetRight() const { return m_x + m_width; }
293 inline void SetRight( wxDouble n ) { m_width += n - (m_x+m_width) ; }
294 inline void MoveRightTo( wxDouble n ) { m_x = n - m_width ; }
295
296 inline wxPoint2DDouble GetLeftTop() const { return wxPoint2DDouble( m_x , m_y ) ; }
297 inline void SetLeftTop( const wxPoint2DDouble &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 ; }
298 inline void MoveLeftTopTo( const wxPoint2DDouble &pt ) { m_x = pt.m_x ; m_y = pt.m_y ; }
299 inline wxPoint2DDouble GetLeftBottom() const { return wxPoint2DDouble( m_x , m_y + m_height ) ; }
300 inline void SetLeftBottom( const wxPoint2DDouble &pt ) { m_width += m_x - pt.m_x ; m_height += pt.m_y - (m_y+m_height) ; m_x = pt.m_x ; }
301 inline void MoveLeftBottomTo( const wxPoint2DDouble &pt ) { m_x = pt.m_x ; m_y = pt.m_y - m_height; }
302 inline wxPoint2DDouble GetRightTop() const { return wxPoint2DDouble( m_x+m_width , m_y ) ; }
303 inline void SetRightTop( const wxPoint2DDouble &pt ) { m_width += pt.m_x - ( m_x + m_width ) ; m_height += m_y - pt.m_y ; m_y = pt.m_y ; }
304 inline void MoveRightTopTo( const wxPoint2DDouble &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y ; }
305 inline wxPoint2DDouble GetRightBottom() const { return wxPoint2DDouble( m_x+m_width , m_y + m_height ) ; }
306 inline void SetRightBottom( const wxPoint2DDouble &pt ) { m_width += pt.m_x - ( m_x + m_width ) ; m_height += pt.m_y - (m_y+m_height) ;}
307 inline void MoveRightBottomTo( const wxPoint2DDouble &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y - m_height; }
308 inline wxPoint2DDouble GetCentre() const { return wxPoint2DDouble( m_x+m_width/2 , m_y+m_height/2 ) ; }
309 inline void SetCentre( const wxPoint2DDouble &pt ) { MoveCentreTo( pt ) ; } // since this is impossible without moving...
310 inline void MoveCentreTo( const wxPoint2DDouble &pt ) { m_x += pt.m_x - (m_x+m_width/2) , m_y += pt.m_y -(m_y+m_height/2) ; }
311 inline wxOutCode GetOutcode( const wxPoint2DDouble &pt ) const
312 { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
313 ( ( pt.m_x >= m_x + m_width ) ? wxOutRight : 0 ) +
314 ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
315 ( ( pt.m_y >= m_y + m_height ) ? wxOutBottom : 0 )) ; }
316 inline bool Contains( const wxPoint2DDouble &pt ) const
317 { return GetOutcode( pt ) == wxInside ; }
318 inline bool Contains( const wxRect2DDouble &rect ) const
319 { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
320 ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ) ; }
321 inline bool IsEmpty() const
322 { return ( m_width <= 0 || m_height <= 0 ) ; }
323 inline bool HaveEqualSize( const wxRect2DDouble &rect ) const
324 { return ( rect.m_width == m_width && rect.m_height == m_height ) ; }
325
326 inline void Inset( wxDouble x , wxDouble y ) { m_x += x ; m_y += y ; m_width -= 2 * x ; m_height -= 2 * y ; }
327 inline void Inset( wxDouble left , wxDouble top ,wxDouble right , wxDouble bottom )
328 { m_x += left ; m_y += top ; m_width -= left + right ; m_height -= top + bottom ;}
329 inline void Offset( const wxPoint2DDouble &pt ) { m_x += pt.m_x ; m_y += pt.m_y ; }
330 void ConstrainTo( const wxRect2DDouble &rect ) ;
331 inline wxPoint2DDouble Interpolate( wxInt32 widthfactor , wxInt32 heightfactor ) { return wxPoint2DDouble( m_x + m_width * widthfactor , m_y + m_height * heightfactor ) ; }
332
333 static void Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) ;
334 inline void Intersect( const wxRect2DDouble &otherRect ) { Intersect( *this , otherRect , this ) ; }
335 inline wxRect2DDouble CreateIntersection( const wxRect2DDouble &otherRect ) const { wxRect2DDouble result ; Intersect( *this , otherRect , &result) ; return result ; }
336 bool Intersects( const wxRect2DDouble &rect ) const ;
337
338 static void Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) ;
339 void Union( const wxRect2DDouble &otherRect ) { Union( *this , otherRect , this ) ; }
340 void Union( const wxPoint2DDouble &pt ) ;
341 inline wxRect2DDouble CreateUnion( const wxRect2DDouble &otherRect ) const { wxRect2DDouble result ; Union( *this , otherRect , &result) ; return result ; }
342
343 inline void Scale( wxDouble f ) { m_x *= f ; m_y *= f ; m_width *= f ; m_height *= f ;}
344 inline void Scale( wxInt32 num , wxInt32 denum )
345 { m_x *= ((wxDouble)num)/((wxDouble)denum) ; m_y *= ((wxDouble)num)/((wxDouble)denum) ;
346 m_width *= ((wxDouble)num)/((wxDouble)denum) ; m_height *= ((wxDouble)num)/((wxDouble)denum) ;}
347
348 wxRect2DDouble& operator = (const wxRect2DDouble& rect);
349 bool operator == (const wxRect2DDouble& rect);
350 bool operator != (const wxRect2DDouble& rect);
351
352 wxDouble m_x ;
353 wxDouble m_y ;
354 wxDouble m_width;
355 wxDouble m_height;
356 };
357
358 class WXDLLEXPORT wxPoint2DInt
359 {
360 public :
361 wxPoint2DInt();
362 wxPoint2DInt( wxInt32 x , wxInt32 y ) ;
363 wxPoint2DInt( const wxPoint2DInt &pt ) ;
364 wxPoint2DInt( const wxPoint &pt ) ;
365
366 // two different conversions to integers, floor and rounding
367 void GetFloor( wxInt32 *x , wxInt32 *y ) ;
368 void GetRounded( wxInt32 *x , wxInt32 *y ) ;
369
370 wxDouble GetVectorLength() ;
371 wxDouble GetVectorAngle() ;
372 void SetVectorLength( wxDouble length ) ;
373 void SetVectorAngle( wxDouble degrees ) ;
374 void SetPolarCoordinates( wxInt32 angle , wxInt32 length ) ;
375 // set the vector length to 1.0, preserving the angle
376 void Normalize() ;
377
378 wxDouble GetDistance( const wxPoint2DInt &pt ) const ;
379 wxDouble GetDistanceSquare( const wxPoint2DInt &pt ) const;
380 wxInt32 GetDotProduct( const wxPoint2DInt &vec ) const;
381 wxInt32 GetCrossProduct( const wxPoint2DInt &vec ) const;
382
383 // the reflection of this point
384 wxPoint2DInt operator-() ;
385
386 wxPoint2DInt& operator=(const wxPoint2DInt& pt) ;
387 wxPoint2DInt& operator+=(const wxPoint2DInt& pt) ;
388 wxPoint2DInt& operator-=(const wxPoint2DInt& pt) ;
389 wxPoint2DInt& operator*=(const wxPoint2DInt& pt) ;
390 wxPoint2DInt& operator*=(wxDouble n) ;
391 wxPoint2DInt& operator*=(wxInt32 n) ;
392 wxPoint2DInt& operator/=(const wxPoint2DInt& pt) ;
393 wxPoint2DInt& operator/=(wxDouble n) ;
394 wxPoint2DInt& operator/=(wxInt32 n) ;
395 operator wxPoint() const ;
396 bool operator==(const wxPoint2DInt& pt) const ;
397 bool operator!=(const wxPoint2DInt& pt) const ;
398
399 void WriteTo( wxDataOutputStream &stream ) const ;
400 void ReadFrom( wxDataInputStream &stream ) ;
401
402 wxInt32 m_x ;
403 wxInt32 m_y ;
404 } ;
405
406 wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
407 wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
408 wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
409 wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
410 wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
411 wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
412 wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
413 wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
414 wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
415 wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
416
417 inline wxPoint2DInt::wxPoint2DInt()
418 {
419 m_x = 0 ;
420 m_y = 0 ;
421 }
422
423 inline wxPoint2DInt::wxPoint2DInt( wxInt32 x , wxInt32 y )
424 {
425 m_x = x ;
426 m_y = y ;
427 }
428
429 inline wxPoint2DInt::wxPoint2DInt( const wxPoint2DInt &pt )
430 {
431 m_x = pt.m_x ;
432 m_y = pt.m_y ;
433 }
434
435 inline wxPoint2DInt::wxPoint2DInt( const wxPoint &pt )
436 {
437 m_x = pt.x ;
438 m_y = pt.y ;
439 }
440
441 inline void wxPoint2DInt::GetFloor( wxInt32 *x , wxInt32 *y )
442 {
443 *x = (wxInt32) floor( m_x ) ;
444 *y = (wxInt32) floor( m_y ) ;
445 }
446
447 inline void wxPoint2DInt::GetRounded( wxInt32 *x , wxInt32 *y )
448 {
449 *x = (wxInt32) floor( m_x + 0.5 ) ;
450 *y = (wxInt32) floor( m_y + 0.5) ;
451 }
452
453 inline wxDouble wxPoint2DInt::GetVectorLength()
454 {
455 return sqrt( (m_x)*(m_x) + (m_y)*(m_y) ) ;
456 }
457
458 inline void wxPoint2DInt::SetVectorLength( wxDouble length )
459 {
460 wxDouble before = GetVectorLength() ;
461 m_x = (wxInt32)(m_x * length / before) ;
462 m_y = (wxInt32)(m_y * length / before) ;
463 }
464
465 inline void wxPoint2DInt::SetPolarCoordinates( wxInt32 angle , wxInt32 length ) ;
466 inline void wxPoint2DInt::Normalize()
467 {
468 SetVectorLength( 1 ) ;
469 }
470
471 inline wxDouble wxPoint2DInt::GetDistance( const wxPoint2DInt &pt ) const
472 {
473 return sqrt( GetDistanceSquare( pt ) );
474 }
475
476 inline wxDouble wxPoint2DInt::GetDistanceSquare( const wxPoint2DInt &pt ) const
477 {
478 return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) ) ;
479 }
480
481 inline wxInt32 wxPoint2DInt::GetDotProduct( const wxPoint2DInt &vec ) const
482 {
483 return ( m_x * vec.m_x + m_y * vec.m_y ) ;
484 }
485
486 inline wxInt32 wxPoint2DInt::GetCrossProduct( const wxPoint2DInt &vec ) const
487 {
488 return ( m_x * vec.m_y - vec.m_x * m_y ) ;
489 }
490
491 inline wxPoint2DInt::operator wxPoint() const
492 {
493 return wxPoint( m_x, m_y);
494 }
495
496 inline wxPoint2DInt wxPoint2DInt::operator-()
497 {
498 return wxPoint2DInt( -m_x, -m_y);
499 }
500
501 inline wxPoint2DInt& wxPoint2DInt::operator=(const wxPoint2DInt& pt)
502 {
503 m_x = pt.m_x ;
504 m_y = pt.m_y;
505 return *this ;
506 }
507
508 inline wxPoint2DInt& wxPoint2DInt::operator+=(const wxPoint2DInt& pt)
509 {
510 m_x = m_x + pt.m_x ;
511 m_y = m_y + pt.m_y;
512 return *this ;
513 }
514
515 inline wxPoint2DInt& wxPoint2DInt::operator-=(const wxPoint2DInt& pt)
516 {
517 m_x = m_x - pt.m_x ;
518 m_y = m_y - pt.m_y;
519 return *this ;
520 }
521
522 inline wxPoint2DInt& wxPoint2DInt::operator*=(const wxPoint2DInt& pt)
523 {
524 m_x = m_x + pt.m_x ;
525 m_y = m_y + pt.m_y;
526 return *this ;
527 }
528
529 inline wxPoint2DInt& wxPoint2DInt::operator/=(const wxPoint2DInt& pt)
530 {
531 m_x = m_x - pt.m_x ;
532 m_y = m_y - pt.m_y;
533 return *this ;
534 }
535
536 inline bool wxPoint2DInt::operator==(const wxPoint2DInt& pt) const
537 {
538 return m_x == pt.m_x && m_y == pt.m_y;
539 }
540
541 inline bool wxPoint2DInt::operator!=(const wxPoint2DInt& pt) const
542 {
543 return m_x != pt.m_x || m_y != pt.m_y;
544 }
545
546 inline wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
547 {
548 return wxPoint2DInt( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y ) ;
549 }
550
551 inline wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
552 {
553 return wxPoint2DInt( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y ) ;
554 }
555
556
557 inline wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
558 {
559 return wxPoint2DInt( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y ) ;
560 }
561
562 inline wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt)
563 {
564 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
565 }
566
567 inline wxPoint2DInt operator*(wxDouble n , const wxPoint2DInt& pt)
568 {
569 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
570 }
571
572 inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n)
573 {
574 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
575 }
576
577 inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxDouble n)
578 {
579 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
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 inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n)
588 {
589 return wxPoint2DInt( pt.m_x / n , pt.m_y / n ) ;
590 }
591
592 inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxDouble n)
593 {
594 return wxPoint2DInt( pt.m_x / n , pt.m_y / n ) ;
595 }
596
597 // 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
598 // top left and bottom right corner, or by the top left corner and size. A point is contained within the rectangle if
599 // left <= x < right and top <= m_y < bottom , thus it is a half open interval.
600
601 class WXDLLEXPORT wxRect2DInt
602 {
603 public:
604 wxRect2DInt() { m_x = m_y = m_width = m_height = 0 ; }
605 wxRect2DInt(wxInt32 x, wxInt32 y, wxInt32 w, wxInt32 h) { m_x = x ; m_y = y ; m_width = w ; m_height = h ; }
606 wxRect2DInt(const wxPoint2DInt& topLeft, const wxPoint2DInt& bottomRight);
607 wxRect2DInt(const wxPoint2DInt& pos, const wxSize& size);
608 wxRect2DInt(const wxRect2DInt& rect);
609
610 // single attribute accessors
611
612 inline wxPoint2DInt GetPosition() { return wxPoint2DInt(m_x, m_y); }
613 inline wxSize GetSize() { return wxSize(m_width, m_height); }
614
615 // for the edge and corner accessors there are two setters conterparts, the Set.. functions keep the other corners at their
616 // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners apropriately
617
618 inline wxInt32 GetLeft() const { return m_x; }
619 inline void SetLeft( wxInt32 n ) { m_width += m_x - n ; m_x = n ; }
620 inline void MoveLeftTo( wxInt32 n ) { m_x = n ; }
621 inline wxInt32 GetTop() const { return m_y; }
622 inline void SetTop( wxInt32 n ) { m_height += m_y - n ; m_y = n ; }
623 inline void MoveTopTo( wxInt32 n ) { m_y = n ; }
624 inline wxInt32 GetBottom() const { return m_y + m_height; }
625 inline void SetBottom( wxInt32 n ) { m_height += n - (m_y+m_height) ;}
626 inline void MoveBottomTo( wxInt32 n ) { m_y = n - m_height ; }
627 inline wxInt32 GetRight() const { return m_x + m_width; }
628 inline void SetRight( wxInt32 n ) { m_width += n - (m_x+m_width) ; }
629 inline void MoveRightTo( wxInt32 n ) { m_x = n - m_width ; }
630
631 inline wxPoint2DInt GetLeftTop() const { return wxPoint2DInt( m_x , m_y ) ; }
632 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 ; }
633 inline void MoveLeftTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x ; m_y = pt.m_y ; }
634 inline wxPoint2DInt GetLeftBottom() const { return wxPoint2DInt( m_x , m_y + m_height ) ; }
635 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 ; }
636 inline void MoveLeftBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x ; m_y = pt.m_y - m_height; }
637 inline wxPoint2DInt GetRightTop() const { return wxPoint2DInt( m_x+m_width , m_y ) ; }
638 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 ; }
639 inline void MoveRightTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y ; }
640 inline wxPoint2DInt GetRightBottom() const { return wxPoint2DInt( m_x+m_width , m_y + m_height ) ; }
641 inline void SetRightBottom( const wxPoint2DInt &pt ) { m_width += pt.m_x - ( m_x + m_width ) ; m_height += pt.m_y - (m_y+m_height) ;}
642 inline void MoveRightBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width ; m_y = pt.m_y - m_height; }
643 inline wxPoint2DInt GetCentre() const { return wxPoint2DInt( m_x+m_width/2 , m_y+m_height/2 ) ; }
644 inline void SetCentre( const wxPoint2DInt &pt ) { MoveCentreTo( pt ) ; } // since this is impossible without moving...
645 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) ; }
646 inline wxOutCode GetOutcode( const wxPoint2DInt &pt ) const
647 { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
648 ( ( pt.m_x >= m_x + m_width ) ? wxOutRight : 0 ) +
649 ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
650 ( ( pt.m_y >= m_y + m_height ) ? wxOutBottom : 0 )) ; }
651 inline bool Contains( const wxPoint2DInt &pt ) const
652 { return GetOutcode( pt ) == wxInside ; }
653 inline bool Contains( const wxRect2DInt &rect ) const
654 { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
655 ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ) ; }
656 inline bool IsEmpty() const
657 { return ( m_width <= 0 || m_height <= 0 ) ; }
658 inline bool HaveEqualSize( const wxRect2DInt &rect ) const
659 { return ( rect.m_width == m_width && rect.m_height == m_height ) ; }
660
661 inline void Inset( wxInt32 x , wxInt32 y ) { m_x += x ; m_y += y ; m_width -= 2 * x ; m_height -= 2 * y ; }
662 inline void Inset( wxInt32 left , wxInt32 top ,wxInt32 right , wxInt32 bottom )
663 { m_x += left ; m_y += top ; m_width -= left + right ; m_height -= top + bottom ;}
664 inline void Offset( const wxPoint2DInt &pt ) { m_x += pt.m_x ; m_y += pt.m_y ; }
665 void ConstrainTo( const wxRect2DInt &rect ) ;
666 inline wxPoint2DInt Interpolate( wxInt32 widthfactor , wxInt32 heightfactor ) { return wxPoint2DInt( m_x + m_width * widthfactor , m_y + m_height * heightfactor ) ; }
667
668 static void Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) ;
669 inline void Intersect( const wxRect2DInt &otherRect ) { Intersect( *this , otherRect , this ) ; }
670 inline wxRect2DInt CreateIntersection( const wxRect2DInt &otherRect ) const { wxRect2DInt result ; Intersect( *this , otherRect , &result) ; return result ; }
671 bool Intersects( const wxRect2DInt &rect ) const ;
672
673 static void Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) ;
674 void Union( const wxRect2DInt &otherRect ) { Union( *this , otherRect , this ) ; }
675 void Union( const wxPoint2DInt &pt ) ;
676 inline wxRect2DInt CreateUnion( const wxRect2DInt &otherRect ) const { wxRect2DInt result ; Union( *this , otherRect , &result) ; return result ; }
677
678 inline void Scale( wxInt32 f ) { m_x *= f ; m_y *= f ; m_width *= f ; m_height *= f ;}
679 inline void Scale( wxInt32 num , wxInt32 denum )
680 { m_x *= ((wxInt32)num)/((wxInt32)denum) ; m_y *= ((wxInt32)num)/((wxInt32)denum) ;
681 m_width *= ((wxInt32)num)/((wxInt32)denum) ; m_height *= ((wxInt32)num)/((wxInt32)denum) ;}
682
683 wxRect2DInt& operator = (const wxRect2DInt& rect);
684 bool operator == (const wxRect2DInt& rect);
685 bool operator != (const wxRect2DInt& rect);
686
687 void WriteTo( wxDataOutputStream &stream ) const ;
688 void ReadFrom( wxDataInputStream &stream ) ;
689
690 wxInt32 m_x ;
691 wxInt32 m_y ;
692 wxInt32 m_width;
693 wxInt32 m_height;
694 };
695
696 inline wxRect2DInt::wxRect2DInt( const wxRect2DInt &r )
697 {
698 m_x = r.m_x ;
699 m_y = r.m_y ;
700 m_width = r.m_width ;
701 m_height = r.m_height ;
702 }
703
704 inline wxRect2DInt::wxRect2DInt( const wxPoint2DInt &a , const wxPoint2DInt &b)
705 {
706 m_x = wxMin( a.m_x , b.m_x ) ;
707 m_y = wxMin( a.m_y , b.m_y ) ;
708 m_width = abs( a.m_x - b.m_x ) ;
709 m_height = abs( a.m_y - b.m_y ) ;
710 }
711
712 class wxTransform2D
713 {
714 public :
715 virtual void Transform( wxPoint2DInt* pt )const = 0 ;
716 virtual void Transform( wxRect2DInt* r ) const ;
717 virtual wxPoint2DInt Transform( const wxPoint2DInt &pt ) const ;
718 virtual wxRect2DInt Transform( const wxRect2DInt &r ) const ;
719
720 virtual void InverseTransform( wxPoint2DInt* pt ) const = 0;
721 virtual void InverseTransform( wxRect2DInt* r ) const ;
722 virtual wxPoint2DInt InverseTransform( const wxPoint2DInt &pt ) const ;
723 virtual wxRect2DInt InverseTransform( const wxRect2DInt &r ) const ;
724 } ;
725
726 inline void wxTransform2D::Transform( wxRect2DInt* r ) const
727 { wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; Transform( &a ) ; Transform( &b ) ; *r = wxRect2DInt( a , b ) ; }
728
729 inline wxPoint2DInt wxTransform2D::Transform( const wxPoint2DInt &pt ) const
730 { wxPoint2DInt res = pt ; Transform( &res ) ; return res ; }
731
732 inline wxRect2DInt wxTransform2D::Transform( const wxRect2DInt &r ) const
733 { wxRect2DInt res = r ; Transform( &res ) ; return res ; }
734
735 inline void wxTransform2D::InverseTransform( wxRect2DInt* r ) const
736 { wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; InverseTransform( &a ) ; InverseTransform( &b ) ; *r = wxRect2DInt( a , b ) ; }
737
738 inline wxPoint2DInt wxTransform2D::InverseTransform( const wxPoint2DInt &pt ) const
739 { wxPoint2DInt res = pt ; InverseTransform( &res ) ; return res ; }
740
741 inline wxRect2DInt wxTransform2D::InverseTransform( const wxRect2DInt &r ) const
742 { wxRect2DInt res = r ; InverseTransform( &res ) ; return res ; }
743
744 #endif