4 # Copyright (C) 2002 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 # TODO: - shouldn't we rename bbox.transform to bbox.transformed? (AW, 23.1.03)
24 # - it would be nice to have a real bbox.transform (AW, 23.1.03)
28 # helper routine for bbox manipulations
31 """minimum of two values, where None represents +infinity, not -infinity as
32 in standard min implementation of python"""
33 if x
is None: return y
34 if y
is None: return x
38 # class representing bounding boxes
43 """class for bounding boxes"""
45 def __init__(self
, llx
=None, lly
=None, urx
=None, ury
=None):
51 def __add__(self
, other
):
54 return bbox(_nmin(self
.llx
, other
.llx
), _nmin(self
.lly
, other
.lly
),
55 max(self
.urx
, other
.urx
), max(self
.ury
, other
.ury
))
57 def __mul__(self
, other
):
58 """return intersection of two bboxes"""
60 return bbox(max(self
.llx
, other
.llx
), max(self
.lly
, other
.lly
),
61 _nmin(self
.urx
, other
.urx
), _nmin(self
.ury
, other
.ury
))
64 return "%s %s %s %s" % (self
.llx
, self
.lly
, self
.urx
, self
.ury
)
66 def write(self
, file):
67 file.write("%%%%BoundingBox: %d %d %d %d\n" %
68 (self
.llx
, self
.lly
, self
.urx
, self
.ury
))
69 # TODO: add HighResBBox
71 def intersects(self
, other
):
72 """check, if two bboxes intersect eachother"""
74 return not (self
.llx
> other
.urx
or
75 self
.lly
> other
.ury
or
76 self
.urx
< other
.llx
or
79 def transform(self
, trafo
):
80 """return bbox transformed by trafo"""
81 # we have to transform all four corner points of the bbox
82 (llx
, lly
)=trafo
._apply
(self
.llx
, self
.lly
)
83 (lrx
, lry
)=trafo
._apply
(self
.urx
, self
.lly
)
84 (urx
, ury
)=trafo
._apply
(self
.urx
, self
.ury
)
85 (ulx
, uly
)=trafo
._apply
(self
.llx
, self
.ury
)
87 # now, by sorting, we obtain the lower left and upper right corner
88 # of the new bounding box.
90 return bbox(min(llx
, lrx
, urx
, ulx
), min(lly
, lry
, ury
, uly
),
91 max(llx
, lrx
, urx
, ulx
), max(lly
, lry
, ury
, uly
))
93 def enhance(self
, size
):
94 """return bbox enhanced in all directions by size"""
95 size
= unit
.topt(unit
.length(size
, default_type
="v"))
96 return bbox(self
.llx
-size
, self
.lly
-size
,
97 self
.urx
+size
, self
.ury
+size
)
100 """return rectangle corresponding to bbox"""
102 return path
._rect
(self
.llx
, self
.lly
, self
.urx
-self
.llx
, self
.ury
-self
.lly
)