]> git.saurik.com Git - wxWidgets.git/blame - include/wx/geometry.h
Made geometry class link.
[wxWidgets.git] / include / wx / geometry.h
CommitLineData
72e7876b
SC
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)
c3a4297c 9// Licence: wxWindows licence
72e7876b
SC
10/////////////////////////////////////////////////////////////////////////////
11
c3a4297c
VZ
12#ifndef _WX_GEOMETRY_H_
13#define _WX_GEOMETRY_H_
72e7876b
SC
14
15#ifdef __GNUG__
16#pragma interface "geometry.h"
17#endif
18
510fc784 19#include "wx/defs.h"
c3a4297c 20
c3a4297c
VZ
21#if wxUSE_GEOMETRY
22
510fc784
RR
23#include "wx/utils.h"
24#include "wx/gdicmn.h"
25#include <math.h>
26
72e7876b 27#ifdef __WXMSW__
c3a4297c 28 #define wxMulDivInt32( a , b , c ) ::MulDiv( a , b , c )
72e7876b 29#elif defined( __WXMAC__ )
c3a4297c
VZ
30 #include "Math64.h"
31 #define wxMulDivInt32( a , b , c ) S32Set( S64Div( S64Multiply( S64Set(a) , S64Set(b) ) , S64Set(c) ) )
72e7876b 32#else
c3a4297c 33 #define wxMulDivInt32( a , b , c ) ((wxInt32)((a)*(((wxDouble)b)/((wxDouble)c))))
72e7876b
SC
34#endif
35
36class wxDataInputStream ;
37class wxDataOutputStream ;
38
39// clipping from Cohen-Sutherland
40
41enum wxOutCode
42{
c3a4297c
VZ
43 wxInside = 0x00 ,
44 wxOutLeft = 0x01 ,
45 wxOutRight = 0x02 ,
46 wxOutTop = 0x08 ,
47 wxOutBottom = 0x04
72e7876b
SC
48} ;
49
50// wxPoint2Ds represent a point or a vector in a 2d coordinate system
51
52class WXDLLEXPORT wxPoint2DDouble
53{
54public :
c3a4297c
VZ
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 ;
72e7876b
SC
94} ;
95
96wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
97wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
98wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
99wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt) ;
100wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt) ;
101wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n) ;
102wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n) ;
103wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2) ;
104wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n) ;
105wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n) ;
106
107inline wxPoint2DDouble::wxPoint2DDouble()
108{
c3a4297c
VZ
109 m_x = 0.0 ;
110 m_y = 0.0 ;
72e7876b
SC
111}
112
113inline wxPoint2DDouble::wxPoint2DDouble( wxDouble x , wxDouble y )
114{
c3a4297c
VZ
115 m_x = x ;
116 m_y = y ;
72e7876b
SC
117}
118
119inline wxPoint2DDouble::wxPoint2DDouble( const wxPoint2DDouble &pt )
120{
c3a4297c
VZ
121 m_x = pt.m_x ;
122 m_y = pt.m_y ;
72e7876b 123}
c3a4297c 124
72e7876b
SC
125inline void wxPoint2DDouble::GetFloor( wxInt32 *x , wxInt32 *y )
126{
c3a4297c
VZ
127 *x = (wxInt32) floor( m_x ) ;
128 *y = (wxInt32) floor( m_y ) ;
72e7876b
SC
129}
130
131inline void wxPoint2DDouble::GetRounded( wxInt32 *x , wxInt32 *y )
132{
c3a4297c
VZ
133 *x = (wxInt32) floor( m_x + 0.5 ) ;
134 *y = (wxInt32) floor( m_y + 0.5) ;
72e7876b
SC
135}
136
72e7876b
SC
137inline wxDouble wxPoint2DDouble::GetDistance( const wxPoint2DDouble &pt )
138{
c3a4297c 139 return sqrt( GetDistanceSquare( pt ) );
72e7876b
SC
140}
141
142inline wxDouble wxPoint2DDouble::GetDistanceSquare( const wxPoint2DDouble &pt )
143{
c3a4297c 144 return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) ) ;
72e7876b
SC
145}
146
147inline wxDouble wxPoint2DDouble::GetDotProduct( const wxPoint2DDouble &vec )
148{
c3a4297c 149 return ( m_x * vec.m_x + m_y * vec.m_y ) ;
72e7876b
SC
150}
151
152inline wxDouble wxPoint2DDouble::GetCrossProduct( const wxPoint2DDouble &vec )
153{
c3a4297c 154 return ( m_x * vec.m_y - vec.m_x * m_y ) ;
72e7876b
SC
155}
156
157inline wxPoint2DDouble wxPoint2DDouble::operator-()
158{
c3a4297c 159 return wxPoint2DDouble( -m_x, -m_y);
72e7876b
SC
160}
161
162inline wxPoint2DDouble& wxPoint2DDouble::operator=(const wxPoint2DDouble& pt)
163{
c3a4297c
VZ
164 m_x = pt.m_x ;
165 m_y = pt.m_y;
166 return *this ;
72e7876b
SC
167}
168
169inline wxPoint2DDouble& wxPoint2DDouble::operator+=(const wxPoint2DDouble& pt)
170{
c3a4297c
VZ
171 m_x = m_x + pt.m_x ;
172 m_y = m_y + pt.m_y;
173 return *this ;
72e7876b
SC
174}
175
176inline wxPoint2DDouble& wxPoint2DDouble::operator-=(const wxPoint2DDouble& pt)
177{
c3a4297c
VZ
178 m_x = m_x - pt.m_x ;
179 m_y = m_y - pt.m_y;
180 return *this ;
72e7876b
SC
181}
182
183inline wxPoint2DDouble& wxPoint2DDouble::operator*=(const wxPoint2DDouble& pt)
184{
c3a4297c
VZ
185 m_x = m_x + pt.m_x ;
186 m_y = m_y + pt.m_y;
187 return *this ;
72e7876b
SC
188}
189
190inline wxPoint2DDouble& wxPoint2DDouble::operator/=(const wxPoint2DDouble& pt)
191{
c3a4297c
VZ
192 m_x = m_x - pt.m_x ;
193 m_y = m_y - pt.m_y;
194 return *this ;
72e7876b
SC
195}
196
197inline bool wxPoint2DDouble::operator==(const wxPoint2DDouble& pt) const
198{
c3a4297c 199 return m_x == pt.m_x && m_y == pt.m_y;
72e7876b
SC
200}
201
202inline bool wxPoint2DDouble::operator!=(const wxPoint2DDouble& pt) const
203{
c3a4297c 204 return m_x != pt.m_x || m_y != pt.m_y;
72e7876b
SC
205}
206
207inline wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
208{
c3a4297c 209 return wxPoint2DDouble( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y ) ;
72e7876b
SC
210}
211
212inline wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
213{
c3a4297c 214 return wxPoint2DDouble( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y ) ;
72e7876b
SC
215}
216
217
218inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
219{
c3a4297c 220 return wxPoint2DDouble( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y ) ;
72e7876b
SC
221}
222
223inline wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt)
224{
c3a4297c 225 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
226}
227
228inline wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt)
229{
c3a4297c 230 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
231}
232
233inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n)
234{
c3a4297c 235 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
236}
237
238inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n)
239{
c3a4297c 240 return wxPoint2DDouble( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
241}
242
243inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
244{
c3a4297c 245 return wxPoint2DDouble( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y ) ;
72e7876b
SC
246}
247
248inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n)
249{
c3a4297c 250 return wxPoint2DDouble( pt.m_x / n , pt.m_y / n ) ;
72e7876b
SC
251}
252
253inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n)
254{
c3a4297c 255 return wxPoint2DDouble( pt.m_x / n , pt.m_y / n ) ;
72e7876b
SC
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
262class WXDLLEXPORT wxRect2DDouble
263{
264public:
c3a4297c
VZ
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;
72e7876b
SC
352};
353
354class WXDLLEXPORT wxPoint2DInt
355{
356public :
c3a4297c
VZ
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 ;
72e7876b
SC
400} ;
401
402wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
403wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
404wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
405wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
406wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt) ;
407wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
408wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n) ;
409wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2) ;
410wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
411wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n) ;
412
413inline wxPoint2DInt::wxPoint2DInt()
414{
c3a4297c
VZ
415 m_x = 0 ;
416 m_y = 0 ;
72e7876b
SC
417}
418
419inline wxPoint2DInt::wxPoint2DInt( wxInt32 x , wxInt32 y )
420{
c3a4297c
VZ
421 m_x = x ;
422 m_y = y ;
72e7876b
SC
423}
424
425inline wxPoint2DInt::wxPoint2DInt( const wxPoint2DInt &pt )
426{
c3a4297c
VZ
427 m_x = pt.m_x ;
428 m_y = pt.m_y ;
72e7876b 429}
c3a4297c 430
72e7876b
SC
431inline wxPoint2DInt::wxPoint2DInt( const wxPoint &pt )
432{
c3a4297c
VZ
433 m_x = pt.x ;
434 m_y = pt.y ;
72e7876b 435}
c3a4297c 436
72e7876b
SC
437inline void wxPoint2DInt::GetFloor( wxInt32 *x , wxInt32 *y )
438{
c3a4297c
VZ
439 *x = (wxInt32) floor( m_x ) ;
440 *y = (wxInt32) floor( m_y ) ;
72e7876b
SC
441}
442
443inline void wxPoint2DInt::GetRounded( wxInt32 *x , wxInt32 *y )
444{
c3a4297c
VZ
445 *x = (wxInt32) floor( m_x + 0.5 ) ;
446 *y = (wxInt32) floor( m_y + 0.5) ;
72e7876b
SC
447}
448
449inline wxDouble wxPoint2DInt::GetVectorLength()
450{
c3a4297c 451 return sqrt( (m_x)*(m_x) + (m_y)*(m_y) ) ;
72e7876b
SC
452}
453
454inline void wxPoint2DInt::SetVectorLength( wxDouble length )
455{
c3a4297c
VZ
456 wxDouble before = GetVectorLength() ;
457 m_x = (wxInt32)(m_x * length / before) ;
458 m_y = (wxInt32)(m_y * length / before) ;
72e7876b
SC
459}
460
72e7876b
SC
461inline void wxPoint2DInt::Normalize()
462{
c3a4297c 463 SetVectorLength( 1 ) ;
72e7876b
SC
464}
465
466inline wxDouble wxPoint2DInt::GetDistance( const wxPoint2DInt &pt ) const
467{
c3a4297c 468 return sqrt( GetDistanceSquare( pt ) );
72e7876b
SC
469}
470
471inline wxDouble wxPoint2DInt::GetDistanceSquare( const wxPoint2DInt &pt ) const
472{
c3a4297c 473 return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) ) ;
72e7876b
SC
474}
475
476inline wxInt32 wxPoint2DInt::GetDotProduct( const wxPoint2DInt &vec ) const
477{
c3a4297c 478 return ( m_x * vec.m_x + m_y * vec.m_y ) ;
72e7876b
SC
479}
480
481inline wxInt32 wxPoint2DInt::GetCrossProduct( const wxPoint2DInt &vec ) const
482{
c3a4297c 483 return ( m_x * vec.m_y - vec.m_x * m_y ) ;
72e7876b
SC
484}
485
486inline wxPoint2DInt::operator wxPoint() const
487{
c3a4297c 488 return wxPoint( m_x, m_y);
72e7876b
SC
489}
490
491inline wxPoint2DInt wxPoint2DInt::operator-()
492{
c3a4297c 493 return wxPoint2DInt( -m_x, -m_y);
72e7876b
SC
494}
495
496inline wxPoint2DInt& wxPoint2DInt::operator=(const wxPoint2DInt& pt)
497{
c3a4297c
VZ
498 m_x = pt.m_x ;
499 m_y = pt.m_y;
500 return *this ;
72e7876b
SC
501}
502
503inline wxPoint2DInt& wxPoint2DInt::operator+=(const wxPoint2DInt& pt)
504{
c3a4297c
VZ
505 m_x = m_x + pt.m_x ;
506 m_y = m_y + pt.m_y;
507 return *this ;
72e7876b
SC
508}
509
510inline wxPoint2DInt& wxPoint2DInt::operator-=(const wxPoint2DInt& pt)
511{
c3a4297c
VZ
512 m_x = m_x - pt.m_x ;
513 m_y = m_y - pt.m_y;
514 return *this ;
72e7876b
SC
515}
516
517inline wxPoint2DInt& wxPoint2DInt::operator*=(const wxPoint2DInt& pt)
518{
c3a4297c
VZ
519 m_x = m_x + pt.m_x ;
520 m_y = m_y + pt.m_y;
521 return *this ;
72e7876b
SC
522}
523
524inline wxPoint2DInt& wxPoint2DInt::operator/=(const wxPoint2DInt& pt)
525{
c3a4297c
VZ
526 m_x = m_x - pt.m_x ;
527 m_y = m_y - pt.m_y;
528 return *this ;
72e7876b
SC
529}
530
531inline bool wxPoint2DInt::operator==(const wxPoint2DInt& pt) const
532{
c3a4297c 533 return m_x == pt.m_x && m_y == pt.m_y;
72e7876b
SC
534}
535
536inline bool wxPoint2DInt::operator!=(const wxPoint2DInt& pt) const
537{
c3a4297c 538 return m_x != pt.m_x || m_y != pt.m_y;
72e7876b
SC
539}
540
541inline wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
542{
c3a4297c 543 return wxPoint2DInt( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y ) ;
72e7876b
SC
544}
545
546inline wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
547{
c3a4297c 548 return wxPoint2DInt( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y ) ;
72e7876b
SC
549}
550
551
552inline wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
553{
c3a4297c 554 return wxPoint2DInt( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y ) ;
72e7876b
SC
555}
556
557inline wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt)
558{
c3a4297c 559 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
560}
561
562inline wxPoint2DInt operator*(wxDouble n , const wxPoint2DInt& pt)
563{
c3a4297c 564 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
565}
566
567inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n)
568{
c3a4297c 569 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
570}
571
572inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxDouble n)
573{
c3a4297c 574 return wxPoint2DInt( pt.m_x * n , pt.m_y * n ) ;
72e7876b
SC
575}
576
577inline wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
578{
c3a4297c 579 return wxPoint2DInt( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y ) ;
72e7876b
SC
580}
581
582inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n)
583{
c3a4297c 584 return wxPoint2DInt( pt.m_x / n , pt.m_y / n ) ;
72e7876b
SC
585}
586
587inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxDouble n)
588{
c3a4297c 589 return wxPoint2DInt( pt.m_x / n , pt.m_y / n ) ;
72e7876b
SC
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
596class WXDLLEXPORT wxRect2DInt
597{
598public:
c3a4297c
VZ
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;
72e7876b
SC
689};
690
691inline wxRect2DInt::wxRect2DInt( const wxRect2DInt &r )
692{
c3a4297c
VZ
693 m_x = r.m_x ;
694 m_y = r.m_y ;
695 m_width = r.m_width ;
696 m_height = r.m_height ;
72e7876b 697}
c3a4297c 698
72e7876b
SC
699inline wxRect2DInt::wxRect2DInt( const wxPoint2DInt &a , const wxPoint2DInt &b)
700{
c3a4297c
VZ
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 ) ;
72e7876b
SC
705}
706
707class wxTransform2D
708{
709public :
c3a4297c
VZ
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 ;
72e7876b
SC
719} ;
720
c3a4297c 721inline void wxTransform2D::Transform( wxRect2DInt* r ) const
72e7876b
SC
722{ wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; Transform( &a ) ; Transform( &b ) ; *r = wxRect2DInt( a , b ) ; }
723
c3a4297c 724inline wxPoint2DInt wxTransform2D::Transform( const wxPoint2DInt &pt ) const
72e7876b
SC
725{ wxPoint2DInt res = pt ; Transform( &res ) ; return res ; }
726
c3a4297c 727inline wxRect2DInt wxTransform2D::Transform( const wxRect2DInt &r ) const
72e7876b
SC
728{ wxRect2DInt res = r ; Transform( &res ) ; return res ; }
729
c3a4297c 730inline void wxTransform2D::InverseTransform( wxRect2DInt* r ) const
72e7876b
SC
731{ wxPoint2DInt a = r->GetLeftTop() , b = r->GetRightBottom() ; InverseTransform( &a ) ; InverseTransform( &b ) ; *r = wxRect2DInt( a , b ) ; }
732
c3a4297c 733inline wxPoint2DInt wxTransform2D::InverseTransform( const wxPoint2DInt &pt ) const
72e7876b
SC
734{ wxPoint2DInt res = pt ; InverseTransform( &res ) ; return res ; }
735
c3a4297c 736inline wxRect2DInt wxTransform2D::InverseTransform( const wxRect2DInt &r ) const
72e7876b
SC
737{ wxRect2DInt res = r ; InverseTransform( &res ) ; return res ; }
738
c3a4297c
VZ
739
740#endif // wxUSE_GEOMETRY
741
742#endif // _WX_GEOMETRY_H_