#include #include using namespace std; #include "apmatrix.h" class Window { public: Window(int rows, int cols); bool IsInBounds(int row, int col) const; // post: returns true if the point (row,col) is // in this window; // otherwise returns false void ColorSquare(int ULrow, int ULcol, int N, int val); // post: all points in this window that are also in the // N-by-N square with upper left corner // (ULrow, ULcol) have been set to val; // points in the square that are not in this // window are ignored int ValAt(int row, int col) const; // post: returns color value at positoin row, col // in this window void print() const; private: int myNumRows; int myNumCols; apmatrix myMat; }; Window::Window(int rows, int cols) : myNumRows(rows), myNumCols(cols), myMat(rows,cols,1) { } void Window::print() const { int j,k; cout << " " << setw(3) << " "; // row number offset for(j=0; j < myNumCols; j++) { cout << setw(3) << j << " "; } cout << endl; cout << setw(3) << " +"; // row number offset for(j=0; j < myNumCols; j++) { cout << "----"; } cout << endl; for(j=0; j < myNumRows; j++) { cout << setw(1) << j << " | "; for(k=0; k < myNumCols; k++) { cout << setw(3) << ValAt(j,k) << " "; } cout << endl; } } bool Window::IsInBounds(int row, int col) const { return 0 <= row && row < myNumRows && 0 <= col && col < myNumCols; } void Window::ColorSquare(int ULrow, int ULcol, int N, int val) { int j,k; for(j=ULrow; j < ULrow + N; j++) { for(k=ULcol; k < ULcol + N; k++) { if (IsInBounds(j,k)) { myMat[j][k] = val; } } } } struct Rectangle { int ULrow; int ULcol; int numRows; int numCols; Rectangle(int r, int c, int nr, int nc) : ULrow(r), ULcol(c), numRows(nr), numCols(nc) { } }; void Enlarge(Window& w, Rectangle& rect, int factor) { int j,k; int rowIndex = rect.ULrow + (rect.numRows - 1) * factor; for(j=rect.ULrow + rect.numRows - 1; j >= rect.ULrow; j--) { int colIndex = rect.ULcol + (rect.numCols - 1) * factor; for(k=rect.ULcol + rect.numCols - 1; k >= rect.ULcol; k--) { if (w.IsInBounds(j,k)) { w.ColorSquare(rowIndex,colIndex,factor, w.ValAt(j,k)); } colIndex -= factor; } rowIndex -= factor; } } int Window::ValAt(int row, int col) const { return myMat[row][col]; } int main() { const int rows = 10; const int cols = 11; Window w(rows, cols); int j,k; for(j=0; j < rows; j++) { for(k=0; k < cols; k++) { w.ColorSquare(j,k,1,(j+1)*10); } } w.print(); /** w.ColorSquare(2,1,3,66); w.print(); w.ColorSquare(2,4,3,77); w.print(); **/ w.ColorSquare(2,1,1,55); w.ColorSquare(2,2,1,99); w.ColorSquare(2,3,1,33); w.ColorSquare(2,4,1,66); w.ColorSquare(3,1,1,22); w.ColorSquare(3,2,1,88); w.ColorSquare(3,3,1,77); w.ColorSquare(3,4,1,44); w.print(); Rectangle r(2,1,2,4); Enlarge(w,r,3); w.print(); return 0; }