summaryrefslogblamecommitdiff
path: root/hw6/src/interval.cpp
blob: 4323cbd80fd1e84facb8f7d81c71e9636787fd31 (plain) (tree)

























































































































































                                                                    
/******************************************************************/
/* 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);
}