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

67 lines
1.3 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-05-26 16:37:59 +00:00
if (start > 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
2013-05-26 16:37:59 +00:00
Interval::getOverlap(const Interval& other) const {
2013-04-04 20:44:15 +00:00
Interval iv(0, 0);
2013-05-26 16:37:59 +00:00
if (other.isInside(start))
iv.start = start;
else if (isInside(other.start))
iv.start = other.start;
if (other.isInside(end))
iv.end = end;
else if (isInside(other.end))
iv.end = other.end;
2013-04-04 20:44:15 +00:00
return iv;
}
/**
* Returns true if the point is inside the interval.
*/
bool
Interval::isInside(float point) const {
2013-05-26 16:37:59 +00:00
return start <= point && point <= end;
2013-04-04 20:44:15 +00:00
}
/**
* Returns the length of the interval (distance between start and end).
*/
float
Interval::getLength() {
return end - start;
}