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

Committer:
EricWieser
Date:
2012-02-13
Revision:
0:1deae5ffe9ed
Child:
1:44819562ea31

File content as of revision 0:1deae5ffe9ed:

#include <cassert> 
#include "Locations.h"

#ifndef Matrix_H
#define Matrix_H
template <class T> class Matrix {
  private:
    T* _data;
    int _width;
    int _height;
    
    T& item(int x, int y) const{
        return _data[y*_width + x];
    }
    
    void init() {
        _data = new T[_width*_height];
    }
  public:
    Matrix(int width, int height) : _width(width), _height(height) {
        init();
    }
    
    template <int w, int h>
    Matrix(T (&array)[h][w]) : _width(w), _height(h) {
        init();
        
        for(int x = 0; x < w; x++) {
            for(int y = 0; y < h; y++) {
                item(x, y) = array[y][x];
            }
        }
    }
    
    Matrix(const Matrix<T> &that) : _width(that._width), _height(that._height) {
        init();
       
        for(int i = 0; i < _width; i++) {
            for(int j = 0; j < _height; j++) {
                this->item(i, j) = that.item(i, j);
            }
        }
    }
    
    ~Matrix() {
        delete [] _data;
    }
    
    int getWidth() const { return _width; }
    int getHeight() const { return _height; }
    
    bool contains(int x, int y) const {
        return x >= 0 && x < _width && y >= 0 && y < _height;
    }
    bool containsRect(int x, int y, int w, int h) const {
        return x >= 0 && x + w <= _width && y >= 0 && y + h <= _height;
    }
    
    T& operator() (const int x, const int y) {
        assert(contains(x, y));
        return item(x, y);
    }
    
    
    const T& operator() (const int x, const int y) const {
        assert(contains(x, y));
        return item(x, y);
    }
    
    T& operator[] (const IntLocation p) {
        return (*this)(p.x, p.y);
    }
    const T& operator[] (const IntLocation p) const {
        return (*this)(p.x, p.y);
    }
    
    
    Matrix<T> slice(int x, int y, int w, int h) const {
        assert(containsRect(x, y, w, h));
        
        Matrix<T> b = Matrix<T>(w, h);
        
        for(int i = 0; i < w; i++) {
            for(int j = 0; j < h; j++) {
                b.item(i, j) = this->item(x + i, y + j);
            }
        }
        return b;
    }
    
    
    void overlay(const Matrix<T> &that, int x, int y) {
        assert(containsRect(x, y, that._width, that._height));
        
        for(int i = 0; i < that._width; i++) {
            for(int j = 0; j < that._height; j++) {
                this->item(i, j) = that.item(x + i, y + j);
            }
        }
    }
    
    void overlay(const Matrix<T> &that, int sx, int sy, int w, int h, int dx, int dy) {
        assert(containsRect(dx, dy, w, h) && that.containsRect(sx, sy, w, h));
        
        for(int i = 0; i < w; i++) {
            for(int j = 0; j < h; j++) {
                this->item(dx + i, dy + j) = that.item(sx + i, sy + j);
            }
        }
    }
    
    T* column(int x) const {
        assert(x >= 0 && x  <= _width);
        T* col = new T[_height];
        for(int j = 0; j < _height; j++) {
            col[j] = this->item(x, j);
        }
        return col;        
    }
    T* row(int y) const {
        assert(y >= 0 && y <= _height);
        T* row = new T[_width];
        for(int i = 0; i < _width; i++) {
            row[i] = this->item(i, y);
        }
        return row;        
    }
    
    void clear() {
        for(int x = 0; x < _width; x++)
            for(int y = 0; y < _width; y++)
                (*this)(x, y) = T();
    }
};
#endif