/** * Aquarium Lab Series * Copyright (C) 2000 Alyce Brady * * Graphics implementation file for Display class -- see display.h * for details. * * Acknowledgements: * This class is part of the Aquarium Lab Series, which was inspired * by the AP Marine Biology Case Study found at * http://www.collegeboard.org/ap/computer-science/marine_biology/ * This Display class is NOT the same as the Display class in Part II * of the Case Study, but is loosely based on that class. * * This class uses the CMU Graphics Library, written at the Carnegie * Mellon University School of Computer Science and available from * Mark Stehlik's Advanced Placement web page: * http://www.cs.cmu.edu/~mjs/ * * License: * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ #include #include "display.h" #include "aquafish.h" // constructors Display::Display(window & w, Aquarium a) : myWindow (w), theAquarium (a), aquaLeft(100), aquaTop(150), widthScale(25), heightScale(20) // postcondition: ready to display an Aquarium or AquaFish // (text-based display) { // Don't make us click on an empty window at the end myWindow.SetWaitClose(false); } // modifier functions (modifiers because they change the state of the output) void Display::ShowAquarium() // postcondition: aquarium displayed graphically { // declare the ShowAquarium function (this code is temporary!) void TempShowAquarium (window & myWindow, Aquarium aqua); // call the ShowAquarium function (this code is also temporary!) TempShowAquarium(myWindow, theAquarium); } void Display::Show(AquaFish theFish) // postcondition: theFish displayed graphically { // Set pen and brush color. myWindow.SetPen(LIGHTSEAGREEN); myWindow.SetBrush(LIGHTSEAGREEN); // Where is this fish going to be drawn? int cellLeft = aquaLeft + widthScale * theFish.XCoord(); int cellTop = aquaTop + heightScale * theFish.YCoord(); // Where are the parts of the fish going to be drawn? This depends // on whether the fish is facing right or left. Since right- and // left-facing fish are symmetric around the middle, we determine // where to draw the parts of a fish relative to the middle. int bodyTailIntersectionX; // x-coordinate of body/tail intersection int noseX; // x-coordinate of front of fish int tailX; // x-coordinate of back of tail int eyeX; // x-coordinate of center of fish eye int cellMid = cellLeft + widthScale/2; // x-coord of middle of cell // Establish direction from the middle depending on fish direction. int dir = 1; // assume facing right if ( theFish.FacingLeft() ) dir = -1; // no! facing left! // Nose is 1/8 from "front" wall, back of tail is at "back wall." // The body and tail of the fish intersect 1/4 from "back wall." // The center of the eye is 5/16 from "front wall." Actual // distances below are computed from center, not from walls. noseX = cellMid + (dir * 3 * widthScale)/8; tailX = cellMid - (dir * widthScale)/2; bodyTailIntersectionX = cellMid - (dir * widthScale)/4; eyeX = cellMid + (dir * 3 * widthScale)/16; // Draw fish body myWindow.DrawEllipse( noseX, cellTop + heightScale/4, bodyTailIntersectionX, cellTop + (7*heightScale)/8, FILLED); // Draw tail myWindow.DrawTriangle( tailX, cellTop + heightScale/4, bodyTailIntersectionX, cellTop + heightScale/2, tailX, cellTop + (3*heightScale)/4, FILLED); // Draw an eye myWindow.SetPen(BLACK); myWindow.DrawCircle( eyeX, cellTop + heightScale/2, 1, FILLED); } void Display::Show(apvector school) // postcondition: all fish in school displayed graphically { for ( int i = 0; i < school.length(); i++ ) { Show (school[i]); } } void TempShowAquarium (window & myWindow, Aquarium theAquarium) { int aquaLeft=100, aquaTop=150, widthScale=25, heightScale=20; // We can derive the x-coordinate of the right wall // and the y-coordinate of the bottom of the aquarium. // int aquaRight = // int aquaBottom = }