commit 22f44ce6325821138d246d603e1f90a51c668e6e
parent e035eac1c843ac3b46f886cb81b7f6d0a195b930
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Thu, 1 Dec 2011 00:28:23 +0100
Début de QuadRoutes.
Diffstat:
7 files changed, 107 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,7 +4,7 @@ CCWARN=-Wall -Wextra -Werror
# -flto (nécessite GCC 4.5) -m32 ou -m64
CFLAGS=-O3 -g3 -I. $(CCWARN)
-OBJECTS = main.o view.o hash.o segment.o vertex.o triangle.o rules/chose.o rules/rectangleroutes.o rules/route.o rules/carrefour.o rules/batiment.o
+OBJECTS = main.o view.o hash.o segment.o vertex.o triangle.o rules/chose.o rules/rectangleroutes.o rules/quadroutes.o rules/route.o rules/carrefour.o rules/batiment.o
LIBS = -lm -lGL -lGLU -lSDL -lGLEW
EXECUTABLE = city
diff --git a/all_includes.hh b/all_includes.hh
@@ -25,5 +25,6 @@ class Chose;
#include "rules/carrefour.hh"
#include "rules/route.hh"
#include "rules/rectangleroutes.hh"
+#include "rules/quadroutes.hh"
#endif
diff --git a/main.cpp b/main.cpp
@@ -16,9 +16,12 @@ void recursiveSubdivide(Chose* c) {
int main() {
// Générer une tile de base
- Vertex ne(50, 50, 0);
+ int size = 50;
+ Vertex ne(size, size, 0);
+ Vertex se(size, 0, 0);
Vertex sw(0, 0, 0);
- Chose* c = new RectangleRoutes(ne,sw);//new RectangleRoutes(ne, sw);
+ Vertex nw(0, size, 0);
+ Chose* c = new QuadRoutes(ne,se,sw,nw);
recursiveSubdivide(c);
new View(c);
diff --git a/rules/batiment.cpp b/rules/batiment.cpp
@@ -1,7 +1,7 @@
#include "all_includes.hh"
Batiment::Batiment(Vertex ne, Vertex se, Vertex sw, Vertex nw) : Chose(), ne(ne), se(se), sw(sw), nw(nw) {
- addEntropy(ne, sw);
+ addEntropy(ne, se, sw, nw);
triangulation();
}
diff --git a/rules/quadroutes.cpp b/rules/quadroutes.cpp
@@ -0,0 +1,71 @@
+#include "all_includes.hh"
+
+QuadRoutes::QuadRoutes(Vertex ne, Vertex se, Vertex sw, Vertex nw) : Chose(), ne(ne), se(se), sw(sw), nw(nw) {
+ addEntropy(ne, se, sw, nw);
+ triangulation();
+}
+
+int QuadRoutes::width() { return std::abs(this->ne.x - this->sw.x); }
+
+int QuadRoutes::height() { return std::abs(this->ne.y - this->sw.y); }
+
+bool QuadRoutes::subdivide() {
+ children.reserve(9);
+ int splitXMin = this->sw.x + std::max(4, this->width()*1/4);
+ int splitXMax = this->ne.x - std::max(4, this->width()*1/4);
+ int splitYMin = this->sw.y + std::max(4, this->height()*1/4);
+ int splitYMax = this->ne.y - std::max(4, this->height()*1/4);
+ Vertex split(
+ hashInRange(this->seed, 0, splitXMin, splitXMax),
+ hashInRange(this->seed, 1, splitYMin, splitYMax),
+ 0 // TODO
+ );
+ // TODO : addChild(…);
+ addChild(new Carrefour(split + Vertex(1,1,0), split + Vertex(1,-1,0), split + Vertex(-1,-1,0), split + Vertex(-1,1,0)));
+ // routes au NESW du carrefour
+ // TODO : la plupart des zéros en z sont faux…
+ Vertex roadEndN(split.x, this->ne.y, 0);
+ Vertex roadEndE(this->ne.x, split.y, 0);
+ Vertex roadEndS(split.x, this->sw.y, 0);
+ Vertex roadEndW(this->sw.x, split.y, 0);
+ // TODO : addChild(…);
+ Route* rn = new Route(roadEndN + Vertex(+1,0,0), split + Vertex(+1,+1,0), split + Vertex(-1,+1,0), roadEndN + Vertex(-1,0,0)); // N
+ Route* re = new Route(roadEndE + Vertex(0,+1,0), roadEndE + Vertex(0,-1,0), split + Vertex(+1,-1,0), split + Vertex(+1,+1,0)); // E
+ Route* rs = new Route(split + Vertex(+1,-1,0), roadEndS + Vertex(+1,0,0), roadEndS + Vertex(-1,0,0), split + Vertex(-1,-1,0)); // S
+ Route* rw = new Route(split + Vertex(-1,+1,0), split + Vertex(-1,-1,0), roadEndW + Vertex(0,-1,0), roadEndW + Vertex(0,+1,0)); // W
+ addChild(rn);
+ addChild(re);
+ addChild(rs);
+ addChild(rw);
+ // Sous-quartiers
+ addChild(sub(ne, re->nw)); // sous-quartier NE
+ addChild(sub(re->se, rs->se)); // sous-quartier SE
+ addChild(sub(rs->nw, sw)); // sous-quartier SW
+ addChild(sub(rn->nw, rw->nw)); // sous-quartier NW
+ return true;
+}
+
+void QuadRoutes::triangulation() {
+ triangles.reserve(2);
+ Vertex nw(this->sw.x, this->ne.y, 0);
+ Vertex se(this->ne.x, this->sw.y, 0);
+ addTriangle(new Triangle(this->ne, nw, this->sw, 0xc0, 0xc0, 0xc0));
+ addTriangle(new Triangle(this->sw, se, this->ne, 0xc0, 0xc0, 0xc0));
+}
+
+Chose* QuadRoutes::sub(Vertex ne, Vertex sw) {
+ Segment rect = Segment(ne,sw);
+ if (rect.width() < 10 || rect.height() < 10) {
+ return new Batiment(ne, Vertex(ne.x, sw.y, 0), sw, Vertex(sw.x, ne.y, 0));
+ } else {
+ return new QuadRoutes(ne, Vertex(ne.x, sw.y, 0), sw, Vertex(sw.x, ne.y, 0));
+ }
+}
+
+std::ostream& operator<<(std::ostream& os, const QuadRoutes* r) {
+ return os << *r;
+}
+
+std::ostream& operator<<(std::ostream& os, const QuadRoutes& r) {
+ return os << "QuadRoutes " << r.ne << "-" << r.sw;
+}
diff --git a/rules/quadroutes.hh b/rules/quadroutes.hh
@@ -0,0 +1,27 @@
+#ifndef _RULES_QUADROUTES_HH_
+#define _RULES_QUADROUTES_HH_
+
+#include "all_includes.hh"
+
+// QuadRoutes est un quadrilatère de routes avec des angles aux coins entre 70° et 110°, et des côtés de longueur >= 10.
+class QuadRoutes : public Chose {
+public:
+ Vertex ne;
+ Vertex se;
+ Vertex sw;
+ Vertex nw;
+public:
+ QuadRoutes(Vertex ne, Vertex se, Vertex sw, Vertex nw);
+ int width();
+ int height();
+ virtual bool subdivide();
+ virtual void triangulation();
+private:
+ Chose* sub(Vertex ne, Vertex sw);
+public:
+ friend std::ostream& operator<<(std::ostream& os, const QuadRoutes& r);
+ friend std::ostream& operator<<(std::ostream& os, const QuadRoutes* r);
+};
+
+
+#endif
diff --git a/rules/rectangleroutes.hh b/rules/rectangleroutes.hh
@@ -3,7 +3,7 @@
#include "all_includes.hh"
-// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°.
+// RectangleRoutes est un quadrilatère de routes avec des angles aux coins égaux à 90°, et des côtés de longueur >= 10.
class RectangleRoutes : public Chose {
public:
Vertex ne;