]>
git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
c2825b43592dad9d15b946edd5c61b7c9c70faa8
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
21 #include "wx/geometry.h"
29 #include "wx/datstrm.h"
41 // for the following calculations always remember
42 // that the right and bottom edges are not part of a rect
44 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
46 wxDouble left
,right
,bottom
,top
;
47 left
= wxMax ( m_x
, rect
.m_x
);
48 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
49 top
= wxMax ( m_y
, rect
.m_y
);
50 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
52 if ( left
< right
&& top
< bottom
)
59 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
61 wxDouble left
,right
,bottom
,top
;
62 left
= wxMax ( src1
.m_x
, src2
.m_x
);
63 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
64 top
= wxMax ( src1
.m_y
, src2
.m_y
);
65 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
67 if ( left
< right
&& top
< bottom
)
71 dest
->m_width
= right
- left
;
72 dest
->m_height
= bottom
- top
;
76 dest
->m_width
= dest
->m_height
= 0;
80 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
82 wxDouble left
,right
,bottom
,top
;
84 left
= wxMin ( src1
.m_x
, src2
.m_x
);
85 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
86 top
= wxMin ( src1
.m_y
, src2
.m_y
);
87 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
91 dest
->m_width
= right
- left
;
92 dest
->m_height
= bottom
- top
;
95 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
104 else if ( x
< m_x
+ m_width
)
117 else if ( y
< m_y
+ m_height
)
127 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
129 if ( GetLeft() < rect
.GetLeft() )
130 SetLeft( rect
.GetLeft() );
132 if ( GetRight() > rect
.GetRight() )
133 SetRight( rect
.GetRight() );
135 if ( GetBottom() > rect
.GetBottom() )
136 SetBottom( rect
.GetBottom() );
138 if ( GetTop() < rect
.GetTop() )
139 SetTop( rect
.GetTop() );
142 wxRect2DDouble
& wxRect2DDouble::operator=( const wxRect2DDouble
&r
)
147 m_height
= r
.m_height
;
153 // for the following calculations always remember
154 // that the right and bottom edges are not part of a rect
159 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
161 stream
.Write32( m_x
);
162 stream
.Write32( m_y
);
165 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
167 m_x
= stream
.Read32();
168 m_y
= stream
.Read32();
170 #endif // wxUSE_STREAMS
172 wxDouble
wxPoint2DInt::GetVectorAngle() const
189 // casts needed for MIPSpro compiler under SGI
190 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
199 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
201 wxDouble length
= GetVectorLength();
202 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
203 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
206 wxDouble
wxPoint2DDouble::GetVectorAngle() const
208 if ( wxIsNullDouble(m_x
) )
215 if ( wxIsNullDouble(m_y
) )
222 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
230 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
232 wxDouble length
= GetVectorLength();
233 m_x
= length
* cos( degrees
/ 180 * M_PI
);
234 m_y
= length
* sin( degrees
/ 180 * M_PI
);
239 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
241 wxInt32 left
,right
,bottom
,top
;
242 left
= wxMax ( m_x
, rect
.m_x
);
243 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
244 top
= wxMax ( m_y
, rect
.m_y
);
245 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
247 if ( left
< right
&& top
< bottom
)
254 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
256 wxInt32 left
,right
,bottom
,top
;
257 left
= wxMax ( src1
.m_x
, src2
.m_x
);
258 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
259 top
= wxMax ( src1
.m_y
, src2
.m_y
);
260 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
262 if ( left
< right
&& top
< bottom
)
266 dest
->m_width
= right
- left
;
267 dest
->m_height
= bottom
- top
;
271 dest
->m_width
= dest
->m_height
= 0;
275 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
277 wxInt32 left
,right
,bottom
,top
;
279 left
= wxMin ( src1
.m_x
, src2
.m_x
);
280 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
281 top
= wxMin ( src1
.m_y
, src2
.m_y
);
282 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
286 dest
->m_width
= right
- left
;
287 dest
->m_height
= bottom
- top
;
290 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
299 else if ( x
< m_x
+ m_width
)
312 else if ( y
< m_y
+ m_height
)
322 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
324 if ( GetLeft() < rect
.GetLeft() )
325 SetLeft( rect
.GetLeft() );
327 if ( GetRight() > rect
.GetRight() )
328 SetRight( rect
.GetRight() );
330 if ( GetBottom() > rect
.GetBottom() )
331 SetBottom( rect
.GetBottom() );
333 if ( GetTop() < rect
.GetTop() )
334 SetTop( rect
.GetTop() );
337 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
342 m_height
= r
.m_height
;
347 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
349 stream
.Write32( m_x
);
350 stream
.Write32( m_y
);
351 stream
.Write32( m_width
);
352 stream
.Write32( m_height
);
355 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
357 m_x
= stream
.Read32();
358 m_y
= stream
.Read32();
359 m_width
= stream
.Read32();
360 m_height
= stream
.Read32();
362 #endif // wxUSE_STREAMS
367 void wxTransform2D::Transform( wxRect2DInt
* r
) const
369 wxPoint2DInt a
= r
->GetLeftTop(), b
= r
->GetRightBottom();
372 *r
= wxRect2DInt( a
, b
);
375 wxPoint2DInt
wxTransform2D::Transform( const wxPoint2DInt
&pt
) const
377 wxPoint2DInt res
= pt
;
382 wxRect2DInt
wxTransform2D::Transform( const wxRect2DInt
&r
) const
389 void wxTransform2D::InverseTransform( wxRect2DInt
* r
) const
391 wxPoint2DInt a
= r
->GetLeftTop(), b
= r
->GetRightBottom();
392 InverseTransform( &a
);
393 InverseTransform( &b
);
394 *r
= wxRect2DInt( a
, b
);
397 wxPoint2DInt
wxTransform2D::InverseTransform( const wxPoint2DInt
&pt
) const
399 wxPoint2DInt res
= pt
;
400 InverseTransform( &res
);
404 wxRect2DInt
wxTransform2D::InverseTransform( const wxRect2DInt
&r
) const
407 InverseTransform( &res
);
411 #endif // wxUSE_GEOMETRY