From 3a3132e44a0017ddfd4ebc31eb9a9c3b08f5bea8 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 24 Jun 2011 10:18:31 -0400 Subject: [PATCH] added a score value and some docs to Graph class --- graph.cpp | 4 ++-- graph.h | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/graph.cpp b/graph.cpp index f427b79..ea53c07 100644 --- a/graph.cpp +++ b/graph.cpp @@ -71,9 +71,9 @@ bool Graph::crosses_edge(Edge e) } -bool Graph::add_vertex(int x, int y, int r, int colour, Vertex* src) +bool Graph::add_vertex(int x, int y, int r, int colour, int score, Vertex* src) { - Vertex* v = new Vertex(x, y, r, colour); + Vertex* v = new Vertex(x, y, r, colour, score); // Make sure the nodes won't overlap if (vertex_would_overlap(v->x, v->y, v->r)) diff --git a/graph.h b/graph.h index 22d738b..04b3151 100644 --- a/graph.h +++ b/graph.h @@ -1,7 +1,17 @@ /* Represents an undirected graph. * Also contains the vertex and edge classes - * vertexes know their center point on the SDL Surface - - * bad decoupling maybe, but not too bad, all things considered + * These are not quite traditional graph theory graphs, because they also have + * cartesian coordinates, and can do some math to ensure planarity is preserved + * (that is, planarity in the existant layout, not being isomorphic to a + * planar) + * + * Each vertex also contains a 'colour' and a 'score', which are just integers + * but may be useful for various algorithms + * + * fixme: this isn't a terribly *efficient* setup - Vertices don't know about + * their neighbors, because the 'edge' class handles that instead. This makes + * walking the tree O(n^2) or so, which is crappy. However, this implementation + * is *simple* and will do well for small-to-medium graphs, I suspect */ #ifndef _GRAPH_H_ @@ -14,18 +24,20 @@ using std::list; class Vertex { public: - Vertex(int x, int y, int r, int colour = 0x000000); + Vertex(int x, int y, int r, int colour = 0, int score = 0); int x; int y; int r; int colour; + int score; }; struct Edge { Vertex* a; Vertex* b; + int score; }; class Graph @@ -39,20 +51,19 @@ class Graph bool point_in_vertex(int x, int y, int size); Vertex * vertex_at(int x, int y); - virtual bool add_vertex(int x, int y, int r, int colour, Vertex* src=NULL); + virtual bool add_vertex(int x, int y, int r, int colour = 0, int score = 0, + Vertex* src=NULL); bool is_planar() const { return planar; } void set_planar(bool planar) { this->planar = planar; } - private: - bool vertex_would_overlap(int x, int y, int r); - bool crosses_edge(Edge e); - protected: list vertices; list edges; private: + bool vertex_would_overlap(int x, int y, int r); + bool crosses_edge(Edge e); bool planar; };