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