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