diff --git a/Makefile b/Makefile index cbef9cc..ccbc8db 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT=graphgame CXX=g++ CXXFLAGS=-DDEBUG -g LDFLAGS=-lSDL -OBJECTS=drawutils.o gamecore.o graph.o main.o mainevent.o +OBJECTS=drawutils.o gamecore.o graph.o main.o mainevent.o mathutils.o all: $(PROJECT) diff --git a/drawutils.cpp b/drawutils.cpp index 890fccd..dc72005 100644 --- a/drawutils.cpp +++ b/drawutils.cpp @@ -1,5 +1,6 @@ #include "drawutils.h" #include +#include "mathutils.h" SDL_Surface* DrawUtils::load(string file) { @@ -58,15 +59,12 @@ void DrawUtils::draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Si SDL_Rect pen; - dx = static_cast(x2-x1); - dy = static_cast(y2-y1); - - len = sqrt(dx*dx + dy*dy); + len = MathUtils::distance(x1,y1,x2,y2); // changing dx and dy's meanings. Now they represent the amount we move // each step in our drawing loop - dx /= len; - dy /= len; + dx = static_cast(x2-x1) / len; + dy = static_cast(y2-y1) / len; int j = static_cast(len); diff --git a/graph.cpp b/graph.cpp index 7d126eb..f079a53 100644 --- a/graph.cpp +++ b/graph.cpp @@ -1,8 +1,6 @@ #include "graph.h" #include "debug.h" -#include - -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(current_vertex->x), + static_cast(current_vertex->y), + static_cast(x), + static_cast(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(b.y) - a.y); - float dx = abs(static_cast(b.x) - a.x); - return sqrt(dy*dy + dx*dx); -} diff --git a/graph.h b/graph.h index e7aa325..0aee507 100644 --- a/graph.h +++ b/graph.h @@ -20,11 +20,9 @@ class Vertex int x; int y; int x_min; - int x_max; int y_min; + int x_max; int y_max; - - static float distance(Vertex a, Vertex b); }; struct Edge diff --git a/mathutils.cpp b/mathutils.cpp new file mode 100644 index 0000000..0f83775 --- /dev/null +++ b/mathutils.cpp @@ -0,0 +1,9 @@ +#include "mathutils.h" +#include + +float MathUtils::distance(float x1, float y1, float x2, float y2) +{ + float dy = y2 - y1; + float dx = x2 - x1; + return sqrt(dy*dy + dx*dx); +} diff --git a/mathutils.h b/mathutils.h new file mode 100644 index 0000000..0f012a4 --- /dev/null +++ b/mathutils.h @@ -0,0 +1,14 @@ +/* Some handy mathematics utilities + */ + +#ifndef _MATHUTILS_H_ +#define _MATHUTILS_H_ + +class MathUtils +{ + public: + // returns the cartesian distance of two points + static float distance(float x1, float y1, float x2, float y2); +}; + +#endif