Major refactoring - moved constants around, made graph class more graphy, put things where they belong. Simplified a lot of code that I wrote when I wasn't thinking clearly, apparently.

This commit is contained in:
2011-06-23 12:36:40 -04:00
parent 54b46d77d2
commit d76fc8099e
4 changed files with 30 additions and 59 deletions

View File

@ -1,9 +1,6 @@
#include "graph.h"
#include "debug.h"
#include "mathutils.h"
int Graph::MAX_MOVE_DISTANCE = 100;
Graph::Graph()
{
current_vertex = NULL;
@ -19,41 +16,23 @@ Graph::~Graph()
}
}
bool Graph::vertex_present(int x, int y, int size)
bool Graph::vertex_present(int x, int y, int r)
{
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<Vertex*>::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;
}
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 size)
void Graph::do_vertex(int x, int y, int r)
{
if (vertex_present(x, y, size)) select_vertex(x, y);
else add_vertex(x, y, size);
if (vertex_present(x, y, r)) select_vertex(x, y);
else add_vertex(x, y, r);
}
@ -63,7 +42,7 @@ void Graph::select_vertex(int x, int y)
cursor != vertices.end(); cursor++)
{
Vertex* v = *cursor;
if (x >= v->x_min && x <= v->x_max && y >= v->y_min && y <= v->y_max)
if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
{
current_vertex = v;
return;
@ -72,23 +51,12 @@ void Graph::select_vertex(int x, int y)
}
void Graph::add_vertex(int x, int y, int size)
void Graph::add_vertex(int x, int y, int r)
{
if (current_vertex != NULL &&
(MathUtils::distance(static_cast<float>(current_vertex->x),
static_cast<float>(current_vertex->y),
static_cast<float>(x),
static_cast<float>(y))
> MAX_MOVE_DISTANCE))
return;
Vertex* v = new Vertex(x, y, r);
Vertex* v = new Vertex(x, 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;
// Make sure the nodes won't overlap
if (vertex_present(v->x, v->y, v->r + r)) return;
vertices.push_back(v);