This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/util/Interval.cpp

71 lines
1.4 KiB
C++
Raw Normal View History

2013-04-04 20:44:15 +00:00
/*
* Interval.cpp
*
* Created on: 03.04.2013
* Author: Felix
*/
#include "Interval.h"
#include <utility>
Interval::Interval(float start, float end) :
start(start),
end(end) {
2013-04-04 21:13:08 +00:00
if (this->start > this->end)
2013-04-04 20:44:15 +00:00
std::swap(this->start, this->end);
};
/**
* Creates an interval from a center point and a radius. The interval
* ranges from center - radius to center + radius.
*/
Interval
Interval::IntervalFromRadius(float center, float radius) {
return Interval(center - radius, center + radius);
}
/**
* Creates an Interval from a start and end point.
*/
Interval
Interval::IntervalFromPoints(float start, float end) {
return Interval(start, end);
}
/**
* Returns the overlap between two intervals, e.g. the overlap between
* intervals (1,3) and (2,4) is (2,3).
*/
Interval
Interval::getOverlap(Interval other) const {
2013-04-04 21:13:08 +00:00
if ((start == other.start) && (end == other.end))
2013-04-04 20:44:15 +00:00
return *this;
Interval smaller = *this;
Interval bigger = other;
2013-04-04 21:13:08 +00:00
if (smaller.start > bigger.start)
2013-04-04 20:44:15 +00:00
std::swap(smaller, bigger);
Interval iv(0, 0);
if (bigger.start < smaller.end) {
iv.start = bigger.start;
iv.end = smaller.end;
}
2013-04-04 21:13:08 +00:00
else
2013-04-04 20:44:15 +00:00
iv.start = iv.end = 0.0f;
return iv;
}
/**
* Returns true if the point is inside the interval.
*/
bool
Interval::isInside(float point) const {
return start < point && point < end;
}
/**
* Returns the length of the interval (distance between start and end).
*/
float
Interval::getLength() {
return end - start;
}