]>
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
155 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
157 stream
.Write32( m_x
);
158 stream
.Write32( m_y
);
161 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
163 m_x
= stream
.Read32();
164 m_y
= stream
.Read32();
167 wxDouble
wxPoint2DInt::GetVectorAngle() const
184 // casts needed for MIPSpro compiler under SGI
185 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
194 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
196 wxDouble length
= GetVectorLength();
197 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
198 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
201 wxDouble
wxPoint2DDouble::GetVectorAngle() const
217 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
225 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
227 wxDouble length
= GetVectorLength();
228 m_x
= length
* cos( degrees
/ 180 * M_PI
);
229 m_y
= length
* sin( degrees
/ 180 * M_PI
);
234 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
236 wxInt32 left
,right
,bottom
,top
;
237 left
= wxMax ( m_x
, rect
.m_x
);
238 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
239 top
= wxMax ( m_y
, rect
.m_y
);
240 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
242 if ( left
< right
&& top
< bottom
)
249 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
251 wxInt32 left
,right
,bottom
,top
;
252 left
= wxMax ( src1
.m_x
, src2
.m_x
);
253 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
254 top
= wxMax ( src1
.m_y
, src2
.m_y
);
255 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
257 if ( left
< right
&& top
< bottom
)
261 dest
->m_width
= right
- left
;
262 dest
->m_height
= bottom
- top
;
266 dest
->m_width
= dest
->m_height
= 0;
270 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
272 wxInt32 left
,right
,bottom
,top
;
274 left
= wxMin ( src1
.m_x
, src2
.m_x
);
275 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
276 top
= wxMin ( src1
.m_y
, src2
.m_y
);
277 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
281 dest
->m_width
= right
- left
;
282 dest
->m_height
= bottom
- top
;
285 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
294 else if ( x
< m_x
+ m_width
)
307 else if ( y
< m_y
+ m_height
)
317 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
319 if ( GetLeft() < rect
.GetLeft() )
320 SetLeft( rect
.GetLeft() );
322 if ( GetRight() > rect
.GetRight() )
323 SetRight( rect
.GetRight() );
325 if ( GetBottom() > rect
.GetBottom() )
326 SetBottom( rect
.GetBottom() );
328 if ( GetTop() < rect
.GetTop() )
329 SetTop( rect
.GetTop() );
332 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
337 m_height
= r
.m_height
;
341 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
343 stream
.Write32( m_x
);
344 stream
.Write32( m_y
);
345 stream
.Write32( m_width
);
346 stream
.Write32( m_height
);
349 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
351 m_x
= stream
.Read32();
352 m_y
= stream
.Read32();
353 m_width
= stream
.Read32();
354 m_height
= stream
.Read32();
357 #endif // wxUSE_GEOMETRY