Fixed first vertex bug, use pointers to track vertices (in case we want to move them easily later), and added a formula for vertex distance calculations

This commit is contained in:
2011-06-22 21:54:35 -04:00
parent bbae7690f4
commit 53f1560393
5 changed files with 78 additions and 29 deletions

View File

@ -1,4 +1,28 @@
#include "graph.h"
#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;
}
}
bool Graph::vertex_present(int x, int y, int size)
{
@ -9,17 +33,22 @@ bool Graph::vertex_present(int x, int y, int size)
int y_min = y - delta;
int y_max = y + delta;
for (list<Vertex>::iterator cursor = vertices.begin();
for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++)
{
Vertex v = *cursor;
if (((x_min >= v.x_min && x_min <= x_max) &&
((y_min >= v.y_min && y_min <= y_max) ||
(y_max >= v.y_min && y_max <= y_max))) ||
((x_max >= v.x_min && x_max <= x_max) &&
((y_min >= v.y_min && y_min <= y_max) ||
(y_max >= v.y_min && y_max <= y_max))))
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;
@ -27,18 +56,25 @@ bool Graph::vertex_present(int x, int y, int size)
void Graph::add_vertex(int x, int y, int size)
{
Vertex v;
v.x = x;
v.y = y;
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;
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;
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;
@ -46,3 +82,11 @@ void Graph::add_vertex(int x, int y, int size)
last_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);
if (dx == 0) return 0;
return dy/dx;
}