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