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