]> git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
Make public headers compatible with Objective-C++ with ARC.
[wxWidgets.git] / src / common / geometry.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/geometry.cpp
3 // Purpose: Common Geometry Classes
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 08/05/99
7 // Copyright: (c) 1999 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_GEOMETRY
19
20 #include "wx/geometry.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #endif
25
26 #include <string.h>
27
28 #include "wx/datstrm.h"
29
30 //
31 // wxPoint2D
32 //
33
34 //
35 // wxRect2D
36 //
37
38 // wxDouble version
39
40 // for the following calculations always remember
41 // that the right and bottom edges are not part of a rect
42
43 bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const
44 {
45 wxDouble left,right,bottom,top;
46 left = wxMax ( m_x , rect.m_x );
47 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
48 top = wxMax ( m_y , rect.m_y );
49 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
50
51 if ( left < right && top < bottom )
52 {
53 return true;
54 }
55 return false;
56 }
57
58 void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
59 {
60 wxDouble left,right,bottom,top;
61 left = wxMax ( src1.m_x , src2.m_x );
62 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
63 top = wxMax ( src1.m_y , src2.m_y );
64 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
65
66 if ( left < right && top < bottom )
67 {
68 dest->m_x = left;
69 dest->m_y = top;
70 dest->m_width = right - left;
71 dest->m_height = bottom - top;
72 }
73 else
74 {
75 dest->m_width = dest->m_height = 0;
76 }
77 }
78
79 void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
80 {
81 wxDouble left,right,bottom,top;
82
83 left = wxMin ( src1.m_x , src2.m_x );
84 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
85 top = wxMin ( src1.m_y , src2.m_y );
86 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
87
88 dest->m_x = left;
89 dest->m_y = top;
90 dest->m_width = right - left;
91 dest->m_height = bottom - top;
92 }
93
94 void wxRect2DDouble::Union( const wxPoint2DDouble &pt )
95 {
96 wxDouble x = pt.m_x;
97 wxDouble y = pt.m_y;
98
99 if ( x < m_x )
100 {
101 SetLeft( x );
102 }
103 else if ( x < m_x + m_width )
104 {
105 // contained
106 }
107 else
108 {
109 SetRight( x );
110 }
111
112 if ( y < m_y )
113 {
114 SetTop( y );
115 }
116 else if ( y < m_y + m_height )
117 {
118 // contained
119 }
120 else
121 {
122 SetBottom( y );
123 }
124 }
125
126 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect )
127 {
128 if ( GetLeft() < rect.GetLeft() )
129 SetLeft( rect.GetLeft() );
130
131 if ( GetRight() > rect.GetRight() )
132 SetRight( rect.GetRight() );
133
134 if ( GetBottom() > rect.GetBottom() )
135 SetBottom( rect.GetBottom() );
136
137 if ( GetTop() < rect.GetTop() )
138 SetTop( rect.GetTop() );
139 }
140
141 wxRect2DDouble& wxRect2DDouble::operator=( const wxRect2DDouble &r )
142 {
143 m_x = r.m_x;
144 m_y = r.m_y;
145 m_width = r.m_width;
146 m_height = r.m_height;
147 return *this;
148 }
149
150 // integer version
151
152 // for the following calculations always remember
153 // that the right and bottom edges are not part of a rect
154
155 // wxPoint2D
156
157 #if wxUSE_STREAMS
158 void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
159 {
160 stream.Write32( m_x );
161 stream.Write32( m_y );
162 }
163
164 void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
165 {
166 m_x = stream.Read32();
167 m_y = stream.Read32();
168 }
169 #endif // wxUSE_STREAMS
170
171 wxDouble wxPoint2DInt::GetVectorAngle() const
172 {
173 if ( m_x == 0 )
174 {
175 if ( m_y >= 0 )
176 return 90;
177 else
178 return 270;
179 }
180 if ( m_y == 0 )
181 {
182 if ( m_x >= 0 )
183 return 0;
184 else
185 return 180;
186 }
187
188 // casts needed for MIPSpro compiler under SGI
189 wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / M_PI;
190 if ( deg < 0 )
191 {
192 deg += 360;
193 }
194 return deg;
195 }
196
197
198 void wxPoint2DInt::SetVectorAngle( wxDouble degrees )
199 {
200 wxDouble length = GetVectorLength();
201 m_x = (int)(length * cos( degrees / 180 * M_PI ));
202 m_y = (int)(length * sin( degrees / 180 * M_PI ));
203 }
204
205 wxDouble wxPoint2DDouble::GetVectorAngle() const
206 {
207 if ( wxIsNullDouble(m_x) )
208 {
209 if ( m_y >= 0 )
210 return 90;
211 else
212 return 270;
213 }
214 if ( wxIsNullDouble(m_y) )
215 {
216 if ( m_x >= 0 )
217 return 0;
218 else
219 return 180;
220 }
221 wxDouble deg = atan2( m_y , m_x ) * 180 / M_PI;
222 if ( deg < 0 )
223 {
224 deg += 360;
225 }
226 return deg;
227 }
228
229 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees )
230 {
231 wxDouble length = GetVectorLength();
232 m_x = length * cos( degrees / 180 * M_PI );
233 m_y = length * sin( degrees / 180 * M_PI );
234 }
235
236 // wxRect2D
237
238 bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
239 {
240 wxInt32 left,right,bottom,top;
241 left = wxMax ( m_x , rect.m_x );
242 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
243 top = wxMax ( m_y , rect.m_y );
244 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
245
246 if ( left < right && top < bottom )
247 {
248 return true;
249 }
250 return false;
251 }
252
253 void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
254 {
255 wxInt32 left,right,bottom,top;
256 left = wxMax ( src1.m_x , src2.m_x );
257 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
258 top = wxMax ( src1.m_y , src2.m_y );
259 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
260
261 if ( left < right && top < bottom )
262 {
263 dest->m_x = left;
264 dest->m_y = top;
265 dest->m_width = right - left;
266 dest->m_height = bottom - top;
267 }
268 else
269 {
270 dest->m_width = dest->m_height = 0;
271 }
272 }
273
274 void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
275 {
276 wxInt32 left,right,bottom,top;
277
278 left = wxMin ( src1.m_x , src2.m_x );
279 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
280 top = wxMin ( src1.m_y , src2.m_y );
281 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
282
283 dest->m_x = left;
284 dest->m_y = top;
285 dest->m_width = right - left;
286 dest->m_height = bottom - top;
287 }
288
289 void wxRect2DInt::Union( const wxPoint2DInt &pt )
290 {
291 wxInt32 x = pt.m_x;
292 wxInt32 y = pt.m_y;
293
294 if ( x < m_x )
295 {
296 SetLeft( x );
297 }
298 else if ( x < m_x + m_width )
299 {
300 // contained
301 }
302 else
303 {
304 SetRight( x );
305 }
306
307 if ( y < m_y )
308 {
309 SetTop( y );
310 }
311 else if ( y < m_y + m_height )
312 {
313 // contained
314 }
315 else
316 {
317 SetBottom( y );
318 }
319 }
320
321 void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
322 {
323 if ( GetLeft() < rect.GetLeft() )
324 SetLeft( rect.GetLeft() );
325
326 if ( GetRight() > rect.GetRight() )
327 SetRight( rect.GetRight() );
328
329 if ( GetBottom() > rect.GetBottom() )
330 SetBottom( rect.GetBottom() );
331
332 if ( GetTop() < rect.GetTop() )
333 SetTop( rect.GetTop() );
334 }
335
336 wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
337 {
338 m_x = r.m_x;
339 m_y = r.m_y;
340 m_width = r.m_width;
341 m_height = r.m_height;
342 return *this;
343 }
344
345 #if wxUSE_STREAMS
346 void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
347 {
348 stream.Write32( m_x );
349 stream.Write32( m_y );
350 stream.Write32( m_width );
351 stream.Write32( m_height );
352 }
353
354 void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
355 {
356 m_x = stream.Read32();
357 m_y = stream.Read32();
358 m_width = stream.Read32();
359 m_height = stream.Read32();
360 }
361 #endif // wxUSE_STREAMS
362
363
364 // wxTransform2D
365
366 void wxTransform2D::Transform( wxRect2DInt* r ) const
367 {
368 wxPoint2DInt a = r->GetLeftTop(), b = r->GetRightBottom();
369 Transform( &a );
370 Transform( &b );
371 *r = wxRect2DInt( a, b );
372 }
373
374 wxPoint2DInt wxTransform2D::Transform( const wxPoint2DInt &pt ) const
375 {
376 wxPoint2DInt res = pt;
377 Transform( &res );
378 return res;
379 }
380
381 wxRect2DInt wxTransform2D::Transform( const wxRect2DInt &r ) const
382 {
383 wxRect2DInt res = r;
384 Transform( &res );
385 return res;
386 }
387
388 void wxTransform2D::InverseTransform( wxRect2DInt* r ) const
389 {
390 wxPoint2DInt a = r->GetLeftTop(), b = r->GetRightBottom();
391 InverseTransform( &a );
392 InverseTransform( &b );
393 *r = wxRect2DInt( a , b );
394 }
395
396 wxPoint2DInt wxTransform2D::InverseTransform( const wxPoint2DInt &pt ) const
397 {
398 wxPoint2DInt res = pt;
399 InverseTransform( &res );
400 return res;
401 }
402
403 wxRect2DInt wxTransform2D::InverseTransform( const wxRect2DInt &r ) const
404 {
405 wxRect2DInt res = r;
406 InverseTransform( &res );
407 return res;
408 }
409
410 #endif // wxUSE_GEOMETRY