#include "graph.h" #include "mathutils.h" Graph::Graph() { current_vertex = NULL; } Graph::~Graph() { for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex* v = *cursor; delete v; } } bool Graph::vertex_present(int x, int y, int r) { for (list::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; } void Graph::do_vertex(int x, int y, int r) { if (vertex_present(x, y, r)) select_vertex(x, y); else add_vertex(x, y, r); } void Graph::select_vertex(int x, int y) { for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex* v = *cursor; if (MathUtils::distance(v->x, v->y, x, y) <= v->r) { current_vertex = v; return; } } } void Graph::add_vertex(int x, int y, int r) { Vertex* v = new Vertex(x, y, r); // Make sure the nodes won't overlap if (vertex_present(v->x, v->y, v->r + r)) return; vertices.push_back(v); if (current_vertex == NULL) { current_vertex = v; return; } Edge e; e.a = current_vertex; e.b = v; edges.push_back(e); current_vertex = v; } Vertex::Vertex(int x, int y, int r) { this->x = x; this->y = y; this->r = r; }