The Gnode object now must hold information specific to our graphical environment. Look at the new constructor:
private:
Gnode(Pixel foreground_color_in,
Pixel background_color_in,
Pixel inside_color_in,
Pixel border_color_in,
Display *display_in,
Drawable drawable_in,
GC gc_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
float node_radius_in,
Vertex *node_center_in,
const char *name_in);
Note we have added a CIRCLE object for each node for visual display and a center and radius.
private:
CIRCLE *C;
Vertex node_center;
float node_radius;
We also add a new method to return the node center:
public:
Vertex *center(){return &node_center;};
The Gedge object must also be initialized with additional information.
private:
Gedge(Pixel foreground_color_in,
Pixel background_color_in,
Pixel inside_color_in,
Pixel border_color_in,
Display *display_in,
Drawable drawable_in,
GC gc_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
Gnode *from,
Gnode *to,
double weight);
There is also additional information added to the Gedge data fields:
private;
BOX *E;
The Graph object must include all of this graphical information in its data fields for passing to the node and edge constructors. Hence, we add the new elements:
private:
Pixel foreground_color;
Pixel background_color;
Pixel node_inside_color;
Pixel node_border_color;
Pixel edge_inside_color;
Pixel edge_border_color;
Display *display;
Drawable drawable;
GC gc;
Pixmap pixmap;
Pixmap pixmap2;
float node_radius;
Vertex node_center;
The new Graph constructor looks like:
public:
Graph(float radius_in,Vertex node_center_in, //node circle
Pixel in_foreground_color, //colors
Pixel in_background_color,
Pixel *graph_colors,
Display *display_in, //graphics
Drawable drawable_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
GC gc_in);
We must add many new methods for the manipulation and display of the visualization of the graph object.
public:
Graph& draw(Drawable D);
Graph& erase(Drawable D);
Graph& translate(int xoffset,int yoffset);
Graph& rotate(float axis_x,float axis_y,float angle);
Graph& scale(float scale_factor);
Graph& CenteredScale(float scale_factor);
Graph& ScaleTranslate(float scale_factor,int xoffset,int yoffset);
Graph& find_center(Vertex *V);
Graph& ReadInputFile(char *filename);
Graph& GenerateRandomGraph(char *filename);
};
The full definition file looks like this:
#include "node.h"
#include "edge.h"
#include "gnodeptrbag.h"
#include "gedgeptrbag.h"
#include "box.h"
#include "circle.h"
class Graph;
class Gedge;
class ostream;
class istream;
class Gnode : public Node{
friend Graph;
private:
GedgePtrBag d_edges;
Gnode(const Gnode&);
Gnode& operator=(const Gnode&);
Gnode(Pixel foreground_color_in,
Pixel background_color_in,
Pixel inside_color_in,
Pixel border_color_in,
Display *display_in,
Drawable drawable_in,
GC gc_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
float node_radius_in,
Vertex *node_center_in,
const char *name_in);
~Gnode();
void add(Gedge *edgeptr);
void remove(Gedge *edgeptr);
//add new data
CIRCLE *C;
Vertex node_center;
float node_radius;
public:
Vertex *center(){return &node_center;};
const GedgePtrBag& edges() const;
};
class Gedge : public Edge{
friend Graph;
private:
BOX *E;
Gnode *d_from_p;
Gnode *d_to_p;
Gedge(const Gedge&);
Gedge& operator=(const Gedge&);
Gedge(Pixel foreground_color_in,
Pixel background_color_in,
Pixel inside_color_in,
Pixel border_color_in,
Display *display_in,
Drawable drawable_in,
GC gc_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
Gnode *from,
Gnode *to,
double weight);
~Gedge();
public:
Gnode *from();
Gnode *to();
};
class Graph{
friend istream& operator>>(istream& in, Graph& A);
friend istream& operator>>(istream& in, Graph* A);
friend ostream& operator<<(ostream& out,const Graph& A);
friend ostream& operator<<(ostream& out,const Graph* A);
private:
private:
GnodePtrBag d_nodes;
GedgePtrBag d_edges;
Pixel foreground_color;
Pixel background_color;
Pixel node_inside_color;
Pixel node_border_color;
Pixel edge_inside_color;
Pixel edge_border_color;
Display *display;
Drawable drawable;
GC gc;
Pixmap pixmap;
Pixmap pixmap2;
float node_radius;
Vertex node_center;
Graph(const Graph&);
Graph& operator=(const Graph&);
public:
Graph(float radius_in,Vertex node_center_in, //node circle
Pixel in_foreground_color, //colors
Pixel in_background_color,
Pixel *graph_colors,
Display *display_in, //graphics
Drawable drawable_in,
Pixmap pixmap_in,
Pixmap pixmap2_in,
GC gc_in);
~Graph();
Gnode *addNode(const char *nodeName,float x,float y);
Gnode *findNode(const char *nodeName);
void removeNode(Gnode *node);
Gedge *addEdge(Gnode *from,Gnode *to,double weight);
Gedge *findEdge(Gnode *from,Gnode *to);
void removeEdge(Gedge *edge);
const GnodePtrBag& nodes() const;
const GedgePtrBag& edges() const;
istream& grab(istream& in);
ostream& print(ostream& out) const;
Graph& draw(Drawable D);
Graph& erase(Drawable D);
Graph& translate(int xoffset,int yoffset);
Graph& rotate(float axis_x,float axis_y,float angle);
Graph& scale(float scale_factor);
Graph& CenteredScale(float scale_factor);
Graph& ScaleTranslate(float scale_factor,int xoffset,int yoffset);
Graph& find_center(Vertex *V);
Graph& ReadInputFile(char *filename);
Graph& GenerateRandomGraph(char *filename);
};