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:
117
graph.cpp
117
graph.cpp
@ -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();
|
||||
cursor != edges.end(); cursor++)
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
{
|
||||
Edge c = *cursor;
|
||||
if (MathUtils::lines_intersect(c.a->x, c.a->y, c.b->x, c.b->y,
|
||||
e.a->x, e.a->y, e.b->x, e.b->y))
|
||||
return true;
|
||||
Vertex* v = *cursor;
|
||||
for (list<Vertex*>::iterator subcursor = v->neighbors.begin();
|
||||
subcursor != v->neighbors.end(); subcursor++)
|
||||
{
|
||||
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;
|
||||
@ -92,20 +98,17 @@ bool Graph::add_vertex(int x, int y, int r, int colour, int score, Vertex* src)
|
||||
|
||||
if (src != NULL)
|
||||
{
|
||||
Edge e;
|
||||
e.a = src;
|
||||
e.b = v;
|
||||
|
||||
if (planar && crosses_edge(e))
|
||||
if (planar && crosses_edge(v, src))
|
||||
{
|
||||
#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
|
||||
delete v;
|
||||
return false;
|
||||
}
|
||||
|
||||
edges.push_back(e);
|
||||
v->neighbors.push_back(src);
|
||||
src->neighbors.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)
|
||||
{
|
||||
list<Edge> dead_edges = get_edges(target);
|
||||
|
||||
for (list<Edge>::iterator cursor = dead_edges.begin();
|
||||
cursor != dead_edges.end(); cursor++)
|
||||
list<Vertex*>::iterator cursor;
|
||||
|
||||
for (cursor = target->neighbors.begin(); cursor != target->neighbors.end();
|
||||
cursor++)
|
||||
{
|
||||
Edge e = *cursor;
|
||||
list<Edge>::iterator to_del = find(edges.begin(), edges.end(), e);
|
||||
|
||||
if (to_del != edges.end())
|
||||
{
|
||||
edges.erase(to_del);
|
||||
}
|
||||
Vertex* neighbor = *cursor;
|
||||
list<Vertex*>::iterator subcursor = find(target->neighbors.begin(),
|
||||
target->neighbors.end(),
|
||||
target);
|
||||
assert(subcursor != target->neighbors.end());
|
||||
target->neighbors.erase(subcursor);
|
||||
}
|
||||
|
||||
list<Vertex*>::iterator to_del = find(vertices.begin(), vertices.end(),
|
||||
target);
|
||||
|
||||
assert(to_del != vertices.end());
|
||||
vertices.erase(to_del);
|
||||
|
||||
cursor = find(vertices.begin(), vertices.end(), target);
|
||||
assert(cursor != vertices.end());
|
||||
vertices.erase(cursor);
|
||||
delete target;
|
||||
}
|
||||
|
||||
@ -197,27 +166,3 @@ Vertex::Vertex(int x, int y, int r, int colour, int score)
|
||||
this->colour = colour;
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user