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.
Matrix.h
00001 #include <cassert> 00002 #include "Locations.h" 00003 00004 #ifndef Matrix_H 00005 #define Matrix_H 00006 template <class T> class Matrix { 00007 private: 00008 //Internal data 00009 T* _data; 00010 int _width; 00011 int _height; 00012 00013 //Unsafe cell accessors 00014 const T& item(int x, int y) const { return _data[y*_width + x]; } 00015 T& item(int x, int y) { return _data[y*_width + x]; } 00016 00017 //Initializer 00018 void init() { _data = new T[_width*_height]; } 00019 public: 00020 //Construct an empty matrix 00021 Matrix(int width, int height) : _width(width), _height(height) { init(); } 00022 00023 //Construct a matrix from a 2D array 00024 template <int w, int h> 00025 Matrix(T (&array)[h][w]) : _width(w), _height(h) { 00026 init(); 00027 00028 for(int x = 0; x < w; x++) 00029 for(int y = 0; y < h; y++) 00030 item(x, y) = array[y][x]; 00031 } 00032 00033 //Copy constructor 00034 Matrix(const Matrix<T> &that) : _width(that._width), _height(that._height) { 00035 init(); 00036 00037 for(int i = 0; i < _width; i++) 00038 for(int j = 0; j < _height; j++) 00039 this->item(i, j) = that.item(i, j); 00040 } 00041 00042 //Destructor - clean up pointers 00043 ~Matrix() { delete [] _data; } 00044 00045 //Size accessors 00046 int getWidth() const { return _width; } 00047 int getHeight() const { return _height; } 00048 00049 //Bounds checking 00050 bool contains(int x, int y) const { 00051 return x >= 0 && x < _width && y >= 0 && y < _height; 00052 } 00053 bool containsRect(int x, int y, int w, int h) const { 00054 return x >= 0 && x + w <= _width && y >= 0 && y + h <= _height; 00055 } 00056 00057 //Safe cell accessors 00058 const T& operator() (int x, int y) const { assert(contains(x, y)); return item(x, y); } 00059 T& operator() (int x, int y) { assert(contains(x, y)); return item(x, y); } 00060 const T& operator[] (const Location<int> &p) const { return (*this)(p.x, p.y); } 00061 T& operator[] (const Location<int> &p) { return (*this)(p.x, p.y); } 00062 00063 //Row and column accessors 00064 T* column(int x) const { 00065 assert(x >= 0 && x <= _width); 00066 T* col = new T[_height]; 00067 for(int j = 0; j < _height; j++) { 00068 col[j] = this->item(x, j); 00069 } 00070 return col; 00071 } 00072 T* row(int y) const { 00073 assert(y >= 0 && y <= _height); 00074 T* row = new T[_width]; 00075 for(int i = 0; i < _width; i++) { 00076 row[i] = this->item(i, y); 00077 } 00078 return row; 00079 } 00080 00081 //Cut out a rectangle from an existing matrix 00082 Matrix<T> slice(int x, int y, int w, int h) const { 00083 assert(containsRect(x, y, w, h)); 00084 00085 Matrix<T> m = Matrix<T>(w, h); 00086 00087 for(int i = 0; i < w; i++) 00088 for(int j = 0; j < h; j++) 00089 m.item(i, j) = this->item(x + i, y + j); 00090 00091 return m; 00092 } 00093 00094 //Copy the data in the rectangle (sx, sy, w, h) from "that" to "this" at (dx, dy) 00095 void overlay(const Matrix<T> &that, int sx, int sy, int w, int h, int dx, int dy) { 00096 assert(containsRect(dx, dy, w, h) && that.containsRect(sx, sy, w, h)); 00097 00098 for(int x = 0; x < w; x++) { 00099 for(int y = 0; y < h; y++) { 00100 this->item(dx + x, dy + y) = that.item(sx + x, sy + y); 00101 } 00102 } 00103 } 00104 00105 //Empty the matrix 00106 void clear() { 00107 for(int x = 0; x < _width; x++) 00108 for(int y = 0; y < _height; y++) 00109 this->item(x, y) = T(); 00110 } 00111 }; 00112 #endif
Generated on Tue Jul 19 2022 21:46:55 by 1.7.2