#include #include #include "apmatrix.h" class Screen { public: enum Color {white, black}; // legal pixel values Screen(int size); // all white screen size X size void DrawRectangle(int row, int col, // draw rectangle on screen int height, int width); void FillRectangle(int row, int col); // fill a rectangle void Clear(); // set screen to all white void Print() const; int Size() const; private: int mySize; apmatrix myPixels; }; Screen::Screen(int size) : mySize(size), myPixels(size,size,white) { } Screen::DrawRectangle(int row, int col, int height, int width) // precondition: screen is blank/all white, // 0 <= row < Size() and 0 <= col < Size(), // 0 < height, and 0 < width { int lastCol = col + width - 1; int lastRow = row + height - 1; int k; if (lastCol >= Size()) { lastCol = Size() - 1; } if (lastRow >= Size()) { lastRow = Size() - 1; } for(k=col; k <= lastCol; k++) // top row { myPixels[row][k] = black; } if (row + height - 1 < Size()) // is bottom row on screen? { for(k=col; k <= lastCol; k++) // bottom row { myPixels[lastRow][k] = black; } } for(k=row; k <= lastRow; k++) // left column { myPixels[k][col] = black; } if (col + width - 1 < Size()) // is right col on screen? { for(k=row; k <= lastRow; k++) // right column { myPixels[k][lastCol] = black; } } } int Screen::Size() const { return mySize; } void Screen::Print(ostream & output) const { int j,k; int size = Size(); // make top border output << " "; for(j=0; j < size; j++) { output << " " << setw(1) << j << " "; } for(j=0; j < size; j++) { output << setw(1) << j << " "; for(k=0; k < size; k++) { if (myPixels[j][k] == white) { output << " - "; } else { output << " * "; } } output << endl; } } void Screen::Clear() { int j,k; int size = Size(); for(j=0; j < size; j++) { for(k=0; k < size; k++) { myPixels[j][k] = white; } } } void Screen::FillRectangle(int row, int col) { if (0 <= row && row < Size() && 0 <= col && 0 < Size() && myPixels[row][col] == white) { myPixels[row][col] = black; FillRectangle(row-1,col); // above FillRectangle(row+1,col); // below FillRectangle(row,col-1); // left FillRectangle(row,col+1); // right } } int main() { Screen s(8); s.Print(cout); s.DrawRectangle(2,3,4,5); s.Print(cout); s.Clear(); s.DrawRectangle(5,4,10,5); s.Print(cout); s.Clear(); s.DrawRectangle(5,5,8,9); s.Print(cout); s.Clear(); s.DrawRectangle(2,3,4,5); s.FillRectangle(4,5); s.Print(cout); s.Clear(); s.DrawRectangle(5,4,10,5); s.FillRectangle(7,7); s.Print(cout); s.Clear(); s.DrawRectangle(5,5,8,9); s.FillRectangle(7,9); s.Print(cout); s.Clear(); }