]>
git.saurik.com Git - wxWidgets.git/blob - src/common/geometry.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/geometry.cpp
3 // Purpose: Common Geometry Classes
4 // Author: Stefan Csomor
7 // Copyright: (c) 1999 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/geometry.h"
28 #include "wx/datstrm.h"
40 // for the following calculations always remember
41 // that the right and bottom edges are not part of a rect
43 bool wxRect2DDouble::Intersects( const wxRect2DDouble
&rect
) const
45 wxDouble left
,right
,bottom
,top
;
46 left
= wxMax ( m_x
, rect
.m_x
);
47 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
48 top
= wxMax ( m_y
, rect
.m_y
);
49 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
51 if ( left
< right
&& top
< bottom
)
58 void wxRect2DDouble::Intersect( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
60 wxDouble left
,right
,bottom
,top
;
61 left
= wxMax ( src1
.m_x
, src2
.m_x
);
62 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
63 top
= wxMax ( src1
.m_y
, src2
.m_y
);
64 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
66 if ( left
< right
&& top
< bottom
)
70 dest
->m_width
= right
- left
;
71 dest
->m_height
= bottom
- top
;
75 dest
->m_width
= dest
->m_height
= 0;
79 void wxRect2DDouble::Union( const wxRect2DDouble
&src1
, const wxRect2DDouble
&src2
, wxRect2DDouble
*dest
)
81 wxDouble left
,right
,bottom
,top
;
83 left
= wxMin ( src1
.m_x
, src2
.m_x
);
84 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
85 top
= wxMin ( src1
.m_y
, src2
.m_y
);
86 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
90 dest
->m_width
= right
- left
;
91 dest
->m_height
= bottom
- top
;
94 void wxRect2DDouble::Union( const wxPoint2DDouble
&pt
)
103 else if ( x
< m_x
+ m_width
)
116 else if ( y
< m_y
+ m_height
)
126 void wxRect2DDouble::ConstrainTo( const wxRect2DDouble
&rect
)
128 if ( GetLeft() < rect
.GetLeft() )
129 SetLeft( rect
.GetLeft() );
131 if ( GetRight() > rect
.GetRight() )
132 SetRight( rect
.GetRight() );
134 if ( GetBottom() > rect
.GetBottom() )
135 SetBottom( rect
.GetBottom() );
137 if ( GetTop() < rect
.GetTop() )
138 SetTop( rect
.GetTop() );
141 wxRect2DDouble
& wxRect2DDouble::operator=( const wxRect2DDouble
&r
)
146 m_height
= r
.m_height
;
152 // for the following calculations always remember
153 // that the right and bottom edges are not part of a rect
158 void wxPoint2DInt::WriteTo( wxDataOutputStream
&stream
) const
160 stream
.Write32( m_x
);
161 stream
.Write32( m_y
);
164 void wxPoint2DInt::ReadFrom( wxDataInputStream
&stream
)
166 m_x
= stream
.Read32();
167 m_y
= stream
.Read32();
169 #endif // wxUSE_STREAMS
171 wxDouble
wxPoint2DInt::GetVectorAngle() const
188 // casts needed for MIPSpro compiler under SGI
189 wxDouble deg
= atan2( (double)m_y
, (double)m_x
) * 180 / M_PI
;
198 void wxPoint2DInt::SetVectorAngle( wxDouble degrees
)
200 wxDouble length
= GetVectorLength();
201 m_x
= (int)(length
* cos( degrees
/ 180 * M_PI
));
202 m_y
= (int)(length
* sin( degrees
/ 180 * M_PI
));
205 wxDouble
wxPoint2DDouble::GetVectorAngle() const
207 if ( wxIsNullDouble(m_x
) )
214 if ( wxIsNullDouble(m_y
) )
221 wxDouble deg
= atan2( m_y
, m_x
) * 180 / M_PI
;
229 void wxPoint2DDouble::SetVectorAngle( wxDouble degrees
)
231 wxDouble length
= GetVectorLength();
232 m_x
= length
* cos( degrees
/ 180 * M_PI
);
233 m_y
= length
* sin( degrees
/ 180 * M_PI
);
238 bool wxRect2DInt::Intersects( const wxRect2DInt
&rect
) const
240 wxInt32 left
,right
,bottom
,top
;
241 left
= wxMax ( m_x
, rect
.m_x
);
242 right
= wxMin ( m_x
+m_width
, rect
.m_x
+ rect
.m_width
);
243 top
= wxMax ( m_y
, rect
.m_y
);
244 bottom
= wxMin ( m_y
+m_height
, rect
.m_y
+ rect
.m_height
);
246 if ( left
< right
&& top
< bottom
)
253 void wxRect2DInt::Intersect( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
255 wxInt32 left
,right
,bottom
,top
;
256 left
= wxMax ( src1
.m_x
, src2
.m_x
);
257 right
= wxMin ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
258 top
= wxMax ( src1
.m_y
, src2
.m_y
);
259 bottom
= wxMin ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
261 if ( left
< right
&& top
< bottom
)
265 dest
->m_width
= right
- left
;
266 dest
->m_height
= bottom
- top
;
270 dest
->m_width
= dest
->m_height
= 0;
274 void wxRect2DInt::Union( const wxRect2DInt
&src1
, const wxRect2DInt
&src2
, wxRect2DInt
*dest
)
276 wxInt32 left
,right
,bottom
,top
;
278 left
= wxMin ( src1
.m_x
, src2
.m_x
);
279 right
= wxMax ( src1
.m_x
+src1
.m_width
, src2
.m_x
+ src2
.m_width
);
280 top
= wxMin ( src1
.m_y
, src2
.m_y
);
281 bottom
= wxMax ( src1
.m_y
+src1
.m_height
, src2
.m_y
+ src2
.m_height
);
285 dest
->m_width
= right
- left
;
286 dest
->m_height
= bottom
- top
;
289 void wxRect2DInt::Union( const wxPoint2DInt
&pt
)
298 else if ( x
< m_x
+ m_width
)
311 else if ( y
< m_y
+ m_height
)
321 void wxRect2DInt::ConstrainTo( const wxRect2DInt
&rect
)
323 if ( GetLeft() < rect
.GetLeft() )
324 SetLeft( rect
.GetLeft() );
326 if ( GetRight() > rect
.GetRight() )
327 SetRight( rect
.GetRight() );
329 if ( GetBottom() > rect
.GetBottom() )
330 SetBottom( rect
.GetBottom() );
332 if ( GetTop() < rect
.GetTop() )
333 SetTop( rect
.GetTop() );
336 wxRect2DInt
& wxRect2DInt::operator=( const wxRect2DInt
&r
)
341 m_height
= r
.m_height
;
346 void wxRect2DInt::WriteTo( wxDataOutputStream
&stream
) const
348 stream
.Write32( m_x
);
349 stream
.Write32( m_y
);
350 stream
.Write32( m_width
);
351 stream
.Write32( m_height
);
354 void wxRect2DInt::ReadFrom( wxDataInputStream
&stream
)
356 m_x
= stream
.Read32();
357 m_y
= stream
.Read32();
358 m_width
= stream
.Read32();
359 m_height
= stream
.Read32();
361 #endif // wxUSE_STREAMS
366 void wxTransform2D::Transform( wxRect2DInt
* r
) const
368 wxPoint2DInt a
= r
->GetLeftTop(), b
= r
->GetRightBottom();
371 *r
= wxRect2DInt( a
, b
);
374 wxPoint2DInt
wxTransform2D::Transform( const wxPoint2DInt
&pt
) const
376 wxPoint2DInt res
= pt
;
381 wxRect2DInt
wxTransform2D::Transform( const wxRect2DInt
&r
) const
388 void wxTransform2D::InverseTransform( wxRect2DInt
* r
) const
390 wxPoint2DInt a
= r
->GetLeftTop(), b
= r
->GetRightBottom();
391 InverseTransform( &a
);
392 InverseTransform( &b
);
393 *r
= wxRect2DInt( a
, b
);
396 wxPoint2DInt
wxTransform2D::InverseTransform( const wxPoint2DInt
&pt
) const
398 wxPoint2DInt res
= pt
;
399 InverseTransform( &res
);
403 wxRect2DInt
wxTransform2D::InverseTransform( const wxRect2DInt
&r
) const
406 InverseTransform( &res
);
410 #endif // wxUSE_GEOMETRY