2011-06-22 21:29:41 +00:00
|
|
|
#include "graph.h"
|
2011-06-23 01:54:35 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using std::abs;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
Graph::Graph()
|
|
|
|
{
|
|
|
|
last_vertex = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
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 01:54:35 +00:00
|
|
|
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
|
2011-06-22 21:29:41 +00:00
|
|
|
return true;
|
2011-06-23 01:54:35 +00:00
|
|
|
}
|
2011-06-22 21:29:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:57:44 +00:00
|
|
|
void Graph::add_vertex(int x, int y, int size)
|
2011-06-22 21:29:41 +00:00
|
|
|
{
|
2011-06-23 01:54:35 +00:00
|
|
|
Vertex* v = new Vertex();
|
|
|
|
v->x = x;
|
|
|
|
v->y = y;
|
2011-06-22 21:57:44 +00:00
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
v->x_min = x - size/2;
|
|
|
|
v->x_max = x + size/2;
|
|
|
|
v->y_min = y - size/2;
|
|
|
|
v->y_max = y + size/2;
|
2011-06-22 21:57:44 +00:00
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
if (vertex_present(v->x, v->y, 25)) return;
|
2011-06-22 21:29:41 +00:00
|
|
|
|
|
|
|
vertices.push_back(v);
|
2011-06-23 01:54:35 +00:00
|
|
|
|
|
|
|
if (last_vertex == NULL)
|
|
|
|
{
|
|
|
|
last_vertex = v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 21:57:44 +00:00
|
|
|
Edge e;
|
|
|
|
e.a = last_vertex;
|
|
|
|
e.b = v;
|
|
|
|
edges.push_back(e);
|
2011-06-22 21:29:41 +00:00
|
|
|
last_vertex = v;
|
|
|
|
}
|
|
|
|
|
2011-06-23 01:54:35 +00:00
|
|
|
|
|
|
|
float Vertex::distance(Vertex a, Vertex b)
|
|
|
|
{
|
|
|
|
float dy = abs(static_cast<float>(b.y) - a.y);
|
|
|
|
float dx = abs(static_cast<float>(b.x) - a.x);
|
|
|
|
if (dx == 0) return 0;
|
|
|
|
return dy/dx;
|
|
|
|
}
|