summaryrefslogtreecommitdiff
path: root/hw6/src/interval.cpp
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
committer53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
commitdb072ad4dc181eca5a1458656b130beb43f475bf (patch)
treea3c03c7f5497cb70503e2486662fa85cfb53415a /hw6/src/interval.cpp
downloadcsci427-master.tar.xz
csci427-master.zip
Diffstat (limited to 'hw6/src/interval.cpp')
-rw-r--r--hw6/src/interval.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/hw6/src/interval.cpp b/hw6/src/interval.cpp
new file mode 100644
index 0000000..4323cbd
--- /dev/null
+++ b/hw6/src/interval.cpp
@@ -0,0 +1,154 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#include "interval.h"
+#include "constants.h"
+
+//////////////////
+// Constrructor //
+//////////////////
+interval::interval(float lower, float upper)
+{
+ _lower = lower;
+ _upper = upper;
+
+ // ensure that _lower < _upper
+ if(_upper < _lower) std::swap(_upper, _lower);
+}
+
+
+interval::interval(const interval& i)
+{
+ _lower = i._lower;
+ _upper = i._upper;
+}
+
+
+
+///////////////
+// Operators //
+///////////////
+interval& interval::operator=(const interval& i)
+{
+ _assign(i);
+ return *this;
+}
+
+
+interval interval::operator+(float v) const
+{
+ return interval(_lower + v, _upper + v);
+}
+
+
+interval interval::operator-(float v) const
+{
+ return interval(_lower - v, _upper - v);
+}
+
+
+interval interval::operator*(float v) const
+{
+ return interval(_lower * v, _upper * v);
+}
+
+
+interval interval::operator/(float v) const
+{
+ return interval(_lower / v, _upper / v);
+}
+
+
+interval& interval::operator+=(float v)
+{
+ _lower += v;
+ _upper += v;
+ return *this;
+}
+
+interval& interval::operator-=(float v)
+{
+ _lower -= v;
+ _upper -= v;
+ return *this;
+}
+
+
+interval& interval::operator*=(float v)
+{
+ _lower *= v;
+ _upper *= v;
+
+ // ensure that _lower < _upper
+ if(_upper < _lower) std::swap(_upper, _lower);
+
+ // Done.
+ return *this;
+}
+
+interval& interval::operator/=(float v)
+{
+ _lower /= v;
+ _upper /= v;
+
+ // ensure that _lower < _upper
+ if(_upper < _lower) std::swap(_upper, _lower);
+
+ // Done.
+ return *this;
+}
+
+
+////////////////
+// Inspectors //
+////////////////
+float interval::lower(void) const
+{
+ return _lower;
+}
+
+
+float interval::upper(void) const
+{
+ return _upper;
+}
+
+
+bool interval::empty(void) const
+{
+ return (_upper - _lower < EPSILON);
+}
+
+
+
+//////////////
+// Mutators //
+//////////////
+void interval::intersect(const interval& i)
+{
+ _lower = std::max(_lower, i._lower);
+ _upper = std::min(_upper, i._upper);
+}
+
+
+
+/////////////////////
+// Private Methods //
+/////////////////////
+void interval::_assign(const interval& i)
+{
+ _lower = i._lower;
+ _upper = i._upper;
+}
+
+
+void interval::_swap(interval& i)
+{
+ std::swap(_lower, i._lower);
+ std::swap(_upper, i._upper);
+}