]> git.saurik.com Git - wxWidgets.git/blame - src/common/geometry.cpp
Applied patch [ 1183157 ] Compilation with GCC 4.0
[wxWidgets.git] / src / common / geometry.cpp
CommitLineData
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
65571936 9// Licence: wxWindows licence
07cdd027
SC
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
d0430ee2 13 #pragma implementation "geometry.h"
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
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
11e82c1b 44
07cdd027
SC
45bool wxRect2DDouble::Intersects( const wxRect2DDouble &rect ) const
46{
11e82c1b
VZ
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 {
5d3e7b52 55 return true;
11e82c1b 56 }
5d3e7b52 57 return false;
07cdd027
SC
58}
59
11e82c1b 60void wxRect2DDouble::Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
07cdd027 61{
11e82c1b
VZ
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 }
07cdd027
SC
79}
80
11e82c1b 81void wxRect2DDouble::Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest )
07cdd027 82{
11e82c1b
VZ
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;
07cdd027
SC
94}
95
11e82c1b 96void wxRect2DDouble::Union( const wxPoint2DDouble &pt )
07cdd027 97{
11e82c1b
VZ
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 }
07cdd027
SC
126}
127
128void wxRect2DDouble::ConstrainTo( const wxRect2DDouble &rect )
129{
11e82c1b
VZ
130 if ( GetLeft() < rect.GetLeft() )
131 SetLeft( rect.GetLeft() );
132
133 if ( GetRight() > rect.GetRight() )
134 SetRight( rect.GetRight() );
07cdd027 135
11e82c1b
VZ
136 if ( GetBottom() > rect.GetBottom() )
137 SetBottom( rect.GetBottom() );
07cdd027 138
11e82c1b
VZ
139 if ( GetTop() < rect.GetTop() )
140 SetTop( rect.GetTop() );
07cdd027
SC
141}
142
7dc85fe2
VZ
143wxRect2DDouble& wxRect2DDouble::operator=( const wxRect2DDouble &r )
144{
145 m_x = r.m_x;
146 m_y = r.m_y;
147 m_width = r.m_width;
148 m_height = r.m_height;
149 return *this;
150}
151
07cdd027
SC
152// integer version
153
154// for the following calculations always remember
155// that the right and bottom edges are not part of a rect
11e82c1b 156
07cdd027
SC
157// wxPoint2D
158
e30285ab 159#if wxUSE_STREAMS
07cdd027 160void wxPoint2DInt::WriteTo( wxDataOutputStream &stream ) const
11e82c1b
VZ
161{
162 stream.Write32( m_x );
163 stream.Write32( m_y );
07cdd027
SC
164}
165
11e82c1b
VZ
166void wxPoint2DInt::ReadFrom( wxDataInputStream &stream )
167{
168 m_x = stream.Read32();
169 m_y = stream.Read32();
07cdd027 170}
e30285ab 171#endif // wxUSE_STREAMS
07cdd027 172
2b5f62a0 173wxDouble wxPoint2DInt::GetVectorAngle() const
07cdd027 174{
11e82c1b
VZ
175 if ( m_x == 0 )
176 {
177 if ( m_y >= 0 )
178 return 90;
179 else
180 return 270;
181 }
182 if ( m_y == 0 )
183 {
184 if ( m_x >= 0 )
185 return 0;
186 else
187 return 180;
188 }
189
ade7e576
VZ
190 // casts needed for MIPSpro compiler under SGI
191 wxDouble deg = atan2( (double)m_y , (double)m_x ) * 180 / M_PI;
11e82c1b
VZ
192 if ( deg < 0 )
193 {
194 deg += 360;
195 }
196 return deg;
07cdd027
SC
197}
198
11e82c1b 199
ade7e576
VZ
200void wxPoint2DInt::SetVectorAngle( wxDouble degrees )
201{
202 wxDouble length = GetVectorLength();
203 m_x = (int)(length * cos( degrees / 180 * M_PI ));
204 m_y = (int)(length * sin( degrees / 180 * M_PI ));
adb1282e 205}
ade7e576 206
adb1282e
SC
207wxDouble wxPoint2DDouble::GetVectorAngle() const
208{
ade7e576
VZ
209 if ( m_x == 0 )
210 {
211 if ( m_y >= 0 )
212 return 90;
213 else
214 return 270;
215 }
216 if ( m_y == 0 )
217 {
218 if ( m_x >= 0 )
219 return 0;
220 else
221 return 180;
222 }
223 wxDouble deg = atan2( m_y , m_x ) * 180 / M_PI;
224 if ( deg < 0 )
225 {
226 deg += 360;
227 }
228 return deg;
adb1282e
SC
229}
230
ade7e576
VZ
231void wxPoint2DDouble::SetVectorAngle( wxDouble degrees )
232{
233 wxDouble length = GetVectorLength();
234 m_x = length * cos( degrees / 180 * M_PI );
235 m_y = length * sin( degrees / 180 * M_PI );
adb1282e
SC
236}
237
07cdd027 238// wxRect2D
11e82c1b 239
07cdd027
SC
240bool wxRect2DInt::Intersects( const wxRect2DInt &rect ) const
241{
11e82c1b
VZ
242 wxInt32 left,right,bottom,top;
243 left = wxMax ( m_x , rect.m_x );
244 right = wxMin ( m_x+m_width, rect.m_x + rect.m_width );
245 top = wxMax ( m_y , rect.m_y );
246 bottom = wxMin ( m_y+m_height, rect.m_y + rect.m_height );
247
248 if ( left < right && top < bottom )
249 {
5d3e7b52 250 return true;
11e82c1b 251 }
5d3e7b52 252 return false;
07cdd027
SC
253}
254
11e82c1b 255void wxRect2DInt::Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
07cdd027 256{
11e82c1b
VZ
257 wxInt32 left,right,bottom,top;
258 left = wxMax ( src1.m_x , src2.m_x );
259 right = wxMin ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
260 top = wxMax ( src1.m_y , src2.m_y );
261 bottom = wxMin ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
262
263 if ( left < right && top < bottom )
264 {
265 dest->m_x = left;
266 dest->m_y = top;
267 dest->m_width = right - left;
268 dest->m_height = bottom - top;
269 }
270 else
271 {
272 dest->m_width = dest->m_height = 0;
273 }
07cdd027
SC
274}
275
11e82c1b 276void wxRect2DInt::Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest )
07cdd027 277{
11e82c1b
VZ
278 wxInt32 left,right,bottom,top;
279
280 left = wxMin ( src1.m_x , src2.m_x );
281 right = wxMax ( src1.m_x+src1.m_width, src2.m_x + src2.m_width );
282 top = wxMin ( src1.m_y , src2.m_y );
283 bottom = wxMax ( src1.m_y+src1.m_height, src2.m_y + src2.m_height );
284
285 dest->m_x = left;
286 dest->m_y = top;
287 dest->m_width = right - left;
288 dest->m_height = bottom - top;
07cdd027
SC
289}
290
11e82c1b 291void wxRect2DInt::Union( const wxPoint2DInt &pt )
07cdd027 292{
11e82c1b
VZ
293 wxInt32 x = pt.m_x;
294 wxInt32 y = pt.m_y;
295
296 if ( x < m_x )
297 {
298 SetLeft( x );
299 }
300 else if ( x < m_x + m_width )
301 {
302 // contained
303 }
304 else
305 {
306 SetRight( x );
307 }
308
309 if ( y < m_y )
310 {
311 SetTop( y );
312 }
313 else if ( y < m_y + m_height )
314 {
315 // contained
316 }
317 else
318 {
319 SetBottom( y );
320 }
07cdd027
SC
321}
322
323void wxRect2DInt::ConstrainTo( const wxRect2DInt &rect )
324{
11e82c1b
VZ
325 if ( GetLeft() < rect.GetLeft() )
326 SetLeft( rect.GetLeft() );
327
328 if ( GetRight() > rect.GetRight() )
329 SetRight( rect.GetRight() );
07cdd027 330
11e82c1b
VZ
331 if ( GetBottom() > rect.GetBottom() )
332 SetBottom( rect.GetBottom() );
07cdd027 333
11e82c1b
VZ
334 if ( GetTop() < rect.GetTop() )
335 SetTop( rect.GetTop() );
07cdd027
SC
336}
337
338wxRect2DInt& wxRect2DInt::operator=( const wxRect2DInt &r )
11e82c1b
VZ
339{
340 m_x = r.m_x;
341 m_y = r.m_y;
342 m_width = r.m_width;
343 m_height = r.m_height;
344 return *this;
07cdd027
SC
345}
346
e30285ab 347#if wxUSE_STREAMS
07cdd027 348void wxRect2DInt::WriteTo( wxDataOutputStream &stream ) const
11e82c1b
VZ
349{
350 stream.Write32( m_x );
351 stream.Write32( m_y );
352 stream.Write32( m_width );
353 stream.Write32( m_height );
07cdd027
SC
354}
355
11e82c1b
VZ
356void wxRect2DInt::ReadFrom( wxDataInputStream &stream )
357{
358 m_x = stream.Read32();
359 m_y = stream.Read32();
360 m_width = stream.Read32();
361 m_height = stream.Read32();
07cdd027 362}
e30285ab 363#endif // wxUSE_STREAMS
07cdd027 364
c3a4297c 365#endif // wxUSE_GEOMETRY