Fixed logic of Interval::getOverlap.

This commit is contained in:
Felix Ableitner 2013-05-26 18:37:59 +02:00
parent 12048ffc8c
commit 27fd692471
2 changed files with 12 additions and 16 deletions

26
source/util/Interval.cpp Normal file → Executable file
View file

@ -12,7 +12,7 @@
Interval::Interval(float start, float end) : Interval::Interval(float start, float end) :
start(start), start(start),
end(end) { end(end) {
if (this->start > this->end) if (start > end)
std::swap(this->start, this->end); std::swap(this->start, this->end);
}; };
/** /**
@ -37,20 +37,16 @@ Interval::IntervalFromPoints(float start, float end) {
* intervals (1,3) and (2,4) is (2,3). * intervals (1,3) and (2,4) is (2,3).
*/ */
Interval Interval
Interval::getOverlap(Interval other) const { Interval::getOverlap(const Interval& other) const {
if ((start == other.start) && (end == other.end))
return *this;
Interval smaller = *this;
Interval bigger = other;
if (smaller.start > bigger.start)
std::swap(smaller, bigger);
Interval iv(0, 0); Interval iv(0, 0);
if (bigger.start < smaller.end) { if (other.isInside(start))
iv.start = bigger.start; iv.start = start;
iv.end = smaller.end; else if (isInside(other.start))
} iv.start = other.start;
else if (other.isInside(end))
iv.start = iv.end = 0.0f; iv.end = end;
else if (isInside(other.end))
iv.end = other.end;
return iv; return iv;
} }
@ -59,7 +55,7 @@ Interval::getOverlap(Interval other) const {
*/ */
bool bool
Interval::isInside(float point) const { Interval::isInside(float point) const {
return start < point && point < end; return start <= point && point <= end;
} }
/** /**
* Returns the length of the interval (distance between start and end). * Returns the length of the interval (distance between start and end).

View file

@ -12,7 +12,7 @@ class Interval {
public: public:
static Interval IntervalFromRadius(float center, float radius); static Interval IntervalFromRadius(float center, float radius);
static Interval IntervalFromPoints(float start, float end); static Interval IntervalFromPoints(float start, float end);
Interval getOverlap(Interval other) const; Interval getOverlap(const Interval& other) const;
bool isInside(float point) const; bool isInside(float point) const;
float getLength(); float getLength();