]> git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / common / geometry.cpp
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)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma interface "geometry.h"
14 #endif
15
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_GEOMETRY
26
27 #include "wx/log.h"
28 #include <string.h>
29
30
31 #include "wx/geometry.h"
32 #include "wx/datstrm.h"
33
34 //
35 // wxPoint2D
36 //
37
38 //
39 // wxRect2D
40 //
41
42 // wxDouble version
43
44 // for the following calculations always remember
45 // that the right and bottom edges are not part of a rect
46
47 bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const
48 {
49 wxDouble left,right,bottom,top ;
50 left = wxMax ( m_x , rect.m_x ) ;
51 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width ) ;
52 top = wxMax ( m_y , rect.m_y ) ;
53 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height ) ;
54
55 if ( left < right && top < bottom )
56 {
57 return TRUE ;
58 }
59 return FALSE ;
60 }
61
62 void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
63 {
64 wxDouble left,right,bottom,top ;
65 left = wxMax ( src1.m_x , src2.m_x ) ;
66 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
67 top = wxMax ( src1.m_y , src2.m_y ) ;
68 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
69
70 if ( left < right && top < bottom )
71 {
72 dest->m_x = left ;
73 dest->m_y = top ;
74 dest->m_width = right - left ;
75 dest->m_height = bottom - top ;
76 }
77 else
78 {
79 dest->m_width = dest->m_height = 0 ;
80 }
81 }
82
83 void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
84 {
85 wxDouble left,right,bottom,top ;
86
87 left = wxMin ( src1.m_x , src2.m_x ) ;
88 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
89 top = wxMin ( src1.m_y , src2.m_y ) ;
90 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
91
92 dest->m_x = left ;
93 dest->m_y = top ;
94 dest->m_width = right - left ;
95 dest->m_height = bottom - top ;
96 }
97
98 void wxRect2DDouble::Union( const wxPoint2DDouble &pt )
99 {
100 wxDouble x = pt.m_x ;
101 wxDouble y = pt.m_y ;
102
103 if ( x < m_x )
104 {
105 SetLeft( x ) ;
106 }
107 else if ( x < m_x + m_width )
108 {
109 // contained
110 }
111 else
112 {
113 SetRight( x ) ;
114 }
115
116 if ( y < m_y )
117 {
118 SetTop( y ) ;
119 }
120 else if ( y < m_y + m_height )
121 {
122 // contained
123 }
124 else
125 {
126 SetBottom( y ) ;
127 }
128 }
129
130 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect )
131 {
132 if ( GetLeft() < rect.GetLeft() )
133 SetLeft( rect.GetLeft() ) ;
134
135 if ( GetRight() > rect.GetRight() )
136 SetRight( rect.GetRight() ) ;
137
138 if ( GetBottom() > rect.GetBottom() )
139 SetBottom( rect.GetBottom() ) ;
140
141 if ( GetTop() < rect.GetTop() )
142 SetTop( rect.GetTop() ) ;
143 }
144
145 // integer version
146
147 // for the following calculations always remember
148 // that the right and bottom edges are not part of a rect
149
150 // wxPoint2D
151
152 void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
153 {
154 stream.Write32( m_x ) ;
155 stream.Write32( m_y ) ;
156 }
157
158 void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
159 {
160 m_x = stream.Read32() ;
161 m_y = stream.Read32() ;
162 }
163
164 wxDouble wxPoint2DInt::GetVectorAngle()
165 {
166 if ( m_x == 0 )
167 {
168 if ( m_y >= 0 )
169 return 90 ;
170 else
171 return 270 ;
172 }
173 if ( m_y == 0 )
174 {
175 if ( m_x >= 0 )
176 return 0 ;
177 else
178 return 180 ;
179 }
180 wxDouble deg = atan2( m_y , m_x ) * 180 / 3.14159265359 ;
181 if ( deg < 0 )
182 {
183 deg += 360 ;
184 }
185 return deg ;
186 }
187
188
189 // wxRect2D
190
191 bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
192 {
193 wxInt32 left,right,bottom,top ;
194 left = wxMax ( m_x , rect.m_x ) ;
195 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width ) ;
196 top = wxMax ( m_y , rect.m_y ) ;
197 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height ) ;
198
199 if ( left < right && top < bottom )
200 {
201 return TRUE ;
202 }
203 return FALSE ;
204 }
205
206 void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
207 {
208 wxInt32 left,right,bottom,top ;
209 left = wxMax ( src1.m_x , src2.m_x ) ;
210 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
211 top = wxMax ( src1.m_y , src2.m_y ) ;
212 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
213
214 if ( left < right && top < bottom )
215 {
216 dest->m_x = left ;
217 dest->m_y = top ;
218 dest->m_width = right - left ;
219 dest->m_height = bottom - top ;
220 }
221 else
222 {
223 dest->m_width = dest->m_height = 0 ;
224 }
225 }
226
227 void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
228 {
229 wxInt32 left,right,bottom,top ;
230
231 left = wxMin ( src1.m_x , src2.m_x ) ;
232 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
233 top = wxMin ( src1.m_y , src2.m_y ) ;
234 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
235
236 dest->m_x = left ;
237 dest->m_y = top ;
238 dest->m_width = right - left ;
239 dest->m_height = bottom - top ;
240 }
241
242 void wxRect2DInt::Union( const wxPoint2DInt &pt )
243 {
244 wxInt32 x = pt.m_x ;
245 wxInt32 y = pt.m_y ;
246
247 if ( x < m_x )
248 {
249 SetLeft( x ) ;
250 }
251 else if ( x < m_x + m_width )
252 {
253 // contained
254 }
255 else
256 {
257 SetRight( x ) ;
258 }
259
260 if ( y < m_y )
261 {
262 SetTop( y ) ;
263 }
264 else if ( y < m_y + m_height )
265 {
266 // contained
267 }
268 else
269 {
270 SetBottom( y ) ;
271 }
272 }
273
274 void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
275 {
276 if ( GetLeft() < rect.GetLeft() )
277 SetLeft( rect.GetLeft() ) ;
278
279 if ( GetRight() > rect.GetRight() )
280 SetRight( rect.GetRight() ) ;
281
282 if ( GetBottom() > rect.GetBottom() )
283 SetBottom( rect.GetBottom() ) ;
284
285 if ( GetTop() < rect.GetTop() )
286 SetTop( rect.GetTop() ) ;
287 }
288
289 wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
290 {
291 m_x = r.m_x ;
292 m_y = r.m_y ;
293 m_width = r.m_width ;
294 m_height = r.m_height ;
295 return *this ;
296 }
297
298 void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
299 {
300 stream.Write32( m_x ) ;
301 stream.Write32( m_y ) ;
302 stream.Write32( m_width ) ;
303 stream.Write32( m_height ) ;
304 }
305
306 void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
307 {
308 m_x = stream.Read32() ;
309 m_y = stream.Read32() ;
310 m_width = stream.Read32() ;
311 m_height = stream.Read32() ;
312 }
313
314 #endif // wxUSE_GEOMETRY