added a score value and some docs to Graph class
This commit is contained in:
parent
6560b1c28b
commit
3a3132e44a
|
@ -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
|
// Make sure the nodes won't overlap
|
||||||
if (vertex_would_overlap(v->x, v->y, v->r))
|
if (vertex_would_overlap(v->x, v->y, v->r))
|
||||||
|
|
27
graph.h
27
graph.h
|
@ -1,7 +1,17 @@
|
||||||
/* Represents an undirected graph.
|
/* Represents an undirected graph.
|
||||||
* Also contains the vertex and edge classes
|
* Also contains the vertex and edge classes
|
||||||
* vertexes know their center point on the SDL Surface -
|
* These are not quite traditional graph theory graphs, because they also have
|
||||||
* bad decoupling maybe, but not too bad, all things considered
|
* 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_
|
#ifndef _GRAPH_H_
|
||||||
|
@ -14,18 +24,20 @@ using std::list;
|
||||||
class Vertex
|
class Vertex
|
||||||
{
|
{
|
||||||
public:
|
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 x;
|
||||||
int y;
|
int y;
|
||||||
int r;
|
int r;
|
||||||
int colour;
|
int colour;
|
||||||
|
int score;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Edge
|
struct Edge
|
||||||
{
|
{
|
||||||
Vertex* a;
|
Vertex* a;
|
||||||
Vertex* b;
|
Vertex* b;
|
||||||
|
int score;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Graph
|
class Graph
|
||||||
|
@ -39,20 +51,19 @@ class Graph
|
||||||
|
|
||||||
bool point_in_vertex(int x, int y, int size);
|
bool point_in_vertex(int x, int y, int size);
|
||||||
Vertex * vertex_at(int x, int y);
|
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; }
|
bool is_planar() const { return planar; }
|
||||||
void set_planar(bool planar) { this->planar = 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:
|
protected:
|
||||||
list<Vertex*> vertices;
|
list<Vertex*> vertices;
|
||||||
list<Edge> edges;
|
list<Edge> edges;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool vertex_would_overlap(int x, int y, int r);
|
||||||
|
bool crosses_edge(Edge e);
|
||||||
bool planar;
|
bool planar;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user