A library to manipulate 2D arrays, and output them to an LED Dot Matrix Display. The display must be wired up using a shift register combined with a nor latch.
Diff: Locations.h
- Revision:
- 1:44819562ea31
- Parent:
- 0:1deae5ffe9ed
--- a/Locations.h Mon Feb 13 22:19:58 2012 +0000 +++ b/Locations.h Mon Feb 13 23:20:30 2012 +0000 @@ -1,31 +1,33 @@ #ifndef LOCATIONS_H #define LOCATIONS_H -class FloatLocation; -class IntLocation { -public: - IntLocation(int x = 0, int y = 0) : x(x), y(y) { } - int x, y; - operator FloatLocation(); - bool operator==(IntLocation l) { return l.x == x && l.y == y; } - IntLocation operator+(IntLocation l) { return IntLocation(l.x + x, l.y + y); } -}; - -class FloatLocation { -public: - FloatLocation(float x = 0, float y = 0) : x(x), y(y) { } - float x, y; - operator IntLocation() { - return IntLocation((int) (x + 0.5), (int) (y + 0.5)); - } - FloatLocation lerp(FloatLocation to, float alpha) { - return FloatLocation( +template <typename T> class Location { + public: + T x, y; + //Constructor + Location(T x = 0, T y = 0) : x(x), y(y) { }; + + //Equals operator + bool operator==(Location<T> l) { return l.x == x && l.y == y; } + + //Addition operator + Location<T> operator+(Location<T> l) { return Location<T>(l.x + x, l.y + y); } + + //Linerearly interpolate + Location<T> lerp(Location<T> to, float alpha) { + return Location<T>( x*(1 - alpha) + to.x*alpha, y*(1 - alpha) + to.y*alpha ); } - bool operator==(FloatLocation l) { return l.x == x && l.y == y; } + + template <typename T2> operator Location<T2>(); }; -IntLocation::operator FloatLocation() { - return FloatLocation(x, y); + +//Convert from Location<int> to Location<float> and back +template <> Location<int>::operator Location<float>() { + return Location<float>(x, y); +} +template <> Location<float>::operator Location<int>() { + return Location<int>((int) (x + 0.5), (int) (y + 0.5)); } #endif \ No newline at end of file