c++ GraphX.cpp /* GraphX.hpp * Interface for the GraphX class * This is a simple
ID: 3691235 • Letter: C
Question
c++
GraphX.cpp
/* GraphX.hpp
* Interface for the GraphX class
* This is a simple user interface that will allow students to utilize
* simple graphics operations in C++ labs. It is mapped onto the Xlib
* graphical library.
*
* Fall 2012 Authors:
* Steve Jensen
* Hengyi Huang
* Spring 2014 Authors:
* Steve Jensen
* Tom Postler
*
* Version 14.04.18
*/
#ifndef GRAPHX_HPP
#define GRAPHX_HPP
#include
#include
using namespace std;
class GraphX
{
public:
// constructors
GraphX(); // default: create a 800 pixel wide x 600 pixel high window
GraphX(int w, int h); // create a w-pixel wide x h-pixel high window
~GraphX(); // destructor: close the x-server connection and remove window
// drawing primitives
void setpos(int x, int y); // move the pen location to x,y
void setposition(int x, int y); // ^^
void setx(int x); // move the pen location to x, unchanged y
void sety(int y); // move the pen location to unchanged x, y
void plot(); // plot current symbol at pen location
void erase(); // erase current symbol at pen location
void penup() {pendn = false;} // pendown state = false
void pu(){penup();} // ^^
void up(){penup();} // ^^
void pendown(){pendn = true;} // pendown state = true
void pd(){pendown();} // ^^
void down(){pendown();} // ^^
void clear(); // fill window with background color
void clearscreen(){clear();} // ^^
void reset(){clear();} // ^^
void resetscreen(){clear();} // ^^
void delay(double secs) // delay start of next GraphX operation
{delaytime = secs;}
void screensize(int x, int y); // clear and resize the GraphX window
void screensize(int x, int y, string c) // ^^ and change color
{backgroundcolor(c); screensize(x,y);}
// appearance
void pencolor(string); // set pen color
void pensize(int); // set pen size (in pixels)
void width(int w){pensize(w);} // ^^
void penstyle(string); // solid line, dashes, dots, dash-dot
void symbol(string); // set the plot symbol
void symbolsize(int); // set the size of the plotted symbol
void backgroundcolor(string); // set background color
// scaling
void scale(int width, int height); // set pixel units for x & y
void origin(int x, int y); // set the origin to x units, y units
void xaxis( ); // draw a line through the x axis
void yaxis( ); // draw a line through the y axis
// status
bool isopen( ){return openstate;} // return state of graphics environment
bool fail(){return !openstate;} // return status of last operation
bool isdown(){return pendn;} // return pen state (down = true)
int xcor(){return xloc;} // return current pen location
int ycor(){return yloc;} // return current pen location
void position(int &x, int &y); // return current pen x,y location
void pos(int &x, int &y); // ^^
int pensize(){return psize;} // return pen size (in pixels)
int width(){return pensize();} // ^^
void getscreensize(int &x, int &y) // return the current screen size
{x = winwidth; y = winheight;}
int delay(){return delaytime;} // return delay between drawing ops
// extended drawing
void moverel(int dx, int dy); // move relative to current location
void plot(int x, int y); // combine 'move' and 'plot' operations
void plot(string); // set current symbol and plot it
void plot(int,int,string); // combine 'move' and 'plot(symbol')
// input
string input(); // return input status
char eventkey(); // return key event value
void eventxy(int &x, int &y); // return x,y (scaled appropriately)
private:
enum GXColor {BLACK=0, WHITE=1, RED=2, YELLOW=3, BLUE=4, GREEN=5, ORANGE=6,
PURPLE=7};
enum GXLine {SOLID, DOTS, DASH, LONGDASH, DOTDASH};
enum GXSym {CIRCLE, SQUARE, DIAMOND, TRIANGLE, INVTRIANGLE,
SOLIDCIRCLE, SOLIDSQUARE, SOLIDDIAMOND, SOLIDTRIANGLE,
SOLIDINVTRIANGLE};
enum GXEvent {NONE=0, KEYPRESS=1, LEFTBUTTON=2, RIGHTBUTTON=3};
bool openstate; // window is opened and OK to use
int winwidth; // width of window in pixels
int winheight; // height of window in pixels
int xloc; // current pen location (scale units)
int yloc; // current pen location (scale units)
double xscale; // pixels per x unit
double yscale; // pixels per y unit
int xorigin; // optional xorigin value (scale units)
int yorigin; // optional yorigin value (scale units)
double delaytime; // delay next operation by (in microseconds)
GXColor bgcolor; // current background color
int psize; // pen size
GXColor pcolor; // pen color
GXLine pstyle; // pen style
GXSym psymbol; // plotting symbol
int symsize; // size (in pixels) of the plotted symbol
bool pendn; // pen 'down' state (true/false)
GXEvent lastinput; // most recent keyboard/mouse event
char kval; // last keyboard event: key value
int buttonx; // last button event: x location
int buttony; // last button event: y location
//Xlib related data
Display *disp; // the display pointer
int screen; // default screen identifier
Window win; // the main window (added)
XEvent event; // event reporting structure
GC gc; // graphics context for drawing
unsigned long pixelvalues[8]; // color pixel values (mapped at start)
//Private class functions
void wait(); // delay a graphics operation
void setDefaults(); // initialize all the default values
bool openX(int, int); // create and initialize environment
void drawSymbol(GXColor); // draw current symbol
void setLineGC(GXColor); // set the Xlib GC information for a line
int mapX(int); // return pixel offset given x location
int mapY(int); // return pixel offset given y location
GXColor getColor(string); // string to GXColor conversion
GXLine getLine(string); // string to GXLine conversion
GXSym getSym(string); // string to GXSym conversion
};
#endif
sample.cpp
#include
#include
#include "GraphX.hpp" // look here for a list & description of functions
using namespace std;
const int SIZE = 400;
int main()
{
int xpos = 0; // left
int ypos = 0; // bottom
string input;
GraphX mywin; // the class that does the window for us
mywin.scale(SIZE, SIZE); // size of window
mywin.symbol("solidcircle"); // type of thing to draw
mywin.symbolsize(30); // size of thing to draw
mywin.pencolor("black"); // color of thing to draw
input = mywin.input(); // input is from mouse or keyboard
while(input != "rightbutton") // stop when we right click
{
mywin.setpos(xpos, ypos); // look at this point
mywin.plot(); // draw it (at point set by setpos())
mywin.delay(0.02); // give you time to see it
mywin.erase(); // delete thing here (at point set by setpos())
xpos += 2; // move to the right
ypos += 1; // move up
// confine x/ypos to 400x400 square
xpos = xpos%SIZE;
ypos = ypos%SIZE;
input = mywin.input(); // did we right click yet?
}
return 0;
}
there is a link to download the GraphX.o
http://www-users.cselabs.umn.edu/classes/Spring-2016/csci1113/labs/lab12/GraphX.o
Workout Now that you are able to plot and draw using GraphX, you are ready to tackle the Forest Fire simulation. This is a discrete simulation that will use basic object-oriented techniques to model trees and forests and visualize the progression of the fire using graphical output.Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "tree.h"
#include "forest.h"
#include "oak.h"
#include "pine.h"
using namespace std;
int main()
{
double pc, wetness, percent = 0, avg;
int y;
Tree normaltree;
Oak oaktree;
Pine pinetree;
Tree* treePtr;
/*for(int i = 1; i < 11; i++)
{
for(int j = 1; j< 20; j++)
{
green.setGrid(i, j, 0.8, 1, 2.0, 1);
}
}*/
cout << "What is the probability catch?: ";
cin >> pc;
cout << "What is the wetness factor?: ";
cin >> wetness;
cout << "How many simulations you want?: ";
cin >> y;
for(int n = 0; n < y; n++)
{
Forest green;
for(int i = 1; i < 20; i++)
{
for(int j = 1; j< 20; j++)
{
green.setGrid(i, j, pc, 1, wetness, 1);
}
}
green.setGrid(10, 10, 1.0, 2, 1.0, 1);
while(green.isBurning() == true)
{
//usleep(500000);
green.regrowth(0.3);
green.applyNextStatus();
cout<< green << endl;
}
percent += green.percentTrees();
}
avg = percent / y;
cout << "Average percentage of unburning tree: " << avg << endl;
return 0;
}
tree.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "tree.h"
using namespace std;
double Tree::getProbCatch()
{
return probCatch;
}
void Tree::setProbCatch(double x)
{
probCatch = x;
}
int Tree::getStatus()
{
return status;
}
void Tree::setStatus(int x)
{
status = x;
}
void showSymbol(ostream& output);
double Tree::getWetness()
{
return wetness;
}
void Tree::setWetness(double x)
{
wetness = x;
}
int Tree::getBurnTime()
{
return burnTime;
}
void Tree::setBurnTime(int x)
{
burnTime = x;
}
ostream& operator << (ostream& output, Tree& arg)
{
output << arg.type << endl << arg.probCatch << endl << arg.status << endl << arg.wetness << endl << arg.burnTime << endl;
return output;
}
void Tree::showSymbol(ostream& output)
{
output << "[33;40m!" << " ";
}
tree.h
#ifndef tree_h
#define tree_h
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;
class Tree
{
public:
Tree():type("not a tree"), probCatch(1.0), status(0), wetness(1.0), burnTime(1){}
Tree(string a, double b, int c, double d, int e): type(a), probCatch(b), status(c), wetness(d), burnTime(e){}
double getProbCatch();
void setProbCatch(double x);
int getStatus();
void setStatus(int x);
double getWetness();
void setWetness(double x);
int getBurnTime();
void setBurnTime(int x);
friend ostream& operator << (ostream& output, Tree& arg);
virtual void showSymbol(ostream& output);
private:
string type;
double probCatch;
int status;
double wetness;
int burnTime;
};
#endif
pine.h
#ifndef pine_h
#define pine_h
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "tree.h"
using namespace std;
class Pine: public Tree
{
public:
Pine(){}
Pine(string a, double b, int c, double d, int e): Tree(a, b, c, d, e){}
void showSymbol(ostream& output){ output << "[35;40m^" << " ";}
};
#endif
oak.h
// nguy1952 Danh Nguyen
// leixx080 Rong Lei
#ifndef oak_h
#define oak_h
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "tree.h"
using namespace std;
class Oak: public Tree
{
public:
Oak(){}
Oak(string a, double b, int c, double d, int e): Tree(a, b, c, d, e){}
void showSymbol(ostream& output){ output << "[34;40m@" << " ";}
};
#endif
forest.h
#ifndef forest_h
#define forest_h
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "tree.h"
using namespace std;
class Forest
{
public:
Forest();
// ~Forest();
int nextStatus(int i, int j);
void applyNextStatus();
friend ostream& operator << (ostream& output, Forest& arg);
bool isBurning();
void setGrid(int i, int j, double prob, int stat, double wet, int burn);
void lightning(double a);
double percentTrees();
void regrowth(double growprob);
private:
Tree* grid[21][21];
};
#endif
forest.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include "forest.h"
#include "tree.h"
#include "pine.h"
#include "oak.h"
using namespace std;
Forest::Forest()
{
srand(time(0));
for(int j = 0; j < 21; j++)
{
grid[j][0] = new Tree("not a tree", 0.0, 0, 1.0, 1);
grid[j][20] = new Tree("not a tree", 0.0, 0, 1.0, 1);
grid[0][j] = new Tree("not a tree", 0.0, 0, 1.0, 1);
grid[20][j] = new Tree("not a tree", 0.0, 0, 1.0, 1);
}
for(int n = 1; n < 20; n++)
{
for(int m = 1; m < 20; m++)
{
if (m % 2 == 0)
{
grid[n][m] = new Pine("pine" , .5 , 1 , 1.0 , 1);
}
else
{
grid[n][m] = new Oak("oak" , .5 , 1 , 1.0 , 1);
}
}
}
grid[10][10] = new Tree("burning tree", 1.0, 2, 1.0, 1);
}
ostream& operator << (ostream& output, Forest& arg)
{
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 21; j++)
{
if(arg.grid[i][j]->getStatus() == 0)
output << "[0;40m " << " ";
if(arg.grid[i][j]->getStatus() == 1)
arg.grid[i][j]->showSymbol(output);
if(arg.grid[i][j]->getStatus() == 2)
output << "[91;40m#" << " ";
if(arg.grid[i][j]->getStatus() == 3)
output << "[0;40m^" << " ";
}
output << endl;
}
return output;
}
int Forest::nextStatus(int i, int j)
{
if(grid[i][j]->getStatus() == 3)
{
return 3;
}
if(grid[i][j]->getStatus() == 0 || grid[i][j]->getStatus() == 2)
{
return 0;
}
if(grid[i][j]->getStatus() == 1)
{
double xval;
xval = static_cast<double>(rand()) / (RAND_MAX);
if((grid[i + 1][j]->getStatus() == 2) || (grid[i - 1][j]->getStatus() == 2) || (grid[i][j + 1]->getStatus() == 2 )|| (grid[i][j - 1]->getStatus() == 2 ))
{
if (grid[i][j]->getProbCatch()/grid[i][j]->getWetness() > xval)
{
return 2;
}
else
return 1;
}
else
return 1;
}
//return grid[i][j].getStatus();
}
void Forest::applyNextStatus()
{
Tree newGrid[21][21];
for(int i = 0 ; i < 21 ; i++)
{
for (int j = 0 ; j < 21 ; j++)
{
newGrid[i][j] = *grid[i][j];
}
}
for(int i = 1; i < 20; i++)
{
for(int j = 1; j < 20; j++)
{
newGrid[i][j].setStatus(nextStatus(i , j));
//grid[i][j] = newGrid[i][j];
}
}
for(int i = 0 ; i < 21 ; i++)
{
for (int j = 0 ; j < 21 ; j++)
{
*grid[i][j] = newGrid[i][j];
}
}
}
bool Forest::isBurning()
{
for(int i = 1 ; i < 20 ; i++)
{
for(int j = 1; j < 20 ; j++)
{
if (grid[i][j]->getStatus() == 2)
{
return true;
}
}
}
return false;
}
void Forest::setGrid(int i, int j, double prob, int stat, double wet, int burn)
{
grid[i][j]->setProbCatch(prob);
grid[i][j]->setStatus(stat);
grid[i][j]->setWetness(wet);
grid[i][j]->setBurnTime(burn);
}
void Forest::lightning(double a)
{
for(int i = 1; i < 20; i++)
{
for(int j = 1; j< 20; j++)
{
if(grid[i][j]->getStatus() == 1)
{
double pval;
pval = static_cast<double>(rand()) / (RAND_MAX);
if (pval < a)
{
grid[i][j]->setStatus(2);
return;
}
else
grid[i][j]->setStatus(1);
}
else
grid[i][j]->setStatus(1);
}
}
}
double Forest::percentTrees()
{
double x;
for(int i = 0; i < 21; i++)
{
for(int j = 0; j < 21; j++)
{
if(grid[i][j]->getStatus() == 1)
{
x++;
}
}
}
return x / 441 * 100;
}
void Forest::regrowth(double growprob)
{
for(int i = 1; i < 20; i++)
{
for(int j = 1; j< 20; j++)
{
if(grid[i][j]->getStatus() == 0)
{
double pval;
pval = static_cast<double>(rand()) / (RAND_MAX);
if (pval < growprob)
{
grid[i][j]->setStatus(3);
}
else
grid[i][j]->setStatus(0);
}
}
}
}
sample output
What is the probability catch?: 5
What is the wetness factor?: 9
How many simulations you want?: 2
@ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @
@ ^ @ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^
@ ^ @ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @
@ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^ ^ ^ ^ @ ^ @
@ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @
@ ^ @ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^
@ ^ @ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @
@ ^ ^ ^ ^ ^ ^ ^ @ ^ ^ ^ ^ ^ ^ ^ @ ^ @
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ ^ ^ @ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @
@ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @ ^ @
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @
@ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @
@ ^ ^ ^ ^ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ ^ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ ^ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
@ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @
Average percentage of unburning tree: 58.9569
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.