/*********************************************************\
         __      __           __  __
        / /     / / _____    / / / / ______   __     __
       / /     / / /___  \  / / / / / ___  | / /    / /
      / /_____/ / _____| / / / / / / /__/ / / /    / /
     / ______  / / ___  / / / / / / _____/ / /    / /
    / /     / / / /__/ / / / / / / |____  / |____/ /
   /_/     /_/ |______/ /_/ /_/  \_____/  \_____  /
                                         ______/ /
            High Level Game Framework   /_______/

  ------------------------------------------------------

  Copyright (c) 2009 - Rodrigo Braz Monteiro.
  This file is subject to the terms of license.txt.

\*********************************************************/


#pragma once

namespace Halley {

	template <typename T>
	class Range {
	public:
		T s;
		T e;

		Range(T start, T end) {
			if (start < end) {
				s = start;
				e = end;
			} else {
				s = end;
				e = start;
			}
		}

		inline T getLength() { return e - s; }

		inline bool overlaps(const Range &p) const
		{
			return (s < p.e) && (p.s < e);
		}

		inline Range getOverlap(const Range &p) const
		{
			return Range(max(s, p.s), min(e, p.e));
		}
		
		inline Range getSweepOverlap(T len) const
		{
			return Range(s - len, e);
		}

		T getTimeToOverlap(const Range& p, T factor=1) const
		{
			T sx = p.s;
			T ex = p.e;
			T divisor = ex - sx;
			if (divisor == 0) return 0;

			T t1 = (s-sx)*factor/divisor;
			T t2 = (e-sx)*factor/divisor;
			return min(abs(t1), abs(t2));
		}
	};
}
