]>
Commit | Line | Data |
---|---|---|
07cdd027 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e82c1b | 2 | // Name: common/geometry.cpp |
07cdd027 SC |
3 | // Purpose: Common Geometry Classes |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 08/05/99 | |
11e82c1b VZ |
7 | // RCS-ID: |
8 | // Copyright: (c) 1999 Stefan Csomor | |
9 | // Licence: wxWindows licence | |
07cdd027 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
cd79f7b0 | 13 | #pragma implementation "geometry.cpp" |
07cdd027 SC |
14 | #endif |
15 | ||
07cdd027 SC |
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 | ||
c3a4297c VZ |
24 | #if wxUSE_GEOMETRY |
25 | ||
07cdd027 SC |
26 | #include "wx/log.h" |
27 | #include <string.h> | |
28 | ||
07cdd027 SC |
29 | #include "wx/geometry.h" |
30 | #include "wx/datstrm.h" | |
31 | ||
ade7e576 VZ |
32 | // normally this is defined in <math.h> |
33 | #ifndef M_PI | |
34 | #define M_PI 3.14159265358979323846 | |
35 | #endif | |
36 | ||
07cdd027 SC |
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 | |
11e82c1b | 49 | |
07cdd027 SC |
50 | bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const |
51 | { | |
11e82c1b VZ |
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; | |
07cdd027 SC |
63 | } |
64 | ||
11e82c1b | 65 | void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) |
07cdd027 | 66 | { |
11e82c1b VZ |
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 | } | |
07cdd027 SC |
84 | } |
85 | ||
11e82c1b | 86 | void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) |
07cdd027 | 87 | { |
11e82c1b VZ |
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; | |
07cdd027 SC |
99 | } |
100 | ||
11e82c1b | 101 | void wxRect2DDouble::Union( const wxPoint2DDouble &pt ) |
07cdd027 | 102 | { |
11e82c1b VZ |
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 | } | |
07cdd027 SC |
131 | } |
132 | ||
133 | void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect ) | |
134 | { | |
11e82c1b VZ |
135 | if ( GetLeft() < rect.GetLeft() ) |
136 | SetLeft( rect.GetLeft() ); | |
137 | ||
138 | if ( GetRight() > rect.GetRight() ) | |
139 | SetRight( rect.GetRight() ); | |
07cdd027 | 140 | |
11e82c1b VZ |
141 | if ( GetBottom() > rect.GetBottom() ) |
142 | SetBottom( rect.GetBottom() ); | |
07cdd027 | 143 | |
11e82c1b VZ |
144 | if ( GetTop() < rect.GetTop() ) |
145 | SetTop( rect.GetTop() ); | |
07cdd027 SC |
146 | } |
147 | ||
148 | // integer version | |
149 | ||
150 | // for the following calculations always remember | |
151 | // that the right and bottom edges are not part of a rect | |
11e82c1b | 152 | |
07cdd027 SC |
153 | // wxPoint2D |
154 | ||
155 | void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const | |
11e82c1b VZ |
156 | { |
157 | stream.Write32( m_x ); | |
158 | stream.Write32( m_y ); | |
07cdd027 SC |
159 | } |
160 | ||
11e82c1b VZ |
161 | void wxPoint2DInt::ReadFrom( wxDataInputStream &stream ) |
162 | { | |
163 | m_x = stream.Read32(); | |
164 | m_y = stream.Read32(); | |
07cdd027 SC |
165 | } |
166 | ||
2b5f62a0 | 167 | wxDouble wxPoint2DInt::GetVectorAngle() const |
07cdd027 | 168 | { |
11e82c1b VZ |
169 | if ( m_x == 0 ) |
170 | { | |
171 | if ( m_y >= 0 ) | |
172 | return 90; | |
173 | else | |
174 | return 270; | |
175 | } | |
176 | if ( m_y == 0 ) | |
177 | { | |
178 | if ( m_x >= 0 ) | |
179 | return 0; | |
180 | else | |
181 | return 180; | |
182 | } | |
183 | ||
ade7e576 VZ |
184 | // casts needed for MIPSpro compiler under SGI |
185 | wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / M_PI; | |
11e82c1b VZ |
186 | if ( deg < 0 ) |
187 | { | |
188 | deg += 360; | |
189 | } | |
190 | return deg; | |
07cdd027 SC |
191 | } |
192 | ||
11e82c1b | 193 | |
ade7e576 VZ |
194 | void wxPoint2DInt::SetVectorAngle( wxDouble degrees ) |
195 | { | |
196 | wxDouble length = GetVectorLength(); | |
197 | m_x = (int)(length * cos( degrees / 180 * M_PI )); | |
198 | m_y = (int)(length * sin( degrees / 180 * M_PI )); | |
adb1282e | 199 | } |
ade7e576 | 200 | |
adb1282e SC |
201 | wxDouble wxPoint2DDouble::GetVectorAngle() const |
202 | { | |
ade7e576 VZ |
203 | if ( m_x == 0 ) |
204 | { | |
205 | if ( m_y >= 0 ) | |
206 | return 90; | |
207 | else | |
208 | return 270; | |
209 | } | |
210 | if ( m_y == 0 ) | |
211 | { | |
212 | if ( m_x >= 0 ) | |
213 | return 0; | |
214 | else | |
215 | return 180; | |
216 | } | |
217 | wxDouble deg = atan2( m_y , m_x ) * 180 / M_PI; | |
218 | if ( deg < 0 ) | |
219 | { | |
220 | deg += 360; | |
221 | } | |
222 | return deg; | |
adb1282e SC |
223 | } |
224 | ||
ade7e576 VZ |
225 | void wxPoint2DDouble::SetVectorAngle( wxDouble degrees ) |
226 | { | |
227 | wxDouble length = GetVectorLength(); | |
228 | m_x = length * cos( degrees / 180 * M_PI ); | |
229 | m_y = length * sin( degrees / 180 * M_PI ); | |
adb1282e SC |
230 | } |
231 | ||
07cdd027 | 232 | // wxRect2D |
11e82c1b | 233 | |
07cdd027 SC |
234 | bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const |
235 | { | |
11e82c1b VZ |
236 | wxInt32 left,right,bottom,top; |
237 | left = wxMax ( m_x , rect.m_x ); | |
238 | right = wxMin ( m_x+m_width, rect.m_x + rect.m_width ); | |
239 | top = wxMax ( m_y , rect.m_y ); | |
240 | bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height ); | |
241 | ||
242 | if ( left < right && top < bottom ) | |
243 | { | |
244 | return TRUE; | |
245 | } | |
246 | return FALSE; | |
07cdd027 SC |
247 | } |
248 | ||
11e82c1b | 249 | void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) |
07cdd027 | 250 | { |
11e82c1b VZ |
251 | wxInt32 left,right,bottom,top; |
252 | left = wxMax ( src1.m_x , src2.m_x ); | |
253 | right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ); | |
254 | top = wxMax ( src1.m_y , src2.m_y ); | |
255 | bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ); | |
256 | ||
257 | if ( left < right && top < bottom ) | |
258 | { | |
259 | dest->m_x = left; | |
260 | dest->m_y = top; | |
261 | dest->m_width = right - left; | |
262 | dest->m_height = bottom - top; | |
263 | } | |
264 | else | |
265 | { | |
266 | dest->m_width = dest->m_height = 0; | |
267 | } | |
07cdd027 SC |
268 | } |
269 | ||
11e82c1b | 270 | void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) |
07cdd027 | 271 | { |
11e82c1b VZ |
272 | wxInt32 left,right,bottom,top; |
273 | ||
274 | left = wxMin ( src1.m_x , src2.m_x ); | |
275 | right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ); | |
276 | top = wxMin ( src1.m_y , src2.m_y ); | |
277 | bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ); | |
278 | ||
279 | dest->m_x = left; | |
280 | dest->m_y = top; | |
281 | dest->m_width = right - left; | |
282 | dest->m_height = bottom - top; | |
07cdd027 SC |
283 | } |
284 | ||
11e82c1b | 285 | void wxRect2DInt::Union( const wxPoint2DInt &pt ) |
07cdd027 | 286 | { |
11e82c1b VZ |
287 | wxInt32 x = pt.m_x; |
288 | wxInt32 y = pt.m_y; | |
289 | ||
290 | if ( x < m_x ) | |
291 | { | |
292 | SetLeft( x ); | |
293 | } | |
294 | else if ( x < m_x + m_width ) | |
295 | { | |
296 | // contained | |
297 | } | |
298 | else | |
299 | { | |
300 | SetRight( x ); | |
301 | } | |
302 | ||
303 | if ( y < m_y ) | |
304 | { | |
305 | SetTop( y ); | |
306 | } | |
307 | else if ( y < m_y + m_height ) | |
308 | { | |
309 | // contained | |
310 | } | |
311 | else | |
312 | { | |
313 | SetBottom( y ); | |
314 | } | |
07cdd027 SC |
315 | } |
316 | ||
317 | void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect ) | |
318 | { | |
11e82c1b VZ |
319 | if ( GetLeft() < rect.GetLeft() ) |
320 | SetLeft( rect.GetLeft() ); | |
321 | ||
322 | if ( GetRight() > rect.GetRight() ) | |
323 | SetRight( rect.GetRight() ); | |
07cdd027 | 324 | |
11e82c1b VZ |
325 | if ( GetBottom() > rect.GetBottom() ) |
326 | SetBottom( rect.GetBottom() ); | |
07cdd027 | 327 | |
11e82c1b VZ |
328 | if ( GetTop() < rect.GetTop() ) |
329 | SetTop( rect.GetTop() ); | |
07cdd027 SC |
330 | } |
331 | ||
332 | wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r ) | |
11e82c1b VZ |
333 | { |
334 | m_x = r.m_x; | |
335 | m_y = r.m_y; | |
336 | m_width = r.m_width; | |
337 | m_height = r.m_height; | |
338 | return *this; | |
07cdd027 SC |
339 | } |
340 | ||
341 | void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const | |
11e82c1b VZ |
342 | { |
343 | stream.Write32( m_x ); | |
344 | stream.Write32( m_y ); | |
345 | stream.Write32( m_width ); | |
346 | stream.Write32( m_height ); | |
07cdd027 SC |
347 | } |
348 | ||
11e82c1b VZ |
349 | void wxRect2DInt::ReadFrom( wxDataInputStream &stream ) |
350 | { | |
351 | m_x = stream.Read32(); | |
352 | m_y = stream.Read32(); | |
353 | m_width = stream.Read32(); | |
354 | m_height = stream.Read32(); | |
07cdd027 SC |
355 | } |
356 | ||
c3a4297c | 357 | #endif // wxUSE_GEOMETRY |