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:
Anna Rose 2011-06-22 21:54:35 -04:00
parent bbae7690f4
commit 53f1560393
5 changed files with 78 additions and 29 deletions

View File

@ -51,7 +51,7 @@ bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
return true; return true;
} }
void DrawUtils::draw_line(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour, SDL_Surface* dest) void DrawUtils::draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour)
{ {
float dx, dy, len; float dx, dy, len;
float cx, cy; // our current coords float cx, cy; // our current coords

View File

@ -17,7 +17,7 @@ class DrawUtils
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y); static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y);
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y, static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
int x2, int y2, int w, int h); int x2, int y2, int w, int h);
static void draw_line(Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour, SDL_Surface* dest); static void draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour);
// transpare (v) - to make transparent // transpare (v) - to make transparent
static bool transpare(SDL_Surface* surface, int r, int g, int b); static bool transpare(SDL_Surface* surface, int r, int g, int b);

View File

@ -67,13 +67,13 @@ void GameCore::render()
{ {
DrawUtils::draw(display, background, 0, 0); DrawUtils::draw(display, background, 0, 0);
list<Vertex> vertices = graph.get_vertices(); list<Vertex*> vertices = graph.get_vertices();
list<Edge> edges = graph.get_edges(); list<Edge> edges = graph.get_edges();
for (list<Vertex>::iterator cursor = vertices.begin(); for (list<Vertex*>::iterator cursor = vertices.begin();
cursor != vertices.end(); cursor++) cursor != vertices.end(); cursor++)
{ {
Vertex v = *cursor; Vertex v = *(*cursor);
DrawUtils::draw(display, node, v.x_min, v.y_min); DrawUtils::draw(display, node, v.x_min, v.y_min);
} }
@ -81,7 +81,8 @@ void GameCore::render()
cursor != edges.end(); cursor++) cursor != edges.end(); cursor++)
{ {
Edge e = *cursor; Edge e = *cursor;
DrawUtils::draw_line(e.a.x, e.a.y, e.b.x, e.b.y, 2, 0x000000, display); DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
0x000000);
} }
SDL_Flip(display); SDL_Flip(display);

View File

@ -1,4 +1,28 @@
#include "graph.h" #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) bool Graph::vertex_present(int x, int y, int size)
{ {
@ -9,36 +33,48 @@ bool Graph::vertex_present(int x, int y, int size)
int y_min = y - delta; int y_min = y - delta;
int y_max = 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++) cursor != vertices.end(); cursor++)
{ {
Vertex v = *cursor; Vertex v = *(*cursor);
if (((x_min >= v.x_min && x_min <= x_max) && if (((x_min >= v.x_min && x_min <= v.x_max) &&
((y_min >= v.y_min && y_min <= y_max) || ((y_min >= v.y_min && y_min <= v.y_max) ||
(y_max >= v.y_min && y_max <= y_max))) || (y_max >= v.y_min && y_max <= v.y_max))) ||
((x_max >= v.x_min && x_max <= x_max) && ((x_max >= v.x_min && x_max <= v.x_max) &&
((y_min >= v.y_min && y_min <= y_max) || ((y_min >= v.y_min && y_min <= v.y_max) ||
(y_max >= v.y_min && y_max <= 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 true;
} }
}
return false; return false;
} }
void Graph::add_vertex(int x, int y, int size) void Graph::add_vertex(int x, int y, int size)
{ {
Vertex v; Vertex* v = new Vertex();
v.x = x; v->x = x;
v.y = y; v->y = y;
v.x_min = x - size/2; v->x_min = x - size/2;
v.x_max = x + size/2; v->x_max = x + size/2;
v.y_min = y - size/2; v->y_min = y - size/2;
v.y_max = 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); vertices.push_back(v);
if (last_vertex == NULL)
{
last_vertex = v;
return;
}
Edge e; Edge e;
e.a = last_vertex; e.a = last_vertex;
e.b = v; e.b = v;
@ -46,3 +82,11 @@ void Graph::add_vertex(int x, int y, int size)
last_vertex = v; 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;
}

18
graph.h
View File

@ -11,37 +11,41 @@
using std::list; using std::list;
struct Vertex class Vertex
{ {
public:
int x; int x;
int y; int y;
int x_min; int x_min;
int x_max; int x_max;
int y_min; int y_min;
int y_max; int y_max;
static float distance(Vertex a, Vertex b);
}; };
struct Edge struct Edge
{ {
Vertex a; Vertex* a;
Vertex b; Vertex* b;
}; };
class Graph class Graph
{ {
public: public:
Graph() {} Graph();
~Graph();
bool vertex_present(int x, int y, int size); bool vertex_present(int x, int y, int size);
list<Vertex> get_vertices() { return vertices; } list<Vertex*> get_vertices() { return vertices; }
list<Edge> get_edges() { return edges; } list<Edge> get_edges() { return edges; }
void add_vertex(int x, int y, int size); void add_vertex(int x, int y, int size);
private: private:
Vertex last_vertex; Vertex* last_vertex;
list<Vertex> vertices; list<Vertex*> vertices;
list<Edge> edges; list<Edge> edges;
}; };