Factored distance calculation into separate utility class

This commit is contained in:
Anna Rose 2011-06-23 12:07:45 -04:00
parent d8e97ff0c9
commit 54b46d77d2
6 changed files with 35 additions and 22 deletions

View File

@ -4,7 +4,7 @@ PROJECT=graphgame
CXX=g++ CXX=g++
CXXFLAGS=-DDEBUG -g CXXFLAGS=-DDEBUG -g
LDFLAGS=-lSDL 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) all: $(PROJECT)

View File

@ -1,5 +1,6 @@
#include "drawutils.h" #include "drawutils.h"
#include <cmath> #include <cmath>
#include "mathutils.h"
SDL_Surface* DrawUtils::load(string file) 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; SDL_Rect pen;
dx = static_cast<float>(x2-x1); len = MathUtils::distance(x1,y1,x2,y2);
dy = static_cast<float>(y2-y1);
len = sqrt(dx*dx + dy*dy);
// changing dx and dy's meanings. Now they represent the amount we move // changing dx and dy's meanings. Now they represent the amount we move
// each step in our drawing loop // each step in our drawing loop
dx /= len; dx = static_cast<float>(x2-x1) / len;
dy /= len; dy = static_cast<float>(y2-y1) / len;
int j = static_cast<int>(len); int j = static_cast<int>(len);

View File

@ -1,8 +1,6 @@
#include "graph.h" #include "graph.h"
#include "debug.h" #include "debug.h"
#include <cmath> #include "mathutils.h"
using std::abs;
int Graph::MAX_MOVE_DISTANCE = 100; 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) void Graph::add_vertex(int x, int y, int size)
{ {
if (current_vertex != NULL && if (current_vertex != NULL &&
(Vertex::distance(*current_vertex, Vertex(x, y)) > MAX_MOVE_DISTANCE)) (MathUtils::distance(static_cast<float>(current_vertex->x),
static_cast<float>(current_vertex->y),
static_cast<float>(x),
static_cast<float>(y))
> MAX_MOVE_DISTANCE))
return; return;
Vertex* v = new Vertex(x, y); Vertex* v = new Vertex(x, y);
@ -102,11 +104,3 @@ void Graph::add_vertex(int x, int y, int size)
edges.push_back(e); edges.push_back(e);
current_vertex = v; current_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);
return sqrt(dy*dy + dx*dx);
}

View File

@ -20,11 +20,9 @@ class Vertex
int x; int x;
int y; int y;
int x_min; int x_min;
int x_max;
int y_min; int y_min;
int x_max;
int y_max; int y_max;
static float distance(Vertex a, Vertex b);
}; };
struct Edge struct Edge

9
mathutils.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "mathutils.h"
#include <math.h>
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);
}

14
mathutils.h Normal file
View File

@ -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