]> git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
Makeproj.cpp corrections; wxTextCtrl resource loading font bug cured
[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 #include "wx/log.h"
26 #include <string.h>
27
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 // integer version
144
145 // for the following calculations always remember
146 // that the right and bottom edges are not part of a rect
147
148 // wxPoint2D
149
150 void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
151 {
152 stream.Write32( m_x ) ;
153 stream.Write32( m_y ) ;
154 }
155
156 void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
157 {
158 m_x = stream.Read32() ;
159 m_y = stream.Read32() ;
160 }
161
162 wxDouble wxPoint2DInt::GetVectorAngle()
163 {
164 if ( m_x == 0 )
165 {
166 if ( m_y >= 0 )
167 return 90 ;
168 else
169 return 270 ;
170 }
171 if ( m_y == 0 )
172 {
173 if ( m_x >= 0 )
174 return 0 ;
175 else
176 return 180 ;
177 }
178 wxDouble deg = atan2( m_y , m_x ) * 180 / 3.14159265359 ;
179 if ( deg < 0 )
180 {
181 deg += 360 ;
182 }
183 return deg ;
184 }
185
186
187 // wxRect2D
188
189 bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
190 {
191 wxInt32 left,right,bottom,top ;
192 left = wxMax ( m_x , rect.m_x ) ;
193 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width ) ;
194 top = wxMax ( m_y , rect.m_y ) ;
195 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height ) ;
196
197 if ( left < right && top < bottom )
198 {
199 return TRUE ;
200 }
201 return FALSE ;
202 }
203
204 void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
205 {
206 wxInt32 left,right,bottom,top ;
207 left = wxMax ( src1.m_x , src2.m_x ) ;
208 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
209 top = wxMax ( src1.m_y , src2.m_y ) ;
210 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
211
212 if ( left < right && top < bottom )
213 {
214 dest->m_x = left ;
215 dest->m_y = top ;
216 dest->m_width = right - left ;
217 dest->m_height = bottom - top ;
218 }
219 else
220 {
221 dest->m_width = dest->m_height = 0 ;
222 }
223 }
224
225 void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
226 {
227 wxInt32 left,right,bottom,top ;
228
229 left = wxMin ( src1.m_x , src2.m_x ) ;
230 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ) ;
231 top = wxMin ( src1.m_y , src2.m_y ) ;
232 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ) ;
233
234 dest->m_x = left ;
235 dest->m_y = top ;
236 dest->m_width = right - left ;
237 dest->m_height = bottom - top ;
238 }
239
240 void wxRect2DInt::Union( const wxPoint2DInt &pt )
241 {
242 wxInt32 x = pt.m_x ;
243 wxInt32 y = pt.m_y ;
244
245 if ( x < m_x )
246 {
247 SetLeft( x ) ;
248 }
249 else if ( x < m_x + m_width )
250 {
251 // contained
252 }
253 else
254 {
255 SetRight( x ) ;
256 }
257
258 if ( y < m_y )
259 {
260 SetTop( y ) ;
261 }
262 else if ( y < m_y + m_height )
263 {
264 // contained
265 }
266 else
267 {
268 SetBottom( y ) ;
269 }
270 }
271
272 void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
273 {
274 if ( GetLeft() < rect.GetLeft() )
275 SetLeft( rect.GetLeft() ) ;
276
277 if ( GetRight() > rect.GetRight() )
278 SetRight( rect.GetRight() ) ;
279
280 if ( GetBottom() > rect.GetBottom() )
281 SetBottom( rect.GetBottom() ) ;
282
283 if ( GetTop() < rect.GetTop() )
284 SetTop( rect.GetTop() ) ;
285 }
286
287 wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
288 {
289 m_x = r.m_x ;
290 m_y = r.m_y ;
291 m_width = r.m_width ;
292 m_height = r.m_height ;
293 return *this ;
294 }
295
296 void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
297 {
298 stream.Write32( m_x ) ;
299 stream.Write32( m_y ) ;
300 stream.Write32( m_width ) ;
301 stream.Write32( m_height ) ;
302 }
303
304 void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
305 {
306 m_x = stream.Read32() ;
307 m_y = stream.Read32() ;
308 m_width = stream.Read32() ;
309 m_height = stream.Read32() ;
310 }
311