// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // House.cpp // // Simple example of a class and its use /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // Enter project specific include files here as well as classes and functions // that you choose to define in the main shell rather than in separate files /////////////////////////////////////////////////////////////////////// // Define a class house that represents a house // The base of the house is a rectangle // The roof is pecified by its height // Draw function paints black base with a picture window and a red roof class House{ short x1; // left side short y1; // top of the base short x2; // right side short y2; // the bottom short height; // roof height // make sure that the house fits inside the graphics window House& Verify(){ if (x1 < 0) // check the left side x1 = 0; if (x2 > 300) // check the right side x2 = 300; if (y1 < 0) // check the top of the house base y1 = 0; if (y2 > 300) // check the bottom of the house y2 = 300; if (y1 - height < 0) // check the roof height = y1; return *this; }; public: // default constructor - makes a big house House(){ x1 = 0; y1 = 100; x2 = 300; y2 = 300; height = 100; }; // use four coordinates and the roof height to set the house data House& Set(short X1, short Y1, short X2, short Y2, short Height){ x1 = X1; y1 = Y1; x2 = X2; y2 = Y2; height = Height; return Verify(); // modify data so the house fits in // and return the 'new' house object }; // use two points and the roof height to set the house data House& Set(const PointData& TL, const PointData& BR, short Height){ TL.Get(x1, y1); // Top Left point of the base BR.Get(x2, y2); // Bottom Right point of the base height = Height; // roof height Verify(); // modify data so the house fits in return *this; // and return the 'new' house object }; // use four coordinates and the roof height to construct the house data House(short X1, short Y1, short X2, short Y2, short Height){ Set(X1, Y1, X2, Y2, Height); }; // use two points and the roof height to construct the house data House(const PointData& TL, const PointData& BR, short Height){ Set(TL, BR, Height); }; // return two points: top left and bottom right void GetBase(PointData& TL, PointData& BR){ TL.Set(x1, y1); // use Set function in class PointData BR.Set(x2, y2); // use Set function in class PointData } // paint the house // function will be defined outside of the class definition // only the prototype is declared here const House& Draw() const; }; // END of class House definition // definition of the member function Draw for the class House const House& House::Draw() const{ // paint the base SetForeColor(0, 0, 0); // the house is black PaintRect(x1, y1, x2, y2); // paint the window int dw = (x2 - x1)/4; int dh = (y2 - y1)/4; EraseRect(x1 + dw, y1 + dh, x2 - dw, y2 - dh); // paint the roof SetForeColor(255, 0, 0); // the roof is red PolygonWidget PW; PW.ChangeMemory(4).SetFill(); PW.Append(x1, y1); // left base of the roof PW.Append(x2, y1); // right base of the roof PW.Append((x1+x2)/2, y1-height); // top of the roof PW.ClosePolygon().Draw(); return *this; }; void RowVillage(){ // paint a row of random houses adjacent to each other cout << "Painting a row of houses." << endl; House H; SetRandomSeed(); int cnt = 0; // count number of houses int x = 0; // left side int width = RandomLong(30,60); // house width while ((x+width) <= 300){ // check if the house fits in int y = RandomLong(150,250); // top of the house base int roof = RandomLong(20, 60); // roof height cnt++; // count this house H.Set(x, y, x+width, 300, roof).Draw(); // draw the house x += width; // next house to the right int width = RandomLong(30,60); // new width to see if it fits PressReturn("Next house"); }; cout << "We painted " << cnt << " houses." << endl; }; void RandomVillage(){ // paint ten random houses cout << "Painting ten random houses." << endl; int i; House H; int x1; // left side int width; // house width int y1; // top of the house base int houseHeight; // house height int roofHeight; // roof height int cnt = 0; for (i = 0; i < 10; i++){ // select the house size and location x1 = RandomLong(0,250); y1 = RandomLong(50,300); width = RandomLong(30,120); houseHeight = RandomLong(30, 100); roofHeight = RandomLong(20, 60); // paint the house H.Set(x1, y1, x1+width, y1+houseHeight, roofHeight).Draw(); }; }; int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); ////////// // Enter the main program here // Build one house, paint it, change it and paint it again House H; H.Draw(); PressReturn("Next house"); ClearDrawing(); // Reset the house data using two points and the height PointData A; // new top left corner PointData C; // new bottom right corner PointData OldTL; PointData OldBR; H.GetBase(OldTL, OldBR); // retrieve old coordinates A.Request("Top left point:", OldTL); // new top left C.Request("Bottom right point:", OldBR);// new bottom right H.Set(A, C, 50).Draw(); // build and draw the house PressReturn("Big House again"); H.Set(OldTL, OldBR, 100).Draw(); // Paint a row of houses PressReturn("Row houses"); ClearDrawing(); RowVillage(); // Paint ten random houses (some may not fit in completely) PressReturn("Random houses"); ClearDrawing(); RandomVillage(); ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; }