]> git.saurik.com Git - wxWidgets.git/blame - src/common/geometry.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[wxWidgets.git] / src / common / geometry.cpp
CommitLineData
07cdd027 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/common/geometry.cpp
07cdd027
SC
3// Purpose: Common Geometry Classes
4// Author: Stefan Csomor
5// Modified by:
6// Created: 08/05/99
11e82c1b 7// Copyright: (c) 1999 Stefan Csomor
65571936 8// Licence: wxWindows licence
07cdd027
SC
9/////////////////////////////////////////////////////////////////////////////
10
07cdd027
SC
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
07cdd027 14#ifdef __BORLANDC__
e4db172a 15 #pragma hdrstop
07cdd027
SC
16#endif
17
c3a4297c
VZ
18#if wxUSE_GEOMETRY
19
e4db172a
WS
20#include "wx/geometry.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
24#endif
25
07cdd027
SC
26#include <string.h>
27
07cdd027
SC
28#include "wx/datstrm.h"
29
30//
31// wxPoint2D
32//
33
34//
35// wxRect2D
36//
37
38// wxDouble version
39
40// for the following calculations always remember
41// that the right and bottom edges are not part of a rect
11e82c1b 42
07cdd027
SC
43bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const
44{
11e82c1b
VZ
45 wxDouble left,right,bottom,top;
46 left = wxMax ( m_x , rect.m_x );
47 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
48 top = wxMax ( m_y , rect.m_y );
49 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
50
51 if ( left < right && top < bottom )
52 {
5d3e7b52 53 return true;
11e82c1b 54 }
5d3e7b52 55 return false;
07cdd027
SC
56}
57
11e82c1b 58void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
07cdd027 59{
11e82c1b
VZ
60 wxDouble left,right,bottom,top;
61 left = wxMax ( src1.m_x , src2.m_x );
62 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
63 top = wxMax ( src1.m_y , src2.m_y );
64 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
65
66 if ( left < right && top < bottom )
67 {
68 dest->m_x = left;
69 dest->m_y = top;
70 dest->m_width = right - left;
71 dest->m_height = bottom - top;
72 }
73 else
74 {
75 dest->m_width = dest->m_height = 0;
76 }
07cdd027
SC
77}
78
11e82c1b 79void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
07cdd027 80{
11e82c1b
VZ
81 wxDouble left,right,bottom,top;
82
83 left = wxMin ( src1.m_x , src2.m_x );
84 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
85 top = wxMin ( src1.m_y , src2.m_y );
86 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
87
88 dest->m_x = left;
89 dest->m_y = top;
90 dest->m_width = right - left;
91 dest->m_height = bottom - top;
07cdd027
SC
92}
93
11e82c1b 94void wxRect2DDouble::Union( const wxPoint2DDouble &pt )
07cdd027 95{
11e82c1b
VZ
96 wxDouble x = pt.m_x;
97 wxDouble y = pt.m_y;
98
99 if ( x < m_x )
100 {
101 SetLeft( x );
102 }
103 else if ( x < m_x + m_width )
104 {
105 // contained
106 }
107 else
108 {
109 SetRight( x );
110 }
111
112 if ( y < m_y )
113 {
114 SetTop( y );
115 }
116 else if ( y < m_y + m_height )
117 {
118 // contained
119 }
120 else
121 {
122 SetBottom( y );
123 }
07cdd027
SC
124}
125
126void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect )
127{
11e82c1b
VZ
128 if ( GetLeft() < rect.GetLeft() )
129 SetLeft( rect.GetLeft() );
130
131 if ( GetRight() > rect.GetRight() )
132 SetRight( rect.GetRight() );
07cdd027 133
11e82c1b
VZ
134 if ( GetBottom() > rect.GetBottom() )
135 SetBottom( rect.GetBottom() );
07cdd027 136
11e82c1b
VZ
137 if ( GetTop() < rect.GetTop() )
138 SetTop( rect.GetTop() );
07cdd027
SC
139}
140
7dc85fe2
VZ
141wxRect2DDouble& wxRect2DDouble::operator=( const wxRect2DDouble &r )
142{
143 m_x = r.m_x;
144 m_y = r.m_y;
145 m_width = r.m_width;
146 m_height = r.m_height;
147 return *this;
148}
149
07cdd027
SC
150// integer version
151
152// for the following calculations always remember
153// that the right and bottom edges are not part of a rect
11e82c1b 154
07cdd027
SC
155// wxPoint2D
156
e30285ab 157#if wxUSE_STREAMS
07cdd027 158void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
11e82c1b
VZ
159{
160 stream.Write32( m_x );
161 stream.Write32( m_y );
07cdd027
SC
162}
163
11e82c1b
VZ
164void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
165{
166 m_x = stream.Read32();
167 m_y = stream.Read32();
07cdd027 168}
e30285ab 169#endif // wxUSE_STREAMS
07cdd027 170
2b5f62a0 171wxDouble wxPoint2DInt::GetVectorAngle() const
07cdd027 172{
11e82c1b
VZ
173 if ( m_x == 0 )
174 {
175 if ( m_y >= 0 )
176 return 90;
177 else
178 return 270;
179 }
180 if ( m_y == 0 )
181 {
182 if ( m_x >= 0 )
183 return 0;
184 else
185 return 180;
186 }
187
ade7e576
VZ
188 // casts needed for MIPSpro compiler under SGI
189 wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / M_PI;
11e82c1b
VZ
190 if ( deg < 0 )
191 {
192 deg += 360;
193 }
194 return deg;
07cdd027
SC
195}
196
11e82c1b 197
ade7e576
VZ
198void wxPoint2DInt::SetVectorAngle( wxDouble degrees )
199{
200 wxDouble length = GetVectorLength();
201 m_x = (int)(length * cos( degrees / 180 * M_PI ));
202 m_y = (int)(length * sin( degrees / 180 * M_PI ));
adb1282e 203}
ade7e576 204
adb1282e
SC
205wxDouble wxPoint2DDouble::GetVectorAngle() const
206{
c77a6796 207 if ( wxIsNullDouble(m_x) )
ade7e576
VZ
208 {
209 if ( m_y >= 0 )
210 return 90;
211 else
212 return 270;
213 }
c77a6796 214 if ( wxIsNullDouble(m_y) )
ade7e576
VZ
215 {
216 if ( m_x >= 0 )
217 return 0;
218 else
219 return 180;
220 }
221 wxDouble deg = atan2( m_y , m_x ) * 180 / M_PI;
222 if ( deg < 0 )
223 {
224 deg += 360;
225 }
226 return deg;
adb1282e
SC
227}
228
ade7e576
VZ
229void wxPoint2DDouble::SetVectorAngle( wxDouble degrees )
230{
231 wxDouble length = GetVectorLength();
232 m_x = length * cos( degrees / 180 * M_PI );
233 m_y = length * sin( degrees / 180 * M_PI );
adb1282e
SC
234}
235
07cdd027 236// wxRect2D
11e82c1b 237
07cdd027
SC
238bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
239{
11e82c1b
VZ
240 wxInt32 left,right,bottom,top;
241 left = wxMax ( m_x , rect.m_x );
242 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
243 top = wxMax ( m_y , rect.m_y );
244 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
245
246 if ( left < right && top < bottom )
247 {
5d3e7b52 248 return true;
11e82c1b 249 }
5d3e7b52 250 return false;
07cdd027
SC
251}
252
11e82c1b 253void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
07cdd027 254{
11e82c1b
VZ
255 wxInt32 left,right,bottom,top;
256 left = wxMax ( src1.m_x , src2.m_x );
257 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
258 top = wxMax ( src1.m_y , src2.m_y );
259 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
260
261 if ( left < right && top < bottom )
262 {
263 dest->m_x = left;
264 dest->m_y = top;
265 dest->m_width = right - left;
266 dest->m_height = bottom - top;
267 }
268 else
269 {
270 dest->m_width = dest->m_height = 0;
271 }
07cdd027
SC
272}
273
11e82c1b 274void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
07cdd027 275{
11e82c1b
VZ
276 wxInt32 left,right,bottom,top;
277
278 left = wxMin ( src1.m_x , src2.m_x );
279 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
280 top = wxMin ( src1.m_y , src2.m_y );
281 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
282
283 dest->m_x = left;
284 dest->m_y = top;
285 dest->m_width = right - left;
286 dest->m_height = bottom - top;
07cdd027
SC
287}
288
11e82c1b 289void wxRect2DInt::Union( const wxPoint2DInt &pt )
07cdd027 290{
11e82c1b
VZ
291 wxInt32 x = pt.m_x;
292 wxInt32 y = pt.m_y;
293
294 if ( x < m_x )
295 {
296 SetLeft( x );
297 }
298 else if ( x < m_x + m_width )
299 {
300 // contained
301 }
302 else
303 {
304 SetRight( x );
305 }
306
307 if ( y < m_y )
308 {
309 SetTop( y );
310 }
311 else if ( y < m_y + m_height )
312 {
313 // contained
314 }
315 else
316 {
317 SetBottom( y );
318 }
07cdd027
SC
319}
320
321void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
322{
11e82c1b
VZ
323 if ( GetLeft() < rect.GetLeft() )
324 SetLeft( rect.GetLeft() );
325
326 if ( GetRight() > rect.GetRight() )
327 SetRight( rect.GetRight() );
07cdd027 328
11e82c1b
VZ
329 if ( GetBottom() > rect.GetBottom() )
330 SetBottom( rect.GetBottom() );
07cdd027 331
11e82c1b
VZ
332 if ( GetTop() < rect.GetTop() )
333 SetTop( rect.GetTop() );
07cdd027
SC
334}
335
336wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
11e82c1b
VZ
337{
338 m_x = r.m_x;
339 m_y = r.m_y;
340 m_width = r.m_width;
341 m_height = r.m_height;
342 return *this;
07cdd027
SC
343}
344
e30285ab 345#if wxUSE_STREAMS
07cdd027 346void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
11e82c1b
VZ
347{
348 stream.Write32( m_x );
349 stream.Write32( m_y );
350 stream.Write32( m_width );
351 stream.Write32( m_height );
07cdd027
SC
352}
353
11e82c1b
VZ
354void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
355{
356 m_x = stream.Read32();
357 m_y = stream.Read32();
358 m_width = stream.Read32();
359 m_height = stream.Read32();
07cdd027 360}
e30285ab 361#endif // wxUSE_STREAMS
07cdd027 362
a68c185d
VS
363
364// wxTransform2D
365
366void wxTransform2D::Transform( wxRect2DInt* r ) const
367{
368 wxPoint2DInt a = r->GetLeftTop(), b = r->GetRightBottom();
369 Transform( &a );
370 Transform( &b );
371 *r = wxRect2DInt( a, b );
372}
373
374wxPoint2DInt wxTransform2D::Transform( const wxPoint2DInt &pt ) const
375{
376 wxPoint2DInt res = pt;
377 Transform( &res );
378 return res;
379}
380
381wxRect2DInt wxTransform2D::Transform( const wxRect2DInt &r ) const
382{
383 wxRect2DInt res = r;
384 Transform( &res );
385 return res;
386}
387
388void wxTransform2D::InverseTransform( wxRect2DInt* r ) const
389{
390 wxPoint2DInt a = r->GetLeftTop(), b = r->GetRightBottom();
391 InverseTransform( &a );
392 InverseTransform( &b );
393 *r = wxRect2DInt( a , b );
394}
395
396wxPoint2DInt wxTransform2D::InverseTransform( const wxPoint2DInt &pt ) const
397{
398 wxPoint2DInt res = pt;
399 InverseTransform( &res );
400 return res;
401}
402
403wxRect2DInt wxTransform2D::InverseTransform( const wxRect2DInt &r ) const
404{
405 wxRect2DInt res = r;
406 InverseTransform( &res );
407 return res;
408}
409
c3a4297c 410#endif // wxUSE_GEOMETRY