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:
parent
bbae7690f4
commit
53f1560393
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -67,13 +67,13 @@ void GameCore::render()
|
|||
{
|
||||
DrawUtils::draw(display, background, 0, 0);
|
||||
|
||||
list<Vertex> vertices = graph.get_vertices();
|
||||
list<Vertex*> vertices = graph.get_vertices();
|
||||
list<Edge> edges = graph.get_edges();
|
||||
|
||||
for (list<Vertex>::iterator cursor = vertices.begin();
|
||||
for (list<Vertex*>::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);
|
||||
|
|
76
graph.cpp
76
graph.cpp
|
@ -1,4 +1,28 @@
|
|||
#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)
|
||||
{
|
||||
|
@ -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<Vertex>::iterator cursor = vertices.begin();
|
||||
for (list<Vertex*>::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<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
18
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<Vertex> get_vertices() { return vertices; }
|
||||
list<Vertex*> get_vertices() { return vertices; }
|
||||
list<Edge> get_edges() { return edges; }
|
||||
|
||||
void add_vertex(int x, int y, int size);
|
||||
|
||||
private:
|
||||
Vertex last_vertex;
|
||||
list<Vertex> vertices;
|
||||
Vertex* last_vertex;
|
||||
list<Vertex*> vertices;
|
||||
list<Edge> edges;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user