summaryrefslogtreecommitdiffstats
path: root/src/2geom/generic-interval.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2geom/generic-interval.h')
-rw-r--r--src/2geom/generic-interval.h29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/2geom/generic-interval.h b/src/2geom/generic-interval.h
index f6d4718de..716390e57 100644
--- a/src/2geom/generic-interval.h
+++ b/src/2geom/generic-interval.h
@@ -90,13 +90,24 @@ public:
}
/// @}
- /// @name Inspect endpoints.
+ /// @name Inspect contained values.
/// @{
C min() const { return _b[0]; }
C max() const { return _b[1]; }
C extent() const { return max() - min(); }
C middle() const { return (max() + min()) / 2; }
bool isSingular() const { return min() == max(); }
+ C operator[](unsigned i) const { assert(i < 2); return _b[i]; }
+ C clamp(C val) const {
+ if (val < min()) return min();
+ if (val > max()) return max();
+ return val;
+ }
+ /// Return the closer end of the interval.
+ C nearestEnd(C val) const {
+ C dmin = std::abs(val - min()), dmax = std::abs(val - max());
+ return dmin <= dmax ? min() : max();
+ }
/// @}
/// @name Test coordinates and other intervals for inclusion.
@@ -138,6 +149,16 @@ public:
_b[1] = val;
}
}
+ /// Set both ends of the interval simultaneously
+ void setEnds(C a, C b) {
+ if (a <= b) {
+ _b[0] = a;
+ _b[1] = b;
+ } else {
+ _b[0] = b;
+ _b[1] = a;
+ }
+ }
/** @brief Extend the interval to include the given number. */
void expandTo(C val) {
if(val < _b[0]) _b[0] = val;
@@ -271,6 +292,8 @@ public:
/** @brief Check whether this interval is empty. */
bool isEmpty() { return !*this; };
+ /// Alias of isEmpty() for STL similarity.
+ bool empty() { return !*this; }
/** @brief Union with another interval, gracefully handling empty ones. */
void unionWith(GenericOptInterval<C> const &a) {
@@ -317,14 +340,14 @@ inline GenericOptInterval<C> operator&(GenericInterval<C> const &a, GenericInter
return GenericOptInterval<C>(a) & GenericOptInterval<C>(b);
}
-//#ifdef _GLIBCXX_IOSTREAM
+#ifdef _GLIBCXX_IOSTREAM
template <typename C>
inline std::ostream &operator<< (std::ostream &os,
Geom::GenericInterval<C> const &I) {
os << "Interval("<<I.min() << ", "<<I.max() << ")";
return os;
}
-//#endif
+#endif
} // namespace Geom
#endif // !LIB2GEOM_SEEN_GENERIC_INTERVAL_H