224 lines
4.1 KiB
C++
224 lines
4.1 KiB
C++
#include "graph.h"
|
|
#include "mathutils.h"
|
|
#include "debug.h"
|
|
#include <list>
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
|
|
using std::list;
|
|
|
|
Graph::Graph(bool planar)
|
|
{
|
|
this->planar = planar;
|
|
}
|
|
|
|
Graph::~Graph()
|
|
{
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
cursor != vertices.end(); cursor++)
|
|
{
|
|
Vertex* v = *cursor;
|
|
delete v;
|
|
}
|
|
}
|
|
|
|
bool Graph::point_in_vertex(int x, int y)
|
|
{
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
cursor != vertices.end(); cursor++)
|
|
{
|
|
Vertex* v = *cursor;
|
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
Vertex * Graph::vertex_at(int x, int y)
|
|
{
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
cursor != vertices.end(); cursor++)
|
|
{
|
|
Vertex* v = *cursor;
|
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return v;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
bool Graph::vertex_would_overlap(int x, int y, int r)
|
|
{
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
cursor != vertices.end(); cursor++)
|
|
{
|
|
Vertex* v = *cursor;
|
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r + r) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool Graph::crosses_edge(Edge e)
|
|
{
|
|
for (list<Edge>::iterator cursor = edges.begin();
|
|
cursor != edges.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;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool Graph::add_vertex(int x, int y, int r, int colour, int score, Vertex* src)
|
|
{
|
|
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))
|
|
{
|
|
#ifdef DEBUG
|
|
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to vertex collision: x=%d, y=%d, r=%d\n", v->x, v->y, v->r);
|
|
#endif
|
|
delete v;
|
|
return false;
|
|
}
|
|
|
|
if (src != NULL)
|
|
{
|
|
Edge e;
|
|
e.a = src;
|
|
e.b = v;
|
|
|
|
if (planar && crosses_edge(e))
|
|
{
|
|
#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);
|
|
#endif
|
|
delete v;
|
|
return false;
|
|
}
|
|
|
|
edges.push_back(e);
|
|
}
|
|
|
|
vertices.push_back(v);
|
|
|
|
#ifdef DEBUG
|
|
fprintf(stderr, "debug: Graph::add_vertex(): added: x=%d, y=%d, r=%d, score=%d\n", v->x, v->y, v->r, v->score);
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
list<Vertex*> Graph::get_colour(int colour)
|
|
{
|
|
list<Vertex*> answer;
|
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
cursor != vertices.end(); cursor++)
|
|
{
|
|
Vertex* v = *cursor;
|
|
if (v->colour == colour) answer.push_back(v);
|
|
}
|
|
|
|
return answer;
|
|
}
|
|
|
|
|
|
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++)
|
|
{
|
|
Edge e = *cursor;
|
|
list<Edge>::iterator to_del = find(edges.begin(), edges.end(), e);
|
|
|
|
if (to_del != edges.end())
|
|
{
|
|
edges.erase(to_del);
|
|
}
|
|
}
|
|
|
|
list<Vertex*>::iterator to_del = find(vertices.begin(), vertices.end(),
|
|
target);
|
|
|
|
assert(to_del != vertices.end());
|
|
vertices.erase(to_del);
|
|
delete target;
|
|
}
|
|
|
|
|
|
Vertex::Vertex(int x, int y, int r, int colour, int score)
|
|
{
|
|
this->x = x;
|
|
this->y = y;
|
|
this->r = r;
|
|
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);
|
|
}
|