#include "graph.h" #include using std::abs; #ifdef DEBUG #include using std::cerr; #endif Graph::Graph() { last_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 size) { int delta = size / 2; int x_min = x - delta; int x_max = x + delta; int y_min = y - delta; int y_max = y + delta; for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex v = *(*cursor); if (((x_min >= v.x_min && x_min <= v.x_max) && ((y_min >= v.y_min && y_min <= v.y_max) || (y_max >= v.y_min && y_max <= v.y_max))) || ((x_max >= v.x_min && x_max <= v.x_max) && ((y_min >= v.y_min && y_min <= v.y_max) || (y_max >= v.y_min && y_max <= v.y_max)))) { #ifdef DEBUG cerr << "debug: Graph::vertex_present(): vertex present at x=" << v.x << ", y=" << v.y << "\n"; #endif return true; } } return false; } void Graph::add_vertex(int x, int y, int size) { Vertex* v = new Vertex(); v->x = x; v->y = y; v->x_min = x - size/2; v->x_max = x + size/2; v->y_min = y - size/2; v->y_max = y + size/2; if (vertex_present(v->x, v->y, 25)) return; vertices.push_back(v); if (last_vertex == NULL) { last_vertex = v; return; } Edge e; e.a = last_vertex; e.b = v; edges.push_back(e); last_vertex = v; } float Vertex::distance(Vertex a, Vertex b) { float dy = abs(static_cast(b.y) - a.y); float dx = abs(static_cast(b.x) - a.x); if (dx == 0) return 0; return dy/dx; }