]> git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
added wxConvFileName which is a variable, not define as wxConvFile; added comments...
[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.h"
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 //
33 // wxPoint2D
34 //
35
36 //
37 // wxRect2D
38 //
39
40 // wxDouble version
41
42 // for the following calculations always remember
43 // that the right and bottom edges are not part of a rect
44
45 bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const
46 {
47 wxDouble left,right,bottom,top;
48 left = wxMax ( m_x , rect.m_x );
49 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
50 top = wxMax ( m_y , rect.m_y );
51 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
52
53 if ( left < right && top < bottom )
54 {
55 return true;
56 }
57 return false;
58 }
59
60 void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
61 {
62 wxDouble left,right,bottom,top;
63 left = wxMax ( src1.m_x , src2.m_x );
64 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
65 top = wxMax ( src1.m_y , src2.m_y );
66 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
67
68 if ( left < right && top < bottom )
69 {
70 dest->m_x = left;
71 dest->m_y = top;
72 dest->m_width = right - left;
73 dest->m_height = bottom - top;
74 }
75 else
76 {
77 dest->m_width = dest->m_height = 0;
78 }
79 }
80
81 void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
82 {
83 wxDouble left,right,bottom,top;
84
85 left = wxMin ( src1.m_x , src2.m_x );
86 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
87 top = wxMin ( src1.m_y , src2.m_y );
88 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
89
90 dest->m_x = left;
91 dest->m_y = top;
92 dest->m_width = right - left;
93 dest->m_height = bottom - top;
94 }
95
96 void wxRect2DDouble::Union( const wxPoint2DDouble &pt )
97 {
98 wxDouble x = pt.m_x;
99 wxDouble y = pt.m_y;
100
101 if ( x < m_x )
102 {
103 SetLeft( x );
104 }
105 else if ( x < m_x + m_width )
106 {
107 // contained
108 }
109 else
110 {
111 SetRight( x );
112 }
113
114 if ( y < m_y )
115 {
116 SetTop( y );
117 }
118 else if ( y < m_y + m_height )
119 {
120 // contained
121 }
122 else
123 {
124 SetBottom( y );
125 }
126 }
127
128 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect )
129 {
130 if ( GetLeft() < rect.GetLeft() )
131 SetLeft( rect.GetLeft() );
132
133 if ( GetRight() > rect.GetRight() )
134 SetRight( rect.GetRight() );
135
136 if ( GetBottom() > rect.GetBottom() )
137 SetBottom( rect.GetBottom() );
138
139 if ( GetTop() < rect.GetTop() )
140 SetTop( rect.GetTop() );
141 }
142
143 wxRect2DDouble& wxRect2DDouble::operator=( const wxRect2DDouble &r )
144 {
145 m_x = r.m_x;
146 m_y = r.m_y;
147 m_width = r.m_width;
148 m_height = r.m_height;
149 return *this;
150 }
151
152 // integer version
153
154 // for the following calculations always remember
155 // that the right and bottom edges are not part of a rect
156
157 // wxPoint2D
158
159 #if wxUSE_STREAMS
160 void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
161 {
162 stream.Write32( m_x );
163 stream.Write32( m_y );
164 }
165
166 void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
167 {
168 m_x = stream.Read32();
169 m_y = stream.Read32();
170 }
171 #endif // wxUSE_STREAMS
172
173 wxDouble wxPoint2DInt::GetVectorAngle() const
174 {
175 if ( m_x == 0 )
176 {
177 if ( m_y >= 0 )
178 return 90;
179 else
180 return 270;
181 }
182 if ( m_y == 0 )
183 {
184 if ( m_x >= 0 )
185 return 0;
186 else
187 return 180;
188 }
189
190 // casts needed for MIPSpro compiler under SGI
191 wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / M_PI;
192 if ( deg < 0 )
193 {
194 deg += 360;
195 }
196 return deg;
197 }
198
199
200 void wxPoint2DInt::SetVectorAngle( wxDouble degrees )
201 {
202 wxDouble length = GetVectorLength();
203 m_x = (int)(length * cos( degrees / 180 * M_PI ));
204 m_y = (int)(length * sin( degrees / 180 * M_PI ));
205 }
206
207 wxDouble wxPoint2DDouble::GetVectorAngle() const
208 {
209 if ( m_x == 0 )
210 {
211 if ( m_y >= 0 )
212 return 90;
213 else
214 return 270;
215 }
216 if ( m_y == 0 )
217 {
218 if ( m_x >= 0 )
219 return 0;
220 else
221 return 180;
222 }
223 wxDouble deg = atan2( m_y , m_x ) * 180 / M_PI;
224 if ( deg < 0 )
225 {
226 deg += 360;
227 }
228 return deg;
229 }
230
231 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees )
232 {
233 wxDouble length = GetVectorLength();
234 m_x = length * cos( degrees / 180 * M_PI );
235 m_y = length * sin( degrees / 180 * M_PI );
236 }
237
238 // wxRect2D
239
240 bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
241 {
242 wxInt32 left,right,bottom,top;
243 left = wxMax ( m_x , rect.m_x );
244 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
245 top = wxMax ( m_y , rect.m_y );
246 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
247
248 if ( left < right && top < bottom )
249 {
250 return true;
251 }
252 return false;
253 }
254
255 void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
256 {
257 wxInt32 left,right,bottom,top;
258 left = wxMax ( src1.m_x , src2.m_x );
259 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
260 top = wxMax ( src1.m_y , src2.m_y );
261 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
262
263 if ( left < right && top < bottom )
264 {
265 dest->m_x = left;
266 dest->m_y = top;
267 dest->m_width = right - left;
268 dest->m_height = bottom - top;
269 }
270 else
271 {
272 dest->m_width = dest->m_height = 0;
273 }
274 }
275
276 void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
277 {
278 wxInt32 left,right,bottom,top;
279
280 left = wxMin ( src1.m_x , src2.m_x );
281 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
282 top = wxMin ( src1.m_y , src2.m_y );
283 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
284
285 dest->m_x = left;
286 dest->m_y = top;
287 dest->m_width = right - left;
288 dest->m_height = bottom - top;
289 }
290
291 void wxRect2DInt::Union( const wxPoint2DInt &pt )
292 {
293 wxInt32 x = pt.m_x;
294 wxInt32 y = pt.m_y;
295
296 if ( x < m_x )
297 {
298 SetLeft( x );
299 }
300 else if ( x < m_x + m_width )
301 {
302 // contained
303 }
304 else
305 {
306 SetRight( x );
307 }
308
309 if ( y < m_y )
310 {
311 SetTop( y );
312 }
313 else if ( y < m_y + m_height )
314 {
315 // contained
316 }
317 else
318 {
319 SetBottom( y );
320 }
321 }
322
323 void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
324 {
325 if ( GetLeft() < rect.GetLeft() )
326 SetLeft( rect.GetLeft() );
327
328 if ( GetRight() > rect.GetRight() )
329 SetRight( rect.GetRight() );
330
331 if ( GetBottom() > rect.GetBottom() )
332 SetBottom( rect.GetBottom() );
333
334 if ( GetTop() < rect.GetTop() )
335 SetTop( rect.GetTop() );
336 }
337
338 wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
339 {
340 m_x = r.m_x;
341 m_y = r.m_y;
342 m_width = r.m_width;
343 m_height = r.m_height;
344 return *this;
345 }
346
347 #if wxUSE_STREAMS
348 void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
349 {
350 stream.Write32( m_x );
351 stream.Write32( m_y );
352 stream.Write32( m_width );
353 stream.Write32( m_height );
354 }
355
356 void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
357 {
358 m_x = stream.Read32();
359 m_y = stream.Read32();
360 m_width = stream.Read32();
361 m_height = stream.Read32();
362 }
363 #endif // wxUSE_STREAMS
364
365 #endif // wxUSE_GEOMETRY