Removed the concept of edges in favor of a proper tree-style approach, with each vertex knowing about its neighbors. This seems to have broken both Grah::crosses_edge() and Graph::remove_vertex. and the move radius circle doesn't draw any more. Go team.

This commit is contained in:
Anna Rose 2011-06-27 18:10:24 -04:00
parent ed0d8c5e26
commit 07fc8c67c1
5 changed files with 53 additions and 120 deletions

View File

@ -52,6 +52,7 @@ bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
return true; return true;
} }
#include "debug.h"
void DrawUtils::draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour) void DrawUtils::draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour)
{ {
float dx, dy, len; float dx, dy, len;

View File

@ -134,7 +134,7 @@ float GameData::calculate_strength(Vertex* node)
float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited) float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list<Vertex*>& visited)
{ {
// Find which vertices we need to visit from here // Find which vertices we need to visit from here
list<Vertex*> neighbors = get_neighbors(node); list<Vertex*> neighbors = node->neighbors;
list<Vertex*> to_visit; list<Vertex*> to_visit;
visited.push_back(node); visited.push_back(node);
@ -184,7 +184,7 @@ int GameData::get_range(Vertex* node)
else if (mode == MODE_ATTACK) else if (mode == MODE_ATTACK)
{ {
int range = 200; int range = 200;
list<Vertex*> neighbors = get_neighbors(node); list<Vertex*> neighbors = node->neighbors;
for(list<Vertex*>::iterator cursor = neighbors.begin(); for(list<Vertex*>::iterator cursor = neighbors.begin();
cursor != neighbors.end(); cursor++) cursor != neighbors.end(); cursor++)

113
graph.cpp
View File

@ -61,15 +61,21 @@ bool Graph::vertex_would_overlap(int x, int y, int r)
} }
bool Graph::crosses_edge(Edge e) bool Graph::crosses_edge(Vertex* a, Vertex* b)
{ {
for (list<Edge>::iterator cursor = edges.begin(); for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != edges.end(); cursor++) cursor != vertices.end(); cursor++)
{ {
Edge c = *cursor; Vertex* v = *cursor;
if (MathUtils::lines_intersect(c.a->x, c.a->y, c.b->x, c.b->y, for (list<Vertex*>::iterator subcursor = v->neighbors.begin();
e.a->x, e.a->y, e.b->x, e.b->y)) subcursor != v->neighbors.end(); subcursor++)
return true; {
Vertex* w = *cursor;
if (MathUtils::lines_intersect(a->x, a->y, b->x, b->y,
v->x, v->y, w->x, w->y))
return true;
}
} }
return false; return false;
@ -92,20 +98,17 @@ bool Graph::add_vertex(int x, int y, int r, int colour, int score, Vertex* src)
if (src != NULL) if (src != NULL)
{ {
Edge e; if (planar && crosses_edge(v, src))
e.a = src;
e.b = v;
if (planar && crosses_edge(e))
{ {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", e.a->x, e.a->y, e.b->x, e.b->y); fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", v->x, v->y, src->x, src->y);
#endif #endif
delete v; delete v;
return false; return false;
} }
edges.push_back(e); v->neighbors.push_back(src);
src->neighbors.push_back(v);
} }
vertices.push_back(v); vertices.push_back(v);
@ -133,58 +136,24 @@ list<Vertex*> Graph::get_colour(int colour)
} }
list<Edge> Graph::get_edges(Vertex* v)
{
list<Edge> answer;
for (list<Edge>::iterator cursor = edges.begin();
cursor != edges.end(); cursor++)
{
Edge e = *cursor;
if (e.a == v || e.b == v) answer.push_back(e);
}
return answer;
}
list<Vertex*> Graph::get_neighbors(Vertex* v)
{
list<Vertex*> answer;
for (list<Edge>::iterator cursor = edges.begin();
cursor != edges.end(); cursor++)
{
Edge e = *cursor;
if (e.a == v) answer.push_back(e.b);
else if (e.b == v) answer.push_back(e.a);
}
return answer;
}
void Graph::remove_vertex(Vertex* target) void Graph::remove_vertex(Vertex* target)
{ {
list<Edge> dead_edges = get_edges(target); list<Vertex*>::iterator cursor;
for (list<Edge>::iterator cursor = dead_edges.begin(); for (cursor = target->neighbors.begin(); cursor != target->neighbors.end();
cursor != dead_edges.end(); cursor++) cursor++)
{ {
Edge e = *cursor; Vertex* neighbor = *cursor;
list<Edge>::iterator to_del = find(edges.begin(), edges.end(), e); list<Vertex*>::iterator subcursor = find(target->neighbors.begin(),
target->neighbors.end(),
if (to_del != edges.end()) target);
{ assert(subcursor != target->neighbors.end());
edges.erase(to_del); target->neighbors.erase(subcursor);
}
} }
list<Vertex*>::iterator to_del = find(vertices.begin(), vertices.end(), cursor = find(vertices.begin(), vertices.end(), target);
target); assert(cursor != vertices.end());
vertices.erase(cursor);
assert(to_del != vertices.end());
vertices.erase(to_del);
delete target; delete target;
} }
@ -197,27 +166,3 @@ Vertex::Vertex(int x, int y, int r, int colour, int score)
this->colour = colour; this->colour = colour;
this->score = score; this->score = score;
} }
Edge::Edge()
{
a = NULL;
b = NULL;
score = 0;
}
Edge::Edge(Vertex* a, Vertex* b, int score)
{
this->a = a;
this->b = b;
this->score = score;
}
bool Edge::operator==(const Edge e) const
{
return (this->a == e.a &&
this->b == e.b &&
this->score == e.score);
}

23
graph.h
View File

@ -1,5 +1,5 @@
/* Represents an undirected graph. /* Represents an undirected graph.
* Also contains the vertex and edge classes * Also contains the vertex class
* These are not quite traditional graph theory graphs, because they also have * These are not quite traditional graph theory graphs, because they also have
* cartesian coordinates, and can do some math to ensure planarity is preserved * cartesian coordinates, and can do some math to ensure planarity is preserved
* (that is, planarity in the existant layout, not being isomorphic to a * (that is, planarity in the existant layout, not being isomorphic to a
@ -31,21 +31,10 @@ class Vertex
int r; int r;
int colour; int colour;
int score; int score;
list<Vertex*> neighbors;
}; };
class Edge
{
public:
Edge();
Edge(Vertex* a, Vertex* b, int score = 0);
Vertex* a;
Vertex* b;
int score;
bool operator==(const Edge e) const;
bool operator!=(const Edge e) const { return !(*this == e); }
};
class Graph class Graph
{ {
@ -54,10 +43,7 @@ class Graph
virtual ~Graph(); virtual ~Graph();
list<Vertex*> get_vertices() const { return vertices; } list<Vertex*> get_vertices() const { return vertices; }
list<Edge> get_edges() const { return edges; }
list<Vertex*> get_colour(int colour); list<Vertex*> get_colour(int colour);
list<Edge> get_edges(Vertex* v);
list<Vertex*> get_neighbors(Vertex* v);
bool point_in_vertex(int x, int y); bool point_in_vertex(int x, int y);
Vertex * vertex_at(int x, int y); Vertex * vertex_at(int x, int y);
@ -70,11 +56,10 @@ class Graph
protected: protected:
list<Vertex*> vertices; list<Vertex*> vertices;
list<Edge> edges;
private: private:
bool vertex_would_overlap(int x, int y, int r); bool vertex_would_overlap(int x, int y, int r);
bool crosses_edge(Edge e); bool crosses_edge(Vertex* a, Vertex* b);
bool planar; bool planar;
}; };

View File

@ -49,31 +49,33 @@ void SDLRenderer::render(GameData& data)
break; break;
} }
// Background image first
DrawUtils::draw(display, background, 0, 0); DrawUtils::draw(display, background, 0, 0);
list<Vertex*> vertices = data.get_vertices(); list<Vertex*> vertices = data.get_vertices();
list<Edge> edges = data.get_edges();
// Now paint on the targeting circle
if (data.get_current_vertex() != NULL) if (data.get_current_vertex() != NULL)
{ {
Vertex* v = data.get_current_vertex(); Vertex* v = data.get_current_vertex();
DrawUtils::draw_circle_filled(display, v->x, v->y, range, range_colour); DrawUtils::draw_circle_filled(display, v->x, v->y, range,
range_colour);
} }
// Now paint each vertex, and any edges that it needs
for (list<Vertex*>::iterator cursor = vertices.begin(); for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++) cursor != vertices.end(); cursor++)
{ {
Vertex v = *(*cursor); Vertex* v = *cursor;
DrawUtils::draw_circle_filled(display, v.x, v.y, v.r, DrawUtils::draw_circle_filled(display, v->x, v->y, v->r,
v.colour); v->colour);
} for (list<Vertex*>::iterator subcursor = v->neighbors.begin();
subcursor != v->neighbors.end(); subcursor++)
for (list<Edge>::iterator cursor = edges.begin(); {
cursor != edges.end(); cursor++) Vertex* v1 = *subcursor;
{ DrawUtils::draw_line(display, v->x, v->y, v1->x, v1->y, 2,
Edge e = *cursor; v->colour);
DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2, }
e.a->colour);
} }
SDL_Flip(display); SDL_Flip(display);