#include #include #include "apmatrix.h" // apap10-1-6.cpp // Richard K. Johnson // 3/18/99 // Programming assignments apap10.1 #6 // Modified 3.28.00 - Green Zhang const int ROWS = 8; const int COLS = 8; enum MyColorType {white, black}; typedef apmatrix ScreenType; struct Pt { int xCoord; int yCoord; }; struct Rectangle { Pt upperLeft; Pt lowerRight; }; void DoFillRect(); // post: Creates and fills the screen with all w's // Displays the screen with all w's // Places a retangle of b's in the screen - might not be inclusive // Fills the rectangle with b's // Displays the screen with retangle filled with b's void InitRect(ScreenType & m); // pre: m is dimensioned // post: m is set to all w's void InsertRect(const Rectangle & r, ScreenType & m); // pre: m is a screen with all w's // r is a rectangle is, at least, partially inside the screen // post: Rectangle dimensions are set to b's in the screen void DisplayRect(const ScreenType & m); // pre: Screen is filled with w's and b's // b's make a retangle figure in or partly in screen // post: Screen is display void GetRect(Rectangle & r, const ScreenType & m); // pre: m is a screen with all w's // post: Returns the rectangle's upper left and lower right coordinates // which will, at least, be inside the screen m void GetXY(const Rectangle & r,int & x, int & y); // pre: Screen is dimensioned // post: Valid row and col values are returned that can be found in the screen void FillRect(ScreenType & m, int x, int y); // pre: // post: int main () { DoFillRect(); return 0; } void DoFillRect() // post: Creates and fills the screen with all w's // Displays the screen with all w's // Places a retangle of b's in the screen - might not be inclusive // Fills the rectangle with b's // Displays the screen with retangle filled with b's { ScreenType m(ROWS, COLS, white); Rectangle r; int x, y; InitRect(m); DisplayRect(m); GetRect(r, m); InsertRect(r, m); DisplayRect(m); GetXY(r, x, y); FillRect(m, x, y); DisplayRect(m); } void InitRect(ScreenType & m) // pre: m is dimensioned // post: m is set to all w's { for (int i=0; i= r.upperLeft.xCoord && j <= r.lowerRight.xCoord && j >= 0 && j < m.numcols())) m[i][j] = black; if ((j == r.upperLeft.xCoord || j == r.lowerRight.xCoord) && (i >= r.upperLeft.yCoord && i <= r.lowerRight.yCoord && i >= 0 && i < m.numrows())) m[i][j] = black; } } void GetRect(Rectangle & r, const ScreenType & m) // pre: m is a screen with all w's // post: Returns the rectangle's upper left and lower right coordinates // which will, at least, be inside the screen m { cout << "Enter upper left" << endl; do { cout << "\trow coord: "; cin >> r.upperLeft.yCoord; if (r.upperLeft.yCoord >= m.numrows()) cout << "\aToo far down." << endl; } while (r.upperLeft.yCoord >= m.numrows()); do { cout << "\tcolumn coord: "; cin >> r.upperLeft.xCoord; if (r.upperLeft.xCoord >= m.numcols()) cout << "\aToo far right." << endl; } while (r.upperLeft.xCoord >= m.numcols()); cout << "Enter lower right" << endl; do { cout << "\trow coord (> " << r.upperLeft.yCoord + 1 << "): "; cin >> r.lowerRight.yCoord; if (r.lowerRight.yCoord <= r.upperLeft.yCoord + 1) cout << "\aToo far up." << endl; } while (r.lowerRight.yCoord <= r.upperLeft.yCoord + 1); do { cout << "\tcolumn coord (> " << r.upperLeft.xCoord + 1 << "): "; cin >> r.lowerRight.xCoord; if (r.lowerRight.xCoord <= r.upperLeft.xCoord + 1) cout << "\aToo far left." << endl; } while (r.lowerRight.xCoord <= r.upperLeft.xCoord +1); cout << endl; } void GetXY(const Rectangle & r,int & row, int & col) // pre: Screen is dimensioned // post: Valid row and col values are returned that can be found in the screen { do { cout << "Enter the row Coordinate (" << r.upperLeft.yCoord << " < row < " << r.lowerRight.yCoord << "): "; cin >> row; if (row <= r.upperLeft.yCoord || row >= r.lowerRight.yCoord) cout << "\aInvalid Input" << endl; }while (row <= r.upperLeft.yCoord || row >= r.lowerRight.yCoord); do { cout << "Enter the column Coordinate (" << r.upperLeft.xCoord << " < column < " << r.lowerRight.xCoord << "): "; cin >> col; if (col <= r.upperLeft.xCoord || col >= r.lowerRight.xCoord) cout << "\aInvalid Input" << endl; }while (col <= r.upperLeft.xCoord || col >= r.lowerRight.xCoord); cout << endl; } void DisplayRect(const ScreenType & m) // pre: Screen is filled with w's and b's // b's make a retangle figure in or partly in screen // post: Screen is display { cout << setiosflags(ios::right); cout << " "; for (int i=0; i