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