]>
git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/geometry.cpp
3 // Purpose: Common Geometry Classes
4 // Author: Stefan Csomor
8 // Copyright: (c) 1999 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "geometry.cpp"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/geometry.h"
30 #include "wx/datstrm.h"
32 // normally this is defined in <math.h>
34 #define M_PI 3.14159265358979323846
47 // for the following calculations always remember
48 // that the right and bottom edges are not part of a rect
50 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
52 wxDouble left
,right
,bottom
,top
;
53 left
= wxMax ( m_x
, rect
.m_x
);
54 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
55 top
= wxMax ( m_y
, rect
.m_y
);
56 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
58 if ( left
< right
&& top
< bottom
)
65 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
67 wxDouble left
,right
,bottom
,top
;
68 left
= wxMax ( src1
.m_x
, src2
.m_x
);
69 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
70 top
= wxMax ( src1
.m_y
, src2
.m_y
);
71 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
73 if ( left
< right
&& top
< bottom
)
77 dest
->m_width
= right
- left
;
78 dest
->m_height
= bottom
- top
;
82 dest
->m_width
= dest
->m_height
= 0;
86 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
88 wxDouble left
,right
,bottom
,top
;
90 left
= wxMin ( src1
.m_x
, src2
.m_x
);
91 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
92 top
= wxMin ( src1
.m_y
, src2
.m_y
);
93 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
97 dest
->m_width
= right
- left
;
98 dest
->m_height
= bottom
- top
;
101 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
110 else if ( x
< m_x
+ m_width
)
123 else if ( y
< m_y
+ m_height
)
133 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
135 if ( GetLeft() < rect
.GetLeft() )
136 SetLeft( rect
.GetLeft() );
138 if ( GetRight() > rect
.GetRight() )
139 SetRight( rect
.GetRight() );
141 if ( GetBottom() > rect
.GetBottom() )
142 SetBottom( rect
.GetBottom() );
144 if ( GetTop() < rect
.GetTop() )
145 SetTop( rect
.GetTop() );
148 wxRect2DDouble
& wxRect2DDouble::operator=( const wxRect2DDouble
&r
)
153 m_height
= r
.m_height
;
159 // for the following calculations always remember
160 // that the right and bottom edges are not part of a rect
165 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
167 stream
.Write32( m_x
);
168 stream
.Write32( m_y
);
171 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
173 m_x
= stream
.Read32();
174 m_y
= stream
.Read32();
176 #endif // wxUSE_STREAMS
178 wxDouble
wxPoint2DInt::GetVectorAngle() const
195 // casts needed for MIPSpro compiler under SGI
196 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
205 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
207 wxDouble length
= GetVectorLength();
208 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
209 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
212 wxDouble
wxPoint2DDouble::GetVectorAngle() const
228 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
236 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
238 wxDouble length
= GetVectorLength();
239 m_x
= length
* cos( degrees
/ 180 * M_PI
);
240 m_y
= length
* sin( degrees
/ 180 * M_PI
);
245 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
247 wxInt32 left
,right
,bottom
,top
;
248 left
= wxMax ( m_x
, rect
.m_x
);
249 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
250 top
= wxMax ( m_y
, rect
.m_y
);
251 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
253 if ( left
< right
&& top
< bottom
)
260 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
262 wxInt32 left
,right
,bottom
,top
;
263 left
= wxMax ( src1
.m_x
, src2
.m_x
);
264 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
265 top
= wxMax ( src1
.m_y
, src2
.m_y
);
266 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
268 if ( left
< right
&& top
< bottom
)
272 dest
->m_width
= right
- left
;
273 dest
->m_height
= bottom
- top
;
277 dest
->m_width
= dest
->m_height
= 0;
281 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
283 wxInt32 left
,right
,bottom
,top
;
285 left
= wxMin ( src1
.m_x
, src2
.m_x
);
286 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
287 top
= wxMin ( src1
.m_y
, src2
.m_y
);
288 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
292 dest
->m_width
= right
- left
;
293 dest
->m_height
= bottom
- top
;
296 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
305 else if ( x
< m_x
+ m_width
)
318 else if ( y
< m_y
+ m_height
)
328 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
330 if ( GetLeft() < rect
.GetLeft() )
331 SetLeft( rect
.GetLeft() );
333 if ( GetRight() > rect
.GetRight() )
334 SetRight( rect
.GetRight() );
336 if ( GetBottom() > rect
.GetBottom() )
337 SetBottom( rect
.GetBottom() );
339 if ( GetTop() < rect
.GetTop() )
340 SetTop( rect
.GetTop() );
343 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
348 m_height
= r
.m_height
;
353 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
355 stream
.Write32( m_x
);
356 stream
.Write32( m_y
);
357 stream
.Write32( m_width
);
358 stream
.Write32( m_height
);
361 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
363 m_x
= stream
.Read32();
364 m_y
= stream
.Read32();
365 m_width
= stream
.Read32();
366 m_height
= stream
.Read32();
368 #endif // wxUSE_STREAMS
370 #endif // wxUSE_GEOMETRY