diff --git a/drawutils.cpp b/drawutils.cpp index 0e42991..63877c0 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -51,7 +51,7 @@ bool DrawUtils::draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y, 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 cx, cy; // our current coords diff --git a/drawutils.h b/drawutils.h index 119dd73..cfe2ec1 100644 --- a/drawutils.h +++ b/drawutils.h @@ -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, 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 static bool transpare(SDL_Surface* surface, int r, int g, int b); diff --git a/gamecore.cpp b/gamecore.cpp index f242b1c..3291705 100644 --- a/gamecore.cpp +++ b/gamecore.cpp @@ -67,13 +67,13 @@ void GameCore::render() { DrawUtils::draw(display, background, 0, 0); - list vertices = graph.get_vertices(); + list vertices = graph.get_vertices(); list edges = graph.get_edges(); - for (list::iterator cursor = vertices.begin(); + for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { - Vertex v = *cursor; + Vertex v = *(*cursor); DrawUtils::draw(display, node, v.x_min, v.y_min); } @@ -81,7 +81,8 @@ void GameCore::render() cursor != edges.end(); 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); diff --git a/graph.cpp b/graph.cpp index 7c54d35..4481bfe 100644 --- a/graph.cpp +++ b/graph.cpp @@ -1,4 +1,28 @@ #include "graph.h" +#include + +using std::abs; + +#ifdef DEBUG +#include +using std::cerr; +#endif + + +Graph::Graph() +{ + last_vertex = NULL; +} + +Graph::~Graph() +{ + for (list::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::iterator cursor = vertices.begin(); + for (list::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(b.y) - a.y); + float dx = abs(static_cast(b.x) - a.x); + if (dx == 0) return 0; + return dy/dx; +} diff --git a/graph.h b/graph.h index f7fe32c..1332868 100644 --- a/graph.h +++ b/graph.h @@ -11,37 +11,41 @@ using std::list; -struct Vertex +class Vertex { + public: int x; int y; int x_min; int x_max; int y_min; int y_max; + + static float distance(Vertex a, Vertex b); }; struct Edge { - Vertex a; - Vertex b; + Vertex* a; + Vertex* b; }; class Graph { public: - Graph() {} + Graph(); + ~Graph(); bool vertex_present(int x, int y, int size); - list get_vertices() { return vertices; } + list get_vertices() { return vertices; } list get_edges() { return edges; } void add_vertex(int x, int y, int size); private: - Vertex last_vertex; - list vertices; + Vertex* last_vertex; + list vertices; list edges; };