2011-06-22 21:29:41 +00:00
|
|
|
#include "graph.h"
|
2011-06-23 16:07:45 +00:00
|
|
|
#include "mathutils.h"
|
2011-06-23 17:54:28 +00:00
|
|
|
#include "debug.h"
|
2011-06-23 01:54:35 +00:00
|
|
|
|
|
|
|
Graph::Graph()
|
|
|
|
{
|
2011-06-23 02:32:33 +00:00
|
|
|
current_vertex = NULL;
|
2011-06-23 01:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Graph::~Graph()
|
|
|
|
{
|
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
|
|
cursor != vertices.end(); cursor++)
|
|
|
|
{
|
|
|
|
Vertex* v = *cursor;
|
|
|
|
delete v;
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 21:29:41 +00:00
|
|
|
|
2011-06-23 16:36:40 +00:00
|
|
|
bool Graph::vertex_present(int x, int y, int r)
|
2011-06-22 21:29:41 +00:00
|
|
|
{
|
2011-06-23 01:54:35 +00:00
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
2011-06-22 21:29:41 +00:00
|
|
|
cursor != vertices.end(); cursor++)
|
|
|
|
{
|
2011-06-23 16:36:40 +00:00
|
|
|
Vertex* v = *cursor;
|
|
|
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return true;
|
2011-06-22 21:29:41 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 17:54:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2011-06-22 21:29:41 +00:00
|
|
|
}
|
|
|
|
|
2011-06-23 02:32:33 +00:00
|
|
|
|
2011-06-23 16:36:40 +00:00
|
|
|
void Graph::do_vertex(int x, int y, int r)
|
2011-06-23 02:32:33 +00:00
|
|
|
{
|
2011-06-23 16:36:40 +00:00
|
|
|
if (vertex_present(x, y, r)) select_vertex(x, y);
|
|
|
|
else add_vertex(x, y, r);
|
2011-06-23 02:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graph::select_vertex(int x, int y)
|
|
|
|
{
|
|
|
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
|
|
|
cursor != vertices.end(); cursor++)
|
|
|
|
{
|
|
|
|
Vertex* v = *cursor;
|
2011-06-23 16:36:40 +00:00
|
|
|
if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
|
2011-06-23 02:32:33 +00:00
|
|
|
{
|
|
|
|
current_vertex = v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-23 16:36:40 +00:00
|
|
|
void Graph::add_vertex(int x, int y, int r)
|
2011-06-22 21:29:41 +00:00
|
|
|
{
|
2011-06-23 16:36:40 +00:00
|
|
|
Vertex* v = new Vertex(x, y, r);
|
2011-06-22 21:57:44 +00:00
|
|
|
|
2011-06-23 16:36:40 +00:00
|
|
|
// Make sure the nodes won't overlap
|
2011-06-23 17:54:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-06-23 01:54:35 +00:00
|
|
|
|
2011-06-23 02:32:33 +00:00
|
|
|
if (current_vertex == NULL)
|
2011-06-23 01:54:35 +00:00
|
|
|
{
|
2011-06-23 02:32:33 +00:00
|
|
|
current_vertex = v;
|
2011-06-23 17:54:28 +00:00
|
|
|
vertices.push_back(v);
|
2011-06-23 01:54:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:57:44 +00:00
|
|
|
Edge e;
|
2011-06-23 02:32:33 +00:00
|
|
|
e.a = current_vertex;
|
2011-06-22 21:57:44 +00:00
|
|
|
e.b = v;
|
2011-06-23 17:54:28 +00:00
|
|
|
|
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices.push_back(v);
|
2011-06-22 21:57:44 +00:00
|
|
|
edges.push_back(e);
|
2011-06-23 02:32:33 +00:00
|
|
|
current_vertex = v;
|
2011-06-23 17:54:28 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "debug: Graph::add_vertex(): added: x=%d, y=%d, r=%d\n", v->x, v->y, v->r);
|
|
|
|
#endif
|
2011-06-22 21:29:41 +00:00
|
|
|
}
|
2011-06-23 16:46:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
Vertex::Vertex(int x, int y, int r)
|
|
|
|
{
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
this->r = r;
|
|
|
|
}
|