From 23add868d6f500870c6c37551a5f755d0f2356ea Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 22 Jun 2011 17:29:41 -0400 Subject: [PATCH] Added a graph class for representing and manipulating the game data --- graph.cpp | 36 ++++++++++++++++++++++++++++++++++++ graph.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 graph.cpp create mode 100644 graph.h diff --git a/graph.cpp b/graph.cpp new file mode 100644 index 0000000..780f3bb --- /dev/null +++ b/graph.cpp @@ -0,0 +1,36 @@ +#include "graph.h" + +bool Graph::vertex_present(int x, int y, int size) +{ + 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 <= 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)))) + return true; + } + + return false; +} + +void Graph::add_vertex(Vertex v) +{ + if (vertex_present(v.x, v.y)) return; + + vertices.push_back(v); + edges.push_back(Edge(last_vertex, v)); + last_vertex = v; +} + diff --git a/graph.h b/graph.h new file mode 100644 index 0000000..960ce6b --- /dev/null +++ b/graph.h @@ -0,0 +1,48 @@ +/* Represents an undirected graph. + * Also contains the vertex and edge classes + * vertexes know their center point on the SDL Surface - + * bad decoupling maybe, but not too bad, all things considered + */ + +#ifndef _GRAPH_H_ +#define _GRAPH_H_ + +#include + +using std::list; + +struct Vertex +{ + int x; + int y; + int min_x; + int min_y; + int max_x; + int max_y; +}; + +struct Edge +{ + Vertex a; + Vertex b; +}; + +class Graph +{ + public: + Graph() {} + + bool vertex_present(int x, int y, int size); + + list get_vertices() { return vertices; } + list get_edges() { return edges; } + + void add_vertex(Vertex v); + + private: + Vertex last_vertex; + list vertices; + list edges; +}; + +#endif