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 .
20 #ifndef _BGFX_RANGE_B2IRANGE_HXX
21 #define _BGFX_RANGE_B2IRANGE_HXX
23 #include <basegfx/point/b2ipoint.hxx>
24 #include <basegfx/point/b2dpoint.hxx>
25 #include <basegfx/tuple/b2ituple.hxx>
26 #include <basegfx/tuple/b2i64tuple.hxx>
27 #include <basegfx/range/basicrange.hxx>
29 #include <basegfx/basegfxdllapi.h>
34 /** A two-dimensional interval over integers
36 This is a set of real numbers, bounded by a lower and an upper
37 pair. All inbetween values are included in the set (see also
38 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
40 Probably you rather want B2IBox for integers.
42 The set is closed, i.e. the upper and the lower bound are
43 included (if you're used to the notation - we're talking about
44 [a,b] here, compared to half-open [a,b) or open intervals
47 That means, isInside(val) will return true also for values of
55 typedef sal_Int32 ValueType
;
56 typedef Int32Traits TraitsType
;
60 /// Create degenerate interval consisting of a single point
61 explicit B2IRange(const B2ITuple
& rTuple
)
62 : maRangeX(rTuple
.getX()),
63 maRangeY(rTuple
.getY())
67 /// Create proper interval between the two given integer pairs
68 B2IRange(sal_Int32 x1
,
79 /// Create proper interval between the two given points
80 B2IRange(const B2ITuple
& rTuple1
,
81 const B2ITuple
& rTuple2
)
82 : maRangeX(rTuple1
.getX()),
83 maRangeY(rTuple1
.getY())
88 /** Check if the interval set is empty
90 @return false, if no value is in this set - having a
91 single point included will already return true.
95 return maRangeX
.isEmpty() || maRangeY
.isEmpty();
98 /// reset the object to empty state again, clearing all values
105 bool operator==( const B2IRange
& rRange
) const
107 return (maRangeX
== rRange
.maRangeX
108 && maRangeY
== rRange
.maRangeY
);
111 bool operator!=( const B2IRange
& rRange
) const
113 return (maRangeX
!= rRange
.maRangeX
114 || maRangeY
!= rRange
.maRangeY
);
117 /// get lower bound of the set. returns arbitrary values for empty sets.
118 sal_Int32
getMinX() const
120 return maRangeX
.getMinimum();
123 /// get lower bound of the set. returns arbitrary values for empty sets.
124 sal_Int32
getMinY() const
126 return maRangeY
.getMinimum();
129 /// get upper bound of the set. returns arbitrary values for empty sets.
130 sal_Int32
getMaxX() const
132 return maRangeX
.getMaximum();
135 /// get upper bound of the set. returns arbitrary values for empty sets.
136 sal_Int32
getMaxY() const
138 return maRangeY
.getMaximum();
141 /// return difference between upper and lower X value. returns 0 for empty sets.
142 sal_Int64
getWidth() const
144 return maRangeX
.getRange();
147 /// return difference between upper and lower Y value. returns 0 for empty sets.
148 sal_Int64
getHeight() const
150 return maRangeY
.getRange();
153 /// get lower bound of the set. returns arbitrary values for empty sets.
154 B2IPoint
getMinimum() const
157 maRangeX
.getMinimum(),
158 maRangeY
.getMinimum()
162 /// get upper bound of the set. returns arbitrary values for empty sets.
163 B2IPoint
getMaximum() const
166 maRangeX
.getMaximum(),
167 maRangeY
.getMaximum()
171 /// return difference between upper and lower point. returns (0,0) for empty sets.
172 B2I64Tuple
getRange() const
180 /// return center point of set. returns (0,0) for empty sets.
181 B2DPoint
getCenter() const
184 maRangeX
.getCenter(),
189 /// yields true if given point is contained in set
190 bool isInside(const B2ITuple
& rTuple
) const
193 maRangeX
.isInside(rTuple
.getX())
194 && maRangeY
.isInside(rTuple
.getY())
198 /// yields true if rRange is inside, or equal to set
199 bool isInside(const B2IRange
& rRange
) const
202 maRangeX
.isInside(rRange
.maRangeX
)
203 && maRangeY
.isInside(rRange
.maRangeY
)
207 /// yields true if rRange at least partly inside set
208 bool overlaps(const B2IRange
& rRange
) const
211 maRangeX
.overlaps(rRange
.maRangeX
)
212 && maRangeY
.overlaps(rRange
.maRangeY
)
216 /// yields true if overlaps(rRange) does, and the overlap is larger than infinitesimal
217 bool overlapsMore(const B2IRange
& rRange
) const
220 maRangeX
.overlapsMore(rRange
.maRangeX
)
221 && maRangeY
.overlapsMore(rRange
.maRangeY
)
225 /// add point to the set, expanding as necessary
226 void expand(const B2ITuple
& rTuple
)
228 maRangeX
.expand(rTuple
.getX());
229 maRangeY
.expand(rTuple
.getY());
232 /// add rRange to the set, expanding as necessary
233 void expand(const B2IRange
& rRange
)
235 maRangeX
.expand(rRange
.maRangeX
);
236 maRangeY
.expand(rRange
.maRangeY
);
239 /// calc set intersection
240 void intersect(const B2IRange
& rRange
)
242 maRangeX
.intersect(rRange
.maRangeX
);
243 maRangeY
.intersect(rRange
.maRangeY
);
246 /// grow set by nValue on all sides
247 void grow(sal_Int32 nValue
)
249 maRangeX
.grow(nValue
);
250 maRangeY
.grow(nValue
);
254 typedef ::basegfx::BasicRange
< ValueType
, TraitsType
> MyBasicRange
;
256 MyBasicRange maRangeX
;
257 MyBasicRange maRangeY
;
260 /** Compute the set difference of the two given ranges
262 This method calculates the symmetric difference (aka XOR)
263 between the two given ranges, and returning the resulting
264 ranges. Thus, the result will contain all areas where one, but
268 Result vector. The up to four difference ranges are returned
277 @return the input vector
279 BASEGFX_DLLPUBLIC ::std::vector
< B2IRange
>& computeSetDifference( ::std::vector
< B2IRange
>& o_rResult
,
280 const B2IRange
& rFirst
,
281 const B2IRange
& rSecond
);
283 } // end of namespace basegfx
285 #endif /* _BGFX_RANGE_B2IRANGE_HXX */
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */