Factored distance calculation into separate utility class

This commit is contained in:
2011-06-23 12:07:45 -04:00
parent d8e97ff0c9
commit 54b46d77d2
6 changed files with 35 additions and 22 deletions

View File

@ -1,8 +1,6 @@
#include "graph.h"
#include "debug.h"
#include <cmath>
using std::abs;
#include "mathutils.h"
int Graph::MAX_MOVE_DISTANCE = 100;
@ -77,7 +75,11 @@ void Graph::select_vertex(int x, int y)
void Graph::add_vertex(int x, int y, int size)
{
if (current_vertex != NULL &&
(Vertex::distance(*current_vertex, Vertex(x, y)) > MAX_MOVE_DISTANCE))
(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);
@ -102,11 +104,3 @@ void Graph::add_vertex(int x, int y, int size)
edges.push_back(e);
current_vertex = v;
}
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);
return sqrt(dy*dy + dx*dx);
}