commit dcf4500732cd7ed2c7171b84b9683865cde65857
parent 0f0a6aa6c02c42898dc193083c1555b455ac1eda
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Sun, 6 Nov 2011 21:30:14 +0100
Refactor pour séparer chaque règle dans deux fichiers .cpp et .hh .
Diffstat:
17 files changed, 254 insertions(+), 137 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -2,6 +2,7 @@ simple-terrain
display
roam
roads
-rules
+city
*.o
+*.d
.*.d
diff --git a/Makefile b/Makefile
@@ -3,17 +3,24 @@ CXX=g++
CCWARN=-Wall -Wextra -Werror
CFLAGS=-O3 $(CCWARN) -g3
+OBJECTS = rules.o hash.o segment.o vertex.o rules/rectangleroutes.o rules/route.o rules/carrefour.o
+EXECUTABLE = city
+
+.PHONY: test
+test: all
+ ./$(EXECUTABLE)
+
.PHONY: all
-all: rules
+all: $(EXECUTABLE)
.PHONY: clean
clean:
- rm rules *.o .*.d
+ rm -f $(EXECUTABLE) $(OBJECTS) $(OBJECTS:.o=.d)
-rules: rules.o hash.o
+city: $(OBJECTS)
$(CXX) -lm $^ -o $@
--include .*.d
+-include $(OBJECTS:.o=.d)
%.o: %.cpp Makefile
- $(CXX) -MMD -MF .$(@:.o=.d) -c $< $(CFLAGS) -o $@
+ $(CXX) -MMD -MF $(@:.o=.d) -c $< $(CFLAGS) -o $@
diff --git a/hash.h b/hash.h
@@ -1,5 +0,0 @@
-unsigned int random();
-unsigned int hash2(unsigned int a, unsigned int b);
-unsigned int hash3(unsigned int seed, int x, int y);
-int hashInRange(int seed, int n, int a, int b);
-int newSeed(int seed, int n);
diff --git a/hash.hh b/hash.hh
@@ -0,0 +1,10 @@
+#ifndef _HASH_HH_
+#define _HASH_HH_
+
+unsigned int random();
+unsigned int hash2(unsigned int a, unsigned int b);
+unsigned int hash3(unsigned int seed, int x, int y);
+int hashInRange(int seed, int n, int a, int b);
+int newSeed(int seed, int n);
+
+#endif
diff --git a/rules.cpp b/rules.cpp
@@ -1,99 +1,7 @@
-#include <iostream>
-#include "rules.h"
-#include "hash.h"
-
-class Carrefour;
-std::ostream& operator<<(std::ostream& os, const Carrefour& c);
-std::ostream& operator<<(std::ostream& os, const Carrefour* c);
-
-class Carrefour {
-public:
- Vertex corners[4];
-public:
- Carrefour(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
- corners[NE]=ne;
- corners[SE]=se;
- corners[SW]=sw;
- corners[NW]=nw;
- std::cout << this << std::endl;
- }
-};
-
-std::ostream& operator<<(std::ostream& os, const Carrefour* c) { return os << *c; }
-std::ostream& operator<<(std::ostream& os, const Carrefour& c) {
- return os << "Carrefour " << c.corners[NE] << "-" << c.corners[SE] << "-" << c.corners[NW] << "-" << c.corners[SW];
-}
-
-class Route;
-std::ostream& operator<<(std::ostream& os, const Route& r);
-std::ostream& operator<<(std::ostream& os, const Route* r);
-
-class Route {
-public:
- Vertex corners[4];
-public:
- Route(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
- corners[NE]=ne;
- corners[SE]=se;
- corners[SW]=sw;
- corners[NW]=nw;
- std::cout << this << std::endl;
- }
-};
-
-std::ostream& operator<<(std::ostream& os, const Route* r) { return os << *r; }
-std::ostream& operator<<(std::ostream& os, const Route& r) {
- return os << "Route " << r.corners[NE] << "-" << r.corners[SE] << "-" << r.corners[NW] << "-" << r.corners[SW];
-}
-
-class RectangleRoutes;
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r);
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r);
-
-// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
-class RectangleRoutes {
-public:
- Vertex ne;
- Vertex sw;
- IO io [4];
- int seed;
-public:
- RectangleRoutes(Vertex ne, Vertex sw, int seed) : ne(ne), sw(sw), seed(seed) {
- std::cout << this << std::endl;
- }
- int width() { return this->ne.x - this->sw.x; }
- int height() { return this->ne.y - this->sw.y; }
- void subdivide() {
- Vertex split(
- hashInRange(this->seed, 0, this->sw.x + this->width()*1/4, this->sw.x + this->width()*3/4),
- hashInRange(this->seed, 1, this->sw.y + this->height()*1/4, this->sw.y + this->height()*3/4)
- );
- Carrefour c(split.add(1,1), split.add(1,-1), split.add(-1,-1), split.add(-1,1));
- // routes au NESW du carrefour
- Vertex roadEndN(this->ne.y, split.x);
- Vertex roadEndE(this->ne.x, split.y);
- Vertex roadEndS(this->sw.y, split.x);
- Vertex roadEndW(this->sw.x, split.y);
- Route rn(roadEndN.add(-1,0), roadEndN.add(+1,0), split.add(+1,+1), split.add(-1,+1));
- Route re(roadEndE.add(0,+1), roadEndE.add(0,-1), split.add(+1,-1), split.add(+1,+1));
- Route rs(roadEndS.add(+1,0), roadEndS.add(-1,0), split.add(-1,-1), split.add(+1,-1));
- Route rw(roadEndW.add(0,-1), roadEndW.add(0,+1), split.add(-1,+1), split.add(-1,-1));
- // Sous-quartiers
- RectangleRoutes(this->ne, re.corners[NW], newSeed(this->seed, 2));
- RectangleRoutes(re.corners[SE], rs.corners[SE], newSeed(this->seed, 3));
- RectangleRoutes(rs.corners[NW], this->sw, newSeed(this->seed, 4));
- RectangleRoutes(Vertex(this->sw.x, this->ne.y), rn.corners[SW], newSeed(this->seed, 5));
- }
-};
-
-std::ostream& operator<<(std::ostream& os, const Vertex& v) {
- return os << "(" << v.x << "," << v.y << ")";
-}
-
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r) { return os << *r; }
-std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r) {
- return os << "RectangleRoutes " << r.ne << "-" << r.sw;
-}
+#include "vertex.hh"
+#include "rules.hh"
+#include "hash.hh"
+#include "rules/rectangleroutes.hh"
int main() {
// Générer une tile de base
diff --git a/rules.h b/rules.h
@@ -1,30 +0,0 @@
-#include <iostream>
-
-typedef struct Vertex {
- int x;
- int y;
- //int z;
- Vertex() {}
- Vertex(int x, int y): x(x), y(y) {}
- Vertex add(int dx, int dy) { Vertex v = *this; v.x += dx; v.y += dy; return v; }
-} Vertex;
-std::ostream& operator<<(std::ostream& os, const Vertex& v);
-
-typedef enum Cardinaux {
- N = 0,
- E = 1,
- S = 2,
- W = 3
-} Cardinaux;
-
-typedef enum Diagonales {
- NE = 0,
- SE = 1,
- SW = 2,
- NW = 3
-} Diagonales;
-
-typedef struct IO {
- int in;
- int out;
-} IO;
diff --git a/rules.hh b/rules.hh
@@ -0,0 +1,23 @@
+#ifndef _RULES_HH_
+#define _RULES_HH_
+
+typedef enum Cardinaux {
+ N = 0,
+ E = 1,
+ S = 2,
+ W = 3
+} Cardinaux;
+
+typedef enum Diagonales {
+ NE = 0,
+ SE = 1,
+ SW = 2,
+ NW = 3
+} Diagonales;
+
+typedef struct IO {
+ int in;
+ int out;
+} IO;
+
+#endif
diff --git a/rules/carrefour.cpp b/rules/carrefour.cpp
@@ -0,0 +1,17 @@
+#include "carrefour.hh"
+
+Carrefour::Carrefour(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
+ corners[NE]=ne;
+ corners[SE]=se;
+ corners[SW]=sw;
+ corners[NW]=nw;
+ std::cout << this << std::endl;
+}
+
+std::ostream& operator<<(std::ostream& os, const Carrefour* c) {
+ return os << *c;
+}
+
+std::ostream& operator<<(std::ostream& os, const Carrefour& c) {
+ return os << "Carrefour " << c.corners[NE] << "-" << c.corners[SE] << "-" << c.corners[NW] << "-" << c.corners[SW];
+}
diff --git a/rules/carrefour.hh b/rules/carrefour.hh
@@ -0,0 +1,18 @@
+#ifndef _CARREFOUR_ROUTE_HH_
+#define _CARREFOUR_ROUTE_HH_
+
+#include <iostream>
+#include "../vertex.hh"
+#include "../rules.hh"
+
+class Carrefour {
+public:
+ Vertex corners[4];
+public:
+ Carrefour(Vertex ne, Vertex se, Vertex sw, Vertex nw);
+};
+
+std::ostream& operator<<(std::ostream& os, const Carrefour& c);
+std::ostream& operator<<(std::ostream& os, const Carrefour* c);
+
+#endif
diff --git a/rules/rectangleroutes.cpp b/rules/rectangleroutes.cpp
@@ -0,0 +1,45 @@
+#include "rectangleroutes.hh"
+#include "../vertex.hh"
+#include "../rules.hh"
+#include "../hash.hh"
+
+#include "carrefour.hh"
+#include "route.hh"
+
+RectangleRoutes::RectangleRoutes(Vertex ne, Vertex sw, int seed) : ne(ne), sw(sw), seed(seed) {
+ std::cout << this << std::endl;
+}
+
+int RectangleRoutes::width() { return this->ne.x - this->sw.x; }
+
+int RectangleRoutes::height() { return this->ne.y - this->sw.y; }
+
+void RectangleRoutes::subdivide() {
+ Vertex split(
+ hashInRange(this->seed, 0, this->sw.x + this->width()*1/4, this->sw.x + this->width()*3/4),
+ hashInRange(this->seed, 1, this->sw.y + this->height()*1/4, this->sw.y + this->height()*3/4)
+ );
+ Carrefour c(split.add(1,1), split.add(1,-1), split.add(-1,-1), split.add(-1,1));
+ // routes au NESW du carrefour
+ Vertex roadEndN(this->ne.y, split.x);
+ Vertex roadEndE(this->ne.x, split.y);
+ Vertex roadEndS(this->sw.y, split.x);
+ Vertex roadEndW(this->sw.x, split.y);
+ Route rn(roadEndN.add(-1,0), roadEndN.add(+1,0), split.add(+1,+1), split.add(-1,+1));
+ Route re(roadEndE.add(0,+1), roadEndE.add(0,-1), split.add(+1,-1), split.add(+1,+1));
+ Route rs(roadEndS.add(+1,0), roadEndS.add(-1,0), split.add(-1,-1), split.add(+1,-1));
+ Route rw(roadEndW.add(0,-1), roadEndW.add(0,+1), split.add(-1,+1), split.add(-1,-1));
+ // Sous-quartiers
+ RectangleRoutes(this->ne, re.corners[NW], newSeed(this->seed, 2));
+ RectangleRoutes(re.corners[SE], rs.corners[SE], newSeed(this->seed, 3));
+ RectangleRoutes(rs.corners[NW], this->sw, newSeed(this->seed, 4));
+ RectangleRoutes(Vertex(this->sw.x, this->ne.y), rn.corners[SW], newSeed(this->seed, 5));
+}
+
+std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r) {
+ return os << *r;
+}
+
+std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r) {
+ return os << "RectangleRoutes " << r.ne << "-" << r.sw;
+}
diff --git a/rules/rectangleroutes.hh b/rules/rectangleroutes.hh
@@ -0,0 +1,25 @@
+#ifndef _RULES_RECTANGLEROUTES_HH_
+#define _RULES_RECTANGLEROUTES_HH_
+
+#include <iostream>
+#include "../vertex.hh"
+#include "../rules.hh"
+
+// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
+class RectangleRoutes {
+public:
+ Vertex ne;
+ Vertex sw;
+ IO io [4];
+ int seed;
+public:
+ RectangleRoutes(Vertex ne, Vertex sw, int seed);
+ int width();
+ int height();
+ void subdivide();
+};
+
+std::ostream& operator<<(std::ostream& os, const RectangleRoutes& r);
+std::ostream& operator<<(std::ostream& os, const RectangleRoutes* r);
+
+#endif
diff --git a/rules/route.cpp b/rules/route.cpp
@@ -0,0 +1,19 @@
+#include "route.hh"
+#include "../vertex.hh"
+#include "../rules.hh"
+
+Route::Route(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
+ corners[NE]=ne;
+ corners[SE]=se;
+ corners[SW]=sw;
+ corners[NW]=nw;
+ std::cout << this << std::endl;
+}
+
+std::ostream& operator<<(std::ostream& os, const Route* r) {
+ return os << *r;
+}
+
+std::ostream& operator<<(std::ostream& os, const Route& r) {
+ return os << "Route " << r.corners[NE] << "-" << r.corners[SE] << "-" << r.corners[NW] << "-" << r.corners[SW];
+}
diff --git a/rules/route.hh b/rules/route.hh
@@ -0,0 +1,18 @@
+#ifndef _RULES_ROUTE_HH_
+#define _RULES_ROUTE_HH_
+
+#include <iostream>
+#include "../vertex.hh"
+#include "../rules.hh"
+
+class Route {
+public:
+ Vertex corners[4];
+public:
+ Route(Vertex ne, Vertex se, Vertex sw, Vertex nw);
+};
+
+std::ostream& operator<<(std::ostream& os, const Route& r);
+std::ostream& operator<<(std::ostream& os, const Route* r);
+
+#endif
diff --git a/segment.cpp b/segment.cpp
@@ -0,0 +1,11 @@
+#include "segment.hh"
+#include "vertex.hh"
+#include <math.h>
+
+Segment::Segment(Vertex u, Vertex v): u(u), v(v) {}
+
+int Segment::length() {
+ int x = u.x - v.x;
+ int y = u.y - v.y;
+ return sqrt(x*x + y*y);
+}
diff --git a/segment.hh b/segment.hh
@@ -0,0 +1,15 @@
+#ifndef _SEGMENT_HH_
+#define _SEGMENT_HH_
+
+#include "vertex.hh"
+
+class Segment {
+public:
+ Vertex u;
+ Vertex v;
+public:
+ Segment(Vertex u, Vertex v);
+ int length();
+};
+
+#endif
diff --git a/vertex.cpp b/vertex.cpp
@@ -0,0 +1,16 @@
+#include "vertex.hh"
+
+Vertex::Vertex() {}
+
+Vertex::Vertex(int x, int y): x(x), y(y) {}
+
+Vertex Vertex::add(int dx, int dy) {
+ Vertex v = *this;
+ v.x += dx;
+ v.y += dy;
+ return v;
+}
+
+std::ostream& operator<<(std::ostream& os, const Vertex& v) {
+ return os << "(" << v.x << "," << v.y << ")";
+}
diff --git a/vertex.hh b/vertex.hh
@@ -0,0 +1,19 @@
+#ifndef _VERTEX_HH_
+#define _VERTEX_HH_
+
+#include <iostream>
+
+class Vertex {
+public:
+ int x;
+ int y;
+ //int z;
+public:
+ Vertex();
+ Vertex(int x, int y);
+ Vertex add(int dx, int dy);
+};
+
+std::ostream& operator<<(std::ostream& os, const Vertex& v);
+
+#endif