]>
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 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/geometry.h"
26 #include "wx/datstrm.h"
38 // for the following calculations always remember
39 // that the right and bottom edges are not part of a rect
41 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
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
);
49 if ( left
< right
&& top
< bottom
)
56 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
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
);
64 if ( left
< right
&& top
< bottom
)
68 dest
->m_width
= right
- left
;
69 dest
->m_height
= bottom
- top
;
73 dest
->m_width
= dest
->m_height
= 0;
77 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
79 wxDouble left
,right
,bottom
,top
;
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
);
88 dest
->m_width
= right
- left
;
89 dest
->m_height
= bottom
- top
;
92 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
101 else if ( x
< m_x
+ m_width
)
114 else if ( y
< m_y
+ m_height
)
124 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
126 if ( GetLeft() < rect
.GetLeft() )
127 SetLeft( rect
.GetLeft() );
129 if ( GetRight() > rect
.GetRight() )
130 SetRight( rect
.GetRight() );
132 if ( GetBottom() > rect
.GetBottom() )
133 SetBottom( rect
.GetBottom() );
135 if ( GetTop() < rect
.GetTop() )
136 SetTop( rect
.GetTop() );
139 wxRect2DDouble
& wxRect2DDouble::operator=( const wxRect2DDouble
&r
)
144 m_height
= r
.m_height
;
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