]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/canvas/bbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Klaas Holwerda
5 // Copyright: 2000 (c) Klaas Holwerda
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "bbox.cpp"
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/canvas/bbox.h"
22 wxBoundingBox::wxBoundingBox()
24 m_minx
= m_miny
= m_maxx
= m_maxy
= 0.0;
29 wxBoundingBox::wxBoundingBox(wxBoundingBox
&other
)
31 m_minx
= other
.m_minx
;
32 m_miny
= other
.m_miny
;
33 m_maxx
= other
.m_maxx
;
34 m_maxy
= other
.m_maxy
;
35 m_validbbox
= other
.m_validbbox
;
39 wxBoundingBox::wxBoundingBox(const wxPoint2DDouble
& a
)
48 wxBoundingBox::wxBoundingBox(double xmin
, double ymin
, double xmax
, double ymax
)
57 // This function checks if two bboxes intersect
58 bool wxBoundingBox::And(wxBoundingBox
*_bbox
, double Marge
)
60 assert (m_validbbox
== TRUE
);
61 assert (_bbox
->GetValid());
62 m_minx
= wxMax(m_minx
, _bbox
->m_minx
);
63 m_maxx
= wxMin(m_maxx
, _bbox
->m_maxx
);
64 m_miny
= wxMax(m_miny
, _bbox
->m_miny
);
65 m_maxy
= wxMin(m_maxy
, _bbox
->m_maxy
);
68 ((m_minx
- Marge
) < (m_maxx
+ Marge
)) &&
69 ((m_miny
- Marge
) < (m_maxy
+ Marge
))
73 // Shrink the boundingbox with the given marge
74 void wxBoundingBox::Shrink(const double Marge
)
76 assert (m_validbbox
== TRUE
);
85 // Expand the boundingbox with another boundingbox
86 void wxBoundingBox::Expand(const wxBoundingBox
&other
)
94 m_minx
= wxMin(m_minx
, other
.m_minx
);
95 m_maxx
= wxMax(m_maxx
, other
.m_maxx
);
96 m_miny
= wxMin(m_miny
, other
.m_miny
);
97 m_maxy
= wxMax(m_maxy
, other
.m_maxy
);
102 // Expand the boundingbox with a point
103 void wxBoundingBox::Expand(const wxPoint2DDouble
& a_point
)
107 m_minx
= m_maxx
= a_point
.m_x
;
108 m_miny
= m_maxy
= a_point
.m_y
;
113 m_minx
= wxMin(m_minx
, a_point
.m_x
);
114 m_maxx
= wxMax(m_maxx
, a_point
.m_x
);
115 m_miny
= wxMin(m_miny
, a_point
.m_y
);
116 m_maxy
= wxMax(m_maxy
, a_point
.m_y
);
120 // Expand the boundingbox with a point
121 void wxBoundingBox::Expand(double x
,double y
)
131 m_minx
= wxMin(m_minx
, x
);
132 m_maxx
= wxMax(m_maxx
, x
);
133 m_miny
= wxMin(m_miny
, y
);
134 m_maxy
= wxMax(m_maxy
, y
);
139 // Expand the boundingbox with two points
140 void wxBoundingBox::Expand(const wxPoint2DDouble
& a
, const wxPoint2DDouble
& b
)
146 // Enlarge the boundingbox with the given marge
147 void wxBoundingBox::EnLarge(const double marge
)
151 m_minx
= m_maxx
= marge
;
152 m_miny
= m_maxy
= marge
;
164 // Calculates if two boundingboxes intersect. If so, the function returns _ON.
165 // If they do not intersect, two scenario's are possible:
166 // other is outside this -> return _OUT
167 // other is inside this -> return _IN
168 OVERLAP
wxBoundingBox::Intersect(wxBoundingBox
&other
, double Marge
)
170 assert (m_validbbox
== TRUE
);
172 // other boundingbox must exist
175 if (((m_minx
- Marge
) > (other
.m_maxx
+ Marge
)) ||
176 ((m_maxx
+ Marge
) < (other
.m_minx
- Marge
)) ||
177 ((m_maxy
+ Marge
) < (other
.m_miny
- Marge
)) ||
178 ((m_miny
- Marge
) > (other
.m_maxy
+ Marge
)))
181 // Check if other.bbox is inside this bbox
182 if ((m_minx
<= other
.m_minx
) &&
183 (m_maxx
>= other
.m_maxx
) &&
184 (m_maxy
>= other
.m_maxy
) &&
185 (m_miny
<= other
.m_miny
))
188 // Boundingboxes intersect
193 // Checks if a line intersects the boundingbox
194 bool wxBoundingBox::LineIntersect(const wxPoint2DDouble
& begin
, const wxPoint2DDouble
& end
)
196 assert (m_validbbox
== TRUE
);
199 !(((begin
.m_y
> m_maxy
) && (end
.m_y
> m_maxy
)) ||
200 ((begin
.m_y
< m_miny
) && (end
.m_y
< m_miny
)) ||
201 ((begin
.m_x
> m_maxx
) && (end
.m_x
> m_maxx
)) ||
202 ((begin
.m_x
< m_minx
) && (end
.m_x
< m_minx
)));
206 // Is the given point in the boundingbox ??
207 bool wxBoundingBox::PointInBox(double x
, double y
, double Marge
)
209 assert (m_validbbox
== TRUE
);
211 if ( x
>= (m_minx
- Marge
) && x
<= (m_maxx
+ Marge
) &&
212 y
>= (m_miny
- Marge
) && y
<= (m_maxy
+ Marge
) )
219 // Is the given point in the boundingbox ??
221 bool wxBoundingBox::PointInBox(const wxPoint2DDouble
& a
, double Marge
)
223 assert (m_validbbox
== TRUE
);
225 return PointInBox(a
.m_x
, a
.m_y
, Marge
);
229 wxPoint2DDouble
wxBoundingBox::GetMin()
231 assert (m_validbbox
== TRUE
);
233 return wxPoint2DDouble(m_minx
, m_miny
);
237 wxPoint2DDouble
wxBoundingBox::GetMax()
239 assert (m_validbbox
== TRUE
);
241 return wxPoint2DDouble(m_maxx
, m_maxy
);
244 bool wxBoundingBox::GetValid() const
249 void wxBoundingBox::SetMin(double px
, double py
)
261 void wxBoundingBox::SetMax(double px
, double py
)
273 void wxBoundingBox::SetValid(bool value
)
278 // adds an offset to the boundingbox
279 // usage : a_boundingbox.Translate(a_point);
280 void wxBoundingBox::Translate(wxPoint2DDouble
& offset
)
282 assert (m_validbbox
== TRUE
);
284 m_minx
+= offset
.m_x
;
285 m_maxx
+= offset
.m_x
;
286 m_miny
+= offset
.m_y
;
287 m_maxy
+= offset
.m_y
;
291 // clears the bounding box settings
292 void wxBoundingBox::Reset()
302 void wxBoundingBox::SetBoundingBox(const wxPoint2DDouble
& a_point
)
304 m_minx
= a_point
.m_x
;
305 m_maxx
= a_point
.m_x
;
306 m_miny
= a_point
.m_y
;
307 m_maxy
= a_point
.m_y
;
311 // Expands the boundingbox with the given point
312 // usage : a_boundingbox = a_boundingbox + pointer_to_an_offset;
313 wxBoundingBox
& wxBoundingBox::operator+(wxBoundingBox
&other
)
315 assert (m_validbbox
== TRUE
);
316 assert (other
.GetValid());
323 // makes a boundingbox same as the other
324 wxBoundingBox
& wxBoundingBox::operator=( const wxBoundingBox
&other
)
326 assert (other
.GetValid());
328 m_minx
= other
.m_minx
;
329 m_maxx
= other
.m_maxx
;
330 m_miny
= other
.m_miny
;
331 m_maxy
= other
.m_maxy
;
332 m_validbbox
= other
.m_validbbox
;
336 void wxBoundingBox::MapBbox( const wxTransformMatrix
& matrix
)
338 assert (m_validbbox
== TRUE
);
340 double x1
,y1
,x2
,y2
,x3
,y3
,x4
,y4
;
342 matrix
.TransformPoint( m_minx
, m_miny
, x1
, y1
);
343 matrix
.TransformPoint( m_minx
, m_maxy
, x2
, y2
);
344 matrix
.TransformPoint( m_maxx
, m_maxy
, x3
, y3
);
345 matrix
.TransformPoint( m_maxx
, m_miny
, x4
, y4
);
347 double xmin
= wxMin(x1
,x2
);
348 xmin
= wxMin(xmin
,x3
);
349 xmin
= wxMin(xmin
,x4
);
351 double xmax
= wxMax( x1
, x2
);
352 xmax
= wxMax(xmax
,x3
);
353 xmax
= wxMax(xmax
,x4
);
355 double ymin
= wxMin(y1
, y2
);
356 ymin
= wxMin(ymin
,y3
);
357 ymin
= wxMin(ymin
,y4
);
359 double ymax
= wxMax(y1
,y2
);
360 ymax
= wxMax(ymax
,y3
);
361 ymax
= wxMax(ymax
,y4
);
363 // Use these min and max values to set the new boundingbox