Eric Wieser / LEDMatrix
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Locations.h Source File

Locations.h

00001 #ifndef LOCATIONS_H
00002 #define LOCATIONS_H
00003 template <typename T> class Location {
00004   public:
00005     T x, y;
00006     //Constructor
00007     Location(T x = 0, T y = 0) : x(x), y(y) { };
00008     
00009     //Equals operator
00010     bool operator==(Location<T> l) { return l.x == x && l.y == y; }
00011     
00012     //Addition operator
00013     Location<T> operator+(Location<T> l) { return Location<T>(l.x + x, l.y + y); }
00014     
00015     //Linerearly interpolate
00016     Location<T> lerp(Location<T> to, float alpha) {
00017         return Location<T>(
00018                    x*(1 - alpha) + to.x*alpha,
00019                    y*(1 - alpha) + to.y*alpha
00020                );
00021     }
00022     
00023     template <typename T2> operator Location<T2>();
00024 };
00025 
00026 //Convert from Location<int> to Location<float> and back
00027 template <> Location<int>::operator Location<float>() {
00028     return Location<float>(x, y);
00029 }
00030 template <> Location<float>::operator Location<int>() {
00031     return Location<int>((int) (x + 0.5), (int) (y + 0.5));
00032 }
00033 #endif