diff --git a/gamecore.cpp b/gamecore.cpp index ed3f265..5dbd550 100644 --- a/gamecore.cpp +++ b/gamecore.cpp @@ -1,8 +1,10 @@ #include "gamecore.h" #include "drawutils.h" +#include "mathutils.h" #include "debug.h" -int GameCore::NODE_SIZE = 25; +int GameCore::MAX_MOVE_DISTANCE = 100; +int GameCore::NODE_RADIUS = 12; GameCore::GameCore() { @@ -73,14 +75,14 @@ void GameCore::render() { Vertex* v = graph.get_current_vertex(); DrawUtils::draw_circle_filled(display, v->x, v->y, - Graph::MAX_MOVE_DISTANCE, 0xcb1919, 128); + MAX_MOVE_DISTANCE, 0xcb1919, 128); } for (list::iterator cursor = vertices.begin(); cursor != vertices.end(); cursor++) { Vertex v = *(*cursor); - DrawUtils::draw_circle_filled(display, v.x, v.y, NODE_SIZE>>1, + DrawUtils::draw_circle_filled(display, v.x, v.y, NODE_RADIUS, 0x000000); } @@ -118,5 +120,11 @@ void GameCore::on_exit() void GameCore::on_lbutton_down(int x, int y) { - graph.do_vertex(x, y, NODE_SIZE); + Vertex* cv = graph.get_current_vertex(); + if (cv != NULL && + (MathUtils::distance(cv->x, cv->y, x, y) + > MAX_MOVE_DISTANCE)) + return; + + graph.do_vertex(x, y, NODE_RADIUS); } diff --git a/gamecore.h b/gamecore.h index 3a79b72..1f4181f 100644 --- a/gamecore.h +++ b/gamecore.h @@ -41,8 +41,8 @@ class GameCore : public MainEvent // data Graph graph; - // constants - static int NODE_SIZE; + static int NODE_RADIUS; + static int MAX_MOVE_DISTANCE; }; #endif diff --git a/graph.cpp b/graph.cpp index f079a53..1e8cae5 100644 --- a/graph.cpp +++ b/graph.cpp @@ -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::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(current_vertex->x), - static_cast(current_vertex->y), - static_cast(x), - static_cast(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); diff --git a/graph.h b/graph.h index 0aee507..a2ecd5e 100644 --- a/graph.h +++ b/graph.h @@ -14,15 +14,12 @@ using std::list; class Vertex { public: - Vertex(int x, int y) + Vertex(int x, int y, int r) { this->x = x; this->y = y; } int x; int y; - int x_min; - int y_min; - int x_max; - int y_max; + int r; }; struct Edge @@ -42,14 +39,12 @@ class Graph list get_vertices() { return vertices; } list get_edges() { return edges; } - void do_vertex(int x, int y, int size); + void select_vertex(int x, int y); + void do_vertex(int x, int y, int r); Vertex* get_current_vertex() { return current_vertex; } - static int MAX_MOVE_DISTANCE; - private: - void add_vertex(int x, int y, int size); - void select_vertex(int x, int y); + void add_vertex(int x, int y, int r); Vertex* current_vertex; list vertices;