Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / range / b2ibox.hxx
blobab80e9c186e5f01cc7da4662b0e1e8405e3464f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 INCLUDED_BASEGFX_RANGE_B2IBOX_HXX
21 #define INCLUDED_BASEGFX_RANGE_B2IBOX_HXX
23 #include <ostream>
24 #include <vector>
26 #include <basegfx/point/b2ipoint.hxx>
27 #include <basegfx/point/b2dpoint.hxx>
28 #include <basegfx/tuple/b2ituple.hxx>
29 #include <basegfx/tuple/b2i64tuple.hxx>
30 #include <basegfx/range/basicbox.hxx>
31 #include <basegfx/basegfxdllapi.h>
33 namespace basegfx
35 /** A two-dimensional interval over integers
37 This is most easily depicted as a set of integers, bounded by
38 a lower and an upper value - but excluding the upper
39 value. All inbetween values are included in the set (see also
40 http://en.wikipedia.org/wiki/Interval_%28mathematics%29).
42 The set is half-open, i.e. the lower bound is included, the
43 upper bound not (if you're used to the notation - we're
44 talking about [a,b) here, compared to closed [a,b] or fully
45 open intervals (a,b)).
47 If you don't need a half-open interval, check B2IRange.
49 That means, isInside(val) will return true also for values of
50 val=a, but not for val=b.
52 Alternatively, consider this a rectangle, where the rightmost
53 pixel column and the bottommost pixel row are excluded - this
54 is much like polygon filling. As a result, filling a given
55 rectangle with basebmp::BitmapDevice::fillPolyPolygon(), will
56 affect exactly the same set of pixel as isInside() would
57 return true for.
59 @see B2IRange
61 class B2IBox
63 public:
64 typedef sal_Int32 ValueType;
65 typedef Int32Traits TraitsType;
67 B2IBox() {}
69 /// Create degenerate interval that's still empty
70 explicit B2IBox(const B2ITuple& rTuple)
71 : maRangeX(rTuple.getX()),
72 maRangeY(rTuple.getY())
76 /// Create proper interval between the two given points
77 B2IBox(sal_Int32 x1,
78 sal_Int32 y1,
79 sal_Int32 x2,
80 sal_Int32 y2) :
81 maRangeX(x1),
82 maRangeY(y1)
84 maRangeX.expand(x2);
85 maRangeY.expand(y2);
88 /// Create proper interval between the two given points
89 B2IBox(const B2ITuple& rTuple1,
90 const B2ITuple& rTuple2) :
91 maRangeX(rTuple1.getX()),
92 maRangeY(rTuple1.getY())
94 expand( rTuple2 );
97 /** Check if the interval set is empty
99 @return false, if no value is in this set - having a
100 single value included will still return false.
102 bool isEmpty() const
104 return maRangeX.isEmpty() || maRangeY.isEmpty();
107 bool operator==( const B2IBox& rBox ) const
109 return (maRangeX == rBox.maRangeX
110 && maRangeY == rBox.maRangeY);
113 bool operator!=( const B2IBox& rBox ) const
115 return (maRangeX != rBox.maRangeX
116 || maRangeY != rBox.maRangeY);
119 /// get lower bound of the set. returns arbitrary values for empty sets.
120 sal_Int32 getMinX() const
122 return maRangeX.getMinimum();
125 /// get lower bound of the set. returns arbitrary values for empty sets.
126 sal_Int32 getMinY() const
128 return maRangeY.getMinimum();
131 /// get upper bound of the set. returns arbitrary values for empty sets.
132 sal_Int32 getMaxX() const
134 return maRangeX.getMaximum();
137 /// get upper bound of the set. returns arbitrary values for empty sets.
138 sal_Int32 getMaxY() const
140 return maRangeY.getMaximum();
143 /// return difference between upper and lower X value. returns 0 for empty sets.
144 sal_Int64 getWidth() const
146 return maRangeX.getRange();
149 /// return difference between upper and lower Y value. returns 0 for empty sets.
150 sal_Int64 getHeight() const
152 return maRangeY.getRange();
155 /// yields true if point is contained in set
156 bool isInside(const B2ITuple& rTuple) const
158 return (
159 maRangeX.isInside(rTuple.getX())
160 && maRangeY.isInside(rTuple.getY())
164 /// add point to the set, expanding as necessary
165 void expand(const B2ITuple& rTuple)
167 maRangeX.expand(rTuple.getX());
168 maRangeY.expand(rTuple.getY());
171 /// calc set intersection
172 void intersect(const B2IBox& rBox)
174 maRangeX.intersect(rBox.maRangeX);
175 maRangeY.intersect(rBox.maRangeY);
178 private:
179 BasicBox maRangeX;
180 BasicBox maRangeY;
183 } // end of namespace basegfx
185 template< typename charT, typename traits >
186 inline std::basic_ostream<charT, traits> & operator <<(
187 std::basic_ostream<charT, traits> & stream, const basegfx::B2IBox& box )
189 if (box.isEmpty())
190 return stream << "EMPTY";
191 else
192 return stream << box.getWidth() << 'x' << box.getHeight()
193 << "@(" << box.getMinX() << "," << box.getMinY() << ")";
196 #endif // INCLUDED_BASEGFX_RANGE_B2IBOX_HXX
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */