]>
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 | ||
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 | |
11e82c1b | 45 | |
07cdd027 SC |
46 | bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const |
47 | { | |
11e82c1b VZ |
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; | |
07cdd027 SC |
59 | } |
60 | ||
11e82c1b | 61 | void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) |
07cdd027 | 62 | { |
11e82c1b VZ |
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 | } | |
07cdd027 SC |
80 | } |
81 | ||
11e82c1b | 82 | void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest ) |
07cdd027 | 83 | { |
11e82c1b VZ |
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; | |
07cdd027 SC |
95 | } |
96 | ||
11e82c1b | 97 | void wxRect2DDouble::Union( const wxPoint2DDouble &pt ) |
07cdd027 | 98 | { |
11e82c1b VZ |
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 | } | |
07cdd027 SC |
127 | } |
128 | ||
129 | void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect ) | |
130 | { | |
11e82c1b VZ |
131 | if ( GetLeft() < rect.GetLeft() ) |
132 | SetLeft( rect.GetLeft() ); | |
133 | ||
134 | if ( GetRight() > rect.GetRight() ) | |
135 | SetRight( rect.GetRight() ); | |
07cdd027 | 136 | |
11e82c1b VZ |
137 | if ( GetBottom() > rect.GetBottom() ) |
138 | SetBottom( rect.GetBottom() ); | |
07cdd027 | 139 | |
11e82c1b VZ |
140 | if ( GetTop() < rect.GetTop() ) |
141 | SetTop( rect.GetTop() ); | |
07cdd027 SC |
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 | |
11e82c1b | 148 | |
07cdd027 SC |
149 | // wxPoint2D |
150 | ||
151 | void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const | |
11e82c1b VZ |
152 | { |
153 | stream.Write32( m_x ); | |
154 | stream.Write32( m_y ); | |
07cdd027 SC |
155 | } |
156 | ||
11e82c1b VZ |
157 | void wxPoint2DInt::ReadFrom( wxDataInputStream &stream ) |
158 | { | |
159 | m_x = stream.Read32(); | |
160 | m_y = stream.Read32(); | |
07cdd027 SC |
161 | } |
162 | ||
163 | wxDouble wxPoint2DInt::GetVectorAngle() | |
164 | { | |
11e82c1b VZ |
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 | ||
180 | // casts needed MIPSpro compiler under SGI | |
181 | wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / 3.14159265359; | |
182 | if ( deg < 0 ) | |
183 | { | |
184 | deg += 360; | |
185 | } | |
186 | return deg; | |
07cdd027 SC |
187 | } |
188 | ||
11e82c1b | 189 | |
adb1282e SC |
190 | void wxPoint2DInt::SetVectorAngle( wxDouble degrees ) { |
191 | wxDouble length = this->GetVectorLength() ; | |
192 | m_x = length * cos( degrees / 180 * 3.14159265359 ) ; | |
193 | m_y = length * sin( degrees / 180 * 3.14159265359 ) ; | |
194 | } | |
195 | ||
196 | wxDouble wxPoint2DDouble::GetVectorAngle() const | |
197 | { | |
198 | if ( m_x == 0 ) | |
199 | { | |
200 | if ( m_y >= 0 ) | |
201 | return 90 ; | |
202 | else | |
203 | return 270 ; | |
204 | } | |
205 | if ( m_y == 0 ) | |
206 | { | |
207 | if ( m_x >= 0 ) | |
208 | return 0 ; | |
209 | else | |
210 | return 180 ; | |
211 | } | |
212 | wxDouble deg = atan2( m_y , m_x ) * 180 / 3.14159265359 ; | |
213 | if ( deg < 0 ) | |
214 | { | |
215 | deg += 360 ; | |
216 | } | |
217 | return deg ; | |
218 | } | |
219 | ||
220 | void wxPoint2DDouble::SetVectorAngle( wxDouble degrees ) { | |
221 | wxDouble length = this->GetVectorLength() ; | |
222 | m_x = length * cos( degrees / 180 * 3.14159265359 ) ; | |
223 | m_y = length * sin( degrees / 180 * 3.14159265359 ) ; | |
224 | } | |
225 | ||
07cdd027 | 226 | // wxRect2D |
11e82c1b | 227 | |
07cdd027 SC |
228 | bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const |
229 | { | |
11e82c1b VZ |
230 | wxInt32 left,right,bottom,top; |
231 | left = wxMax ( m_x , rect.m_x ); | |
232 | right = wxMin ( m_x+m_width, rect.m_x + rect.m_width ); | |
233 | top = wxMax ( m_y , rect.m_y ); | |
234 | bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height ); | |
235 | ||
236 | if ( left < right && top < bottom ) | |
237 | { | |
238 | return TRUE; | |
239 | } | |
240 | return FALSE; | |
07cdd027 SC |
241 | } |
242 | ||
11e82c1b | 243 | void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) |
07cdd027 | 244 | { |
11e82c1b VZ |
245 | wxInt32 left,right,bottom,top; |
246 | left = wxMax ( src1.m_x , src2.m_x ); | |
247 | right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ); | |
248 | top = wxMax ( src1.m_y , src2.m_y ); | |
249 | bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ); | |
250 | ||
251 | if ( left < right && top < bottom ) | |
252 | { | |
253 | dest->m_x = left; | |
254 | dest->m_y = top; | |
255 | dest->m_width = right - left; | |
256 | dest->m_height = bottom - top; | |
257 | } | |
258 | else | |
259 | { | |
260 | dest->m_width = dest->m_height = 0; | |
261 | } | |
07cdd027 SC |
262 | } |
263 | ||
11e82c1b | 264 | void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest ) |
07cdd027 | 265 | { |
11e82c1b VZ |
266 | wxInt32 left,right,bottom,top; |
267 | ||
268 | left = wxMin ( src1.m_x , src2.m_x ); | |
269 | right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width ); | |
270 | top = wxMin ( src1.m_y , src2.m_y ); | |
271 | bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height ); | |
272 | ||
273 | dest->m_x = left; | |
274 | dest->m_y = top; | |
275 | dest->m_width = right - left; | |
276 | dest->m_height = bottom - top; | |
07cdd027 SC |
277 | } |
278 | ||
11e82c1b | 279 | void wxRect2DInt::Union( const wxPoint2DInt &pt ) |
07cdd027 | 280 | { |
11e82c1b VZ |
281 | wxInt32 x = pt.m_x; |
282 | wxInt32 y = pt.m_y; | |
283 | ||
284 | if ( x < m_x ) | |
285 | { | |
286 | SetLeft( x ); | |
287 | } | |
288 | else if ( x < m_x + m_width ) | |
289 | { | |
290 | // contained | |
291 | } | |
292 | else | |
293 | { | |
294 | SetRight( x ); | |
295 | } | |
296 | ||
297 | if ( y < m_y ) | |
298 | { | |
299 | SetTop( y ); | |
300 | } | |
301 | else if ( y < m_y + m_height ) | |
302 | { | |
303 | // contained | |
304 | } | |
305 | else | |
306 | { | |
307 | SetBottom( y ); | |
308 | } | |
07cdd027 SC |
309 | } |
310 | ||
311 | void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect ) | |
312 | { | |
11e82c1b VZ |
313 | if ( GetLeft() < rect.GetLeft() ) |
314 | SetLeft( rect.GetLeft() ); | |
315 | ||
316 | if ( GetRight() > rect.GetRight() ) | |
317 | SetRight( rect.GetRight() ); | |
07cdd027 | 318 | |
11e82c1b VZ |
319 | if ( GetBottom() > rect.GetBottom() ) |
320 | SetBottom( rect.GetBottom() ); | |
07cdd027 | 321 | |
11e82c1b VZ |
322 | if ( GetTop() < rect.GetTop() ) |
323 | SetTop( rect.GetTop() ); | |
07cdd027 SC |
324 | } |
325 | ||
326 | wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r ) | |
11e82c1b VZ |
327 | { |
328 | m_x = r.m_x; | |
329 | m_y = r.m_y; | |
330 | m_width = r.m_width; | |
331 | m_height = r.m_height; | |
332 | return *this; | |
07cdd027 SC |
333 | } |
334 | ||
335 | void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const | |
11e82c1b VZ |
336 | { |
337 | stream.Write32( m_x ); | |
338 | stream.Write32( m_y ); | |
339 | stream.Write32( m_width ); | |
340 | stream.Write32( m_height ); | |
07cdd027 SC |
341 | } |
342 | ||
11e82c1b VZ |
343 | void wxRect2DInt::ReadFrom( wxDataInputStream &stream ) |
344 | { | |
345 | m_x = stream.Read32(); | |
346 | m_y = stream.Read32(); | |
347 | m_width = stream.Read32(); | |
348 | m_height = stream.Read32(); | |
07cdd027 SC |
349 | } |
350 | ||
c3a4297c | 351 | #endif // wxUSE_GEOMETRY |