]>
git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/geometry.cpp
3 // Purpose: Common Geometry Classes
4 // Author: Stefan Csomor
8 // Copyright: (c) 1999 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "geometry.cpp"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/geometry.h"
30 #include "wx/datstrm.h"
32 // normally this is defined in <math.h>
34 #define M_PI 3.14159265358979323846
47 // for the following calculations always remember
48 // that the right and bottom edges are not part of a rect
50 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
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
);
58 if ( left
< right
&& top
< bottom
)
65 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
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
);
73 if ( left
< right
&& top
< bottom
)
77 dest
->m_width
= right
- left
;
78 dest
->m_height
= bottom
- top
;
82 dest
->m_width
= dest
->m_height
= 0;
86 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
88 wxDouble left
,right
,bottom
,top
;
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
);
97 dest
->m_width
= right
- left
;
98 dest
->m_height
= bottom
- top
;
101 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
110 else if ( x
< m_x
+ m_width
)
123 else if ( y
< m_y
+ m_height
)
133 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
135 if ( GetLeft() < rect
.GetLeft() )
136 SetLeft( rect
.GetLeft() );
138 if ( GetRight() > rect
.GetRight() )
139 SetRight( rect
.GetRight() );
141 if ( GetBottom() > rect
.GetBottom() )
142 SetBottom( rect
.GetBottom() );
144 if ( GetTop() < rect
.GetTop() )
145 SetTop( rect
.GetTop() );
150 // for the following calculations always remember
151 // that the right and bottom edges are not part of a rect
156 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
158 stream
.Write32( m_x
);
159 stream
.Write32( m_y
);
162 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
164 m_x
= stream
.Read32();
165 m_y
= stream
.Read32();
167 #endif // wxUSE_STREAMS
169 wxDouble
wxPoint2DInt::GetVectorAngle() const
186 // casts needed for MIPSpro compiler under SGI
187 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
196 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
198 wxDouble length
= GetVectorLength();
199 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
200 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
203 wxDouble
wxPoint2DDouble::GetVectorAngle() const
219 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
227 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
229 wxDouble length
= GetVectorLength();
230 m_x
= length
* cos( degrees
/ 180 * M_PI
);
231 m_y
= length
* sin( degrees
/ 180 * M_PI
);
236 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
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
);
244 if ( left
< right
&& top
< bottom
)
251 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
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
);
259 if ( left
< right
&& top
< bottom
)
263 dest
->m_width
= right
- left
;
264 dest
->m_height
= bottom
- top
;
268 dest
->m_width
= dest
->m_height
= 0;
272 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
274 wxInt32 left
,right
,bottom
,top
;
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
);
283 dest
->m_width
= right
- left
;
284 dest
->m_height
= bottom
- top
;
287 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
296 else if ( x
< m_x
+ m_width
)
309 else if ( y
< m_y
+ m_height
)
319 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
321 if ( GetLeft() < rect
.GetLeft() )
322 SetLeft( rect
.GetLeft() );
324 if ( GetRight() > rect
.GetRight() )
325 SetRight( rect
.GetRight() );
327 if ( GetBottom() > rect
.GetBottom() )
328 SetBottom( rect
.GetBottom() );
330 if ( GetTop() < rect
.GetTop() )
331 SetTop( rect
.GetTop() );
334 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
339 m_height
= r
.m_height
;
344 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
346 stream
.Write32( m_x
);
347 stream
.Write32( m_y
);
348 stream
.Write32( m_width
);
349 stream
.Write32( m_height
);
352 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
354 m_x
= stream
.Read32();
355 m_y
= stream
.Read32();
356 m_width
= stream
.Read32();
357 m_height
= stream
.Read32();
359 #endif // wxUSE_STREAMS
361 #endif // wxUSE_GEOMETRY