1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
24 #include <basegfx/tuple/b2ituple.hxx>
25 #include <basegfx/range/basicbox.hxx>
29 /** A two-dimensional interval over integers
31 This is most easily depicted as a set of integers, bounded by
32 a lower and an upper value - but excluding the upper
33 value. All inbetween values are included in the set (see also
34 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
36 The set is half-open, i.e. the lower bound is included, the
37 upper bound not (if you're used to the notation - we're
38 talking about [a,b) here, compared to closed [a,b] or fully
39 open intervals (a,b)).
41 If you don't need a half-open interval, check B2IRange.
43 That means, isInside(val) will return true also for values of
44 val=a, but not for val=b.
46 Alternatively, consider this a rectangle, where the rightmost
47 pixel column and the bottommost pixel row are excluded - this
48 is much like polygon filling. As a result, filling a given
49 rectangle with basebmp::BitmapDevice::fillPolyPolygon(), will
50 affect exactly the same set of pixel as isInside() would
58 typedef sal_Int32 ValueType
;
59 typedef Int32Traits TraitsType
;
63 /// Create degenerate interval that's still empty
64 explicit B2IBox(const B2ITuple
& rTuple
)
65 : maRangeX(rTuple
.getX()),
66 maRangeY(rTuple
.getY())
70 /// Create proper interval between the two given points
82 /// Create proper interval between the two given points
83 B2IBox(const B2ITuple
& rTuple1
,
84 const B2ITuple
& rTuple2
) :
85 maRangeX(rTuple1
.getX()),
86 maRangeY(rTuple1
.getY())
91 /** Check if the interval set is empty
93 @return false, if no value is in this set - having a
94 single value included will still return false.
98 return maRangeX
.isEmpty() || maRangeY
.isEmpty();
101 bool operator==( const B2IBox
& rBox
) const
103 return (maRangeX
== rBox
.maRangeX
104 && maRangeY
== rBox
.maRangeY
);
107 bool operator!=( const B2IBox
& rBox
) const
109 return (maRangeX
!= rBox
.maRangeX
110 || maRangeY
!= rBox
.maRangeY
);
113 /// get lower bound of the set. returns arbitrary values for empty sets.
114 sal_Int32
getMinX() const
116 return maRangeX
.getMinimum();
119 /// get lower bound of the set. returns arbitrary values for empty sets.
120 sal_Int32
getMinY() const
122 return maRangeY
.getMinimum();
125 /// get upper bound of the set. returns arbitrary values for empty sets.
126 sal_Int32
getMaxX() const
128 return maRangeX
.getMaximum();
131 /// get upper bound of the set. returns arbitrary values for empty sets.
132 sal_Int32
getMaxY() const
134 return maRangeY
.getMaximum();
137 /// return difference between upper and lower X value. returns 0 for empty sets.
138 sal_Int64
getWidth() const
140 return maRangeX
.getRange();
143 /// return difference between upper and lower Y value. returns 0 for empty sets.
144 sal_Int64
getHeight() const
146 return maRangeY
.getRange();
149 /// yields true if point is contained in set
150 bool isInside(const B2ITuple
& rTuple
) const
153 maRangeX
.isInside(rTuple
.getX())
154 && maRangeY
.isInside(rTuple
.getY())
158 /// add point to the set, expanding as necessary
159 void expand(const B2ITuple
& rTuple
)
161 maRangeX
.expand(rTuple
.getX());
162 maRangeY
.expand(rTuple
.getY());
165 /// calc set intersection
166 void intersect(const B2IBox
& rBox
)
168 maRangeX
.intersect(rBox
.maRangeX
);
169 maRangeY
.intersect(rBox
.maRangeY
);
177 } // end of namespace basegfx
179 template< typename charT
, typename traits
>
180 inline std::basic_ostream
<charT
, traits
> & operator <<(
181 std::basic_ostream
<charT
, traits
> & stream
, const basegfx::B2IBox
& box
)
184 return stream
<< "EMPTY";
186 return stream
<< box
.getWidth() << 'x' << box
.getHeight()
187 << "@(" << box
.getMinX() << "," << box
.getMinY() << ")";
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */