]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "geometry.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/geometry.h"
30 #include "wx/datstrm.h"
42 // for the following calculations always remember
43 // that the right and bottom edges are not part of a rect
45 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
47 wxDouble left
,right
,bottom
,top
;
48 left
= wxMax ( m_x
, rect
.m_x
);
49 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
50 top
= wxMax ( m_y
, rect
.m_y
);
51 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
53 if ( left
< right
&& top
< bottom
)
60 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
62 wxDouble left
,right
,bottom
,top
;
63 left
= wxMax ( src1
.m_x
, src2
.m_x
);
64 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
65 top
= wxMax ( src1
.m_y
, src2
.m_y
);
66 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
68 if ( left
< right
&& top
< bottom
)
72 dest
->m_width
= right
- left
;
73 dest
->m_height
= bottom
- top
;
77 dest
->m_width
= dest
->m_height
= 0;
81 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
83 wxDouble left
,right
,bottom
,top
;
85 left
= wxMin ( src1
.m_x
, src2
.m_x
);
86 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
87 top
= wxMin ( src1
.m_y
, src2
.m_y
);
88 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
92 dest
->m_width
= right
- left
;
93 dest
->m_height
= bottom
- top
;
96 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
105 else if ( x
< m_x
+ m_width
)
118 else if ( y
< m_y
+ m_height
)
128 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
130 if ( GetLeft() < rect
.GetLeft() )
131 SetLeft( rect
.GetLeft() );
133 if ( GetRight() > rect
.GetRight() )
134 SetRight( rect
.GetRight() );
136 if ( GetBottom() > rect
.GetBottom() )
137 SetBottom( rect
.GetBottom() );
139 if ( GetTop() < rect
.GetTop() )
140 SetTop( rect
.GetTop() );
143 wxRect2DDouble
& wxRect2DDouble::operator=( const wxRect2DDouble
&r
)
148 m_height
= r
.m_height
;
154 // for the following calculations always remember
155 // that the right and bottom edges are not part of a rect
160 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
162 stream
.Write32( m_x
);
163 stream
.Write32( m_y
);
166 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
168 m_x
= stream
.Read32();
169 m_y
= stream
.Read32();
171 #endif // wxUSE_STREAMS
173 wxDouble
wxPoint2DInt::GetVectorAngle() const
190 // casts needed for MIPSpro compiler under SGI
191 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
200 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
202 wxDouble length
= GetVectorLength();
203 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
204 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
207 wxDouble
wxPoint2DDouble::GetVectorAngle() const
223 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
231 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
233 wxDouble length
= GetVectorLength();
234 m_x
= length
* cos( degrees
/ 180 * M_PI
);
235 m_y
= length
* sin( degrees
/ 180 * M_PI
);
240 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
242 wxInt32 left
,right
,bottom
,top
;
243 left
= wxMax ( m_x
, rect
.m_x
);
244 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
245 top
= wxMax ( m_y
, rect
.m_y
);
246 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
248 if ( left
< right
&& top
< bottom
)
255 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
257 wxInt32 left
,right
,bottom
,top
;
258 left
= wxMax ( src1
.m_x
, src2
.m_x
);
259 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
260 top
= wxMax ( src1
.m_y
, src2
.m_y
);
261 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
263 if ( left
< right
&& top
< bottom
)
267 dest
->m_width
= right
- left
;
268 dest
->m_height
= bottom
- top
;
272 dest
->m_width
= dest
->m_height
= 0;
276 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
278 wxInt32 left
,right
,bottom
,top
;
280 left
= wxMin ( src1
.m_x
, src2
.m_x
);
281 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
282 top
= wxMin ( src1
.m_y
, src2
.m_y
);
283 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
287 dest
->m_width
= right
- left
;
288 dest
->m_height
= bottom
- top
;
291 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
300 else if ( x
< m_x
+ m_width
)
313 else if ( y
< m_y
+ m_height
)
323 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
325 if ( GetLeft() < rect
.GetLeft() )
326 SetLeft( rect
.GetLeft() );
328 if ( GetRight() > rect
.GetRight() )
329 SetRight( rect
.GetRight() );
331 if ( GetBottom() > rect
.GetBottom() )
332 SetBottom( rect
.GetBottom() );
334 if ( GetTop() < rect
.GetTop() )
335 SetTop( rect
.GetTop() );
338 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
343 m_height
= r
.m_height
;
348 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
350 stream
.Write32( m_x
);
351 stream
.Write32( m_y
);
352 stream
.Write32( m_width
);
353 stream
.Write32( m_height
);
356 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
358 m_x
= stream
.Read32();
359 m_y
= stream
.Read32();
360 m_width
= stream
.Read32();
361 m_height
= stream
.Read32();
363 #endif // wxUSE_STREAMS
365 #endif // wxUSE_GEOMETRY