Refactor pour séparer chaque règle dans deux fichiers .cpp et .hh .

This commit is contained in:
Georges Dupéron 2011-11-06 21:30:14 +01:00
parent 0f0a6aa6c0
commit dcf4500732
16 changed files with 249 additions and 132 deletions

3
.gitignore vendored
View File

@ -2,6 +2,7 @@ simple-terrain
display
roam
roads
rules
city
*.o
*.d
.*.d

View File

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

View File

@ -1,5 +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

100
rules.cpp
View File

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

30
rules.h
View File

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

23
rules.hh Normal file
View File

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

17
rules/carrefour.cpp Normal file
View File

@ -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];
}

18
rules/carrefour.hh Normal file
View File

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

45
rules/rectangleroutes.cpp Normal file
View File

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

25
rules/rectangleroutes.hh Normal file
View File

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

19
rules/route.cpp Normal file
View File

@ -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];
}

18
rules/route.hh Normal file
View File

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

11
segment.cpp Normal file
View File

@ -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);
}

15
segment.hh Normal file
View File

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

16
vertex.cpp Normal file
View File

@ -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 << ")";
}

19
vertex.hh Normal file
View File

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