]>
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"
30 #include "wx/geometry.h"
31 #include "wx/datstrm.h"
43 // for the following calculations always remember
44 // that the right and bottom edges are not part of a rect
46 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
48 wxDouble left
,right
,bottom
,top
;
49 left
= wxMax ( m_x
, rect
.m_x
);
50 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
51 top
= wxMax ( m_y
, rect
.m_y
);
52 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
54 if ( left
< right
&& top
< bottom
)
61 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
63 wxDouble left
,right
,bottom
,top
;
64 left
= wxMax ( src1
.m_x
, src2
.m_x
);
65 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
66 top
= wxMax ( src1
.m_y
, src2
.m_y
);
67 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
69 if ( left
< right
&& top
< bottom
)
73 dest
->m_width
= right
- left
;
74 dest
->m_height
= bottom
- top
;
78 dest
->m_width
= dest
->m_height
= 0;
82 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
84 wxDouble left
,right
,bottom
,top
;
86 left
= wxMin ( src1
.m_x
, src2
.m_x
);
87 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
88 top
= wxMin ( src1
.m_y
, src2
.m_y
);
89 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
93 dest
->m_width
= right
- left
;
94 dest
->m_height
= bottom
- top
;
97 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
106 else if ( x
< m_x
+ m_width
)
119 else if ( y
< m_y
+ m_height
)
129 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
131 if ( GetLeft() < rect
.GetLeft() )
132 SetLeft( rect
.GetLeft() );
134 if ( GetRight() > rect
.GetRight() )
135 SetRight( rect
.GetRight() );
137 if ( GetBottom() > rect
.GetBottom() )
138 SetBottom( rect
.GetBottom() );
140 if ( GetTop() < rect
.GetTop() )
141 SetTop( rect
.GetTop() );
146 // for the following calculations always remember
147 // that the right and bottom edges are not part of a rect
151 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
153 stream
.Write32( m_x
);
154 stream
.Write32( m_y
);
157 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
159 m_x
= stream
.Read32();
160 m_y
= stream
.Read32();
163 wxDouble
wxPoint2DInt::GetVectorAngle()
180 // casts needed MIPSpro compiler under SGI
181 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / 3.14159265359;
190 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
) {
191 wxDouble length
= this->GetVectorLength() ;
192 m_x
= length
* cos( degrees
/ 180 * 3.14159265359 ) ;
193 m_y
= length
* sin( degrees
/ 180 * 3.14159265359 ) ;
196 wxDouble
wxPoint2DDouble::GetVectorAngle() const
212 wxDouble deg
= atan2( m_y
, m_x
) * 180 / 3.14159265359 ;
220 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
) {
221 wxDouble length
= this->GetVectorLength() ;
222 m_x
= length
* cos( degrees
/ 180 * 3.14159265359 ) ;
223 m_y
= length
* sin( degrees
/ 180 * 3.14159265359 ) ;
228 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
230 wxInt32 left
,right
,bottom
,top
;
231 left
= wxMax ( m_x
, rect
.m_x
);
232 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
233 top
= wxMax ( m_y
, rect
.m_y
);
234 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
236 if ( left
< right
&& top
< bottom
)
243 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
245 wxInt32 left
,right
,bottom
,top
;
246 left
= wxMax ( src1
.m_x
, src2
.m_x
);
247 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
248 top
= wxMax ( src1
.m_y
, src2
.m_y
);
249 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
251 if ( left
< right
&& top
< bottom
)
255 dest
->m_width
= right
- left
;
256 dest
->m_height
= bottom
- top
;
260 dest
->m_width
= dest
->m_height
= 0;
264 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
266 wxInt32 left
,right
,bottom
,top
;
268 left
= wxMin ( src1
.m_x
, src2
.m_x
);
269 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
270 top
= wxMin ( src1
.m_y
, src2
.m_y
);
271 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
275 dest
->m_width
= right
- left
;
276 dest
->m_height
= bottom
- top
;
279 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
288 else if ( x
< m_x
+ m_width
)
301 else if ( y
< m_y
+ m_height
)
311 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
313 if ( GetLeft() < rect
.GetLeft() )
314 SetLeft( rect
.GetLeft() );
316 if ( GetRight() > rect
.GetRight() )
317 SetRight( rect
.GetRight() );
319 if ( GetBottom() > rect
.GetBottom() )
320 SetBottom( rect
.GetBottom() );
322 if ( GetTop() < rect
.GetTop() )
323 SetTop( rect
.GetTop() );
326 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
331 m_height
= r
.m_height
;
335 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
337 stream
.Write32( m_x
);
338 stream
.Write32( m_y
);
339 stream
.Write32( m_width
);
340 stream
.Write32( m_height
);
343 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
345 m_x
= stream
.Read32();
346 m_y
= stream
.Read32();
347 m_width
= stream
.Read32();
348 m_height
= stream
.Read32();
351 #endif // wxUSE_GEOMETRY