Files
Ascent-of-Man/matrix2d.h
T
2026-11-08 15:42:54 +01:00

37 lines
895 B
C++

#ifndef MATRIX2D_H
#define MATRIX2D_H
template <typename T>
class Matrix2D {
std::vector<T> data;
unsigned int sizeX, sizeY;
public:
unsigned int getWidth() const { return sizeX; }
unsigned int getHeight() const { return sizeY; }
Matrix2D (unsigned int x, unsigned int y)
: sizeX (x), sizeY (y) {
data.resize (sizeX*sizeY);
}
T& operator()( int x, int y) {
if (x >= (int) sizeX )
x-= sizeX;
if (x < 0 )
x+= sizeX;
if (y<0) y=0;
if (y >= (int) sizeY )
y =sizeY-1;
//if (y>= sizeY || y<0)
// throw std::out_of_range("OOB access"); // Throw something more appropriate
return data[sizeX*y + x]; // Stride-aware access
}
T* row(unsigned int y) {
if (y >= sizeY) y = sizeY - 1;
return &data[sizeX * y];
}
};
#endif // MATRIX2D_H