From 7b7575d120894c3aeeafdbdf788aaa9552fdde7c Mon Sep 17 00:00:00 2001 From: Yoann Date: Wed, 21 Dec 2011 10:31:43 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20fonction=20de=20reduction=20d'u?= =?UTF-8?q?n=20segment,=20et=20d'une=20fonction=20permettant=20d'extraire?= =?UTF-8?q?=20un=20parall=C3=A9logramme=20=C3=A0=20partir=20d'un=20quadril?= =?UTF-8?q?at=C3=A8re.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 3 +- quad.cpp | 25 +++++++++++++ quad.hh | 1 + rules/batiment/batimentquad.cpp | 54 +++++++++++++++------------ rules/batiment/batimentquadjardin.cpp | 5 ++- rules/batiment/batimentquadpont.cpp | 4 -- segment.cpp | 5 +++ segment.hh | 3 +- 8 files changed, 70 insertions(+), 30 deletions(-) diff --git a/main.cpp b/main.cpp index 0ba1aba..ba9d6da 100644 --- a/main.cpp +++ b/main.cpp @@ -21,11 +21,12 @@ void recursiveSubdivide(Chose* c) { int main() { // Générer une tile de base std::cout << "Initial seed = " << Chose::initialSeed << std::endl; - int size = 10000; + int size = 20000; Vertex ne(size, size, 0); Vertex se(size, 0, 0); Vertex sw(0, 0, 0); Vertex nw(0, size, 0); + //Chose::initialSeed = 1440907084; Chose* c = QuartierQuad::factory(Chose::initialSeed, 0, ne, se, sw, nw); // c->split(); recursiveSubdivide(c); diff --git a/quad.cpp b/quad.cpp index 750cfcc..b6fddd2 100644 --- a/quad.cpp +++ b/quad.cpp @@ -22,6 +22,31 @@ void Quad::offsetNESW(int offsetN, int offsetE, int offsetS, int offsetW) { this->offset(W,offsetW); } +void Quad::makeParallelogram() { + int l1, l2; + + if(Segment(corner[NW],corner[NE]).length() < Segment(corner[SE],corner[SW]).length()) { + if((l1 = Segment(corner[NE],corner[SE]).length()) < (l2 = Segment(corner[SW],corner[NW]).length())) { + corner[SW] = Segment(corner[NW],corner[SW]).reduce(l1).v; + corner[SE] = corner[SW] + (corner[NE] - corner[NW]); + } + else if((l1 = Segment(corner[NE],corner[SE]).length()) > (l2 = Segment(corner[SW],corner[NW]).length())) { + corner[SE] = Segment(corner[NE],corner[SE]).reduce(l2).v; + corner[SW] = corner[SE] + (corner[NW] - corner[NE]); + } + } + else { + if((l1 = Segment(corner[NE],corner[SE]).length()) < (l2 = Segment(corner[SW],corner[NW]).length())) { + corner[NW] = Segment(corner[SW],corner[NW]).reduce(l1).v; + corner[NE] = corner[NW] + (corner[SE] - corner[SW]); + } + else if((l1 = Segment(corner[NE],corner[SE]).length()) > (l2 = Segment(corner[SW],corner[NW]).length())) { + corner[NE] = Segment(corner[SE],corner[NE]).reduce(l2).v; + corner[NW] = corner[NE] + (corner[SW] - corner[SE]); + } + } +} + int Quad::minLengthNS() { return std::min( Segment(corner[NW],corner[NE]).length(), diff --git a/quad.hh b/quad.hh index 77f94e7..6382b87 100644 --- a/quad.hh +++ b/quad.hh @@ -21,6 +21,7 @@ class Quad { int maxLength(); float minAngle(); float maxAngle(); + void makeParallelogram(); }; diff --git a/rules/batiment/batimentquad.cpp b/rules/batiment/batimentquad.cpp index c2e43ed..3e2c233 100644 --- a/rules/batiment/batimentquad.cpp +++ b/rules/batiment/batimentquad.cpp @@ -40,33 +40,41 @@ bool BatimentQuad::merge() { } Chose* BatimentQuad::factory(int seed, int n, Vertex ne, Vertex se, Vertex sw, Vertex nw) { - int th = 20; // Terrain height. - Quad q = Quad(ne,se,sw,nw); - seed = seed; - n = n; - th = th; - q.offset(N,-140); - q.offset(E,-140); - q.offset(S,-140); - q.offset(W,-140); -/* - addChild(new TrottoirQuadNormal(lctr+ne,lctr+se,lctr+q.corner[1],lctr+q.corner[0],th,E)); - addChild(new TrottoirQuadNormal(lctr+se,lctr+sw,lctr+q.corner[2],lctr+q.corner[1],th,E)); - addChild(new TrottoirQuadNormal(lctr+sw,lctr+nw,lctr+q.corner[3],lctr+q.corner[2],th,E)); - addChild(new TrottoirQuadNormal(lctr+nw,lctr+ne,lctr+q.corner[0],lctr+q.corner[3],th,E)); + int rand = this->seed % 20; - q.corner[0] = q.corner[0] + Vertex(0,0,th); - q.corner[1] = q.corner[1] + Vertex(0,0,th); - q.corner[2] = q.corner[2] + Vertex(0,0,th); - q.corner[3] = q.corner[3] + Vertex(0,0,th); + if(rand <= 2) { + Quad q = Quad(ne,se,sw,nw); + addChild(new BatimentQuadPont(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3],300)); + } + else if(rand <= 15) { + int th = 20; // Terrain height. + Quad q = Quad(ne,se,sw,nw); + seed = seed; + n = n; + th = th; + q.offset(N,-140); + q.offset(E,-140); + q.offset(S,-140); + q.offset(W,-140); - addChild(new BatimentQuadJardin(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3])); + addChild(new TrottoirQuadNormal(lctr+ne,lctr+se,lctr+q.corner[1],lctr+q.corner[0],th,E)); + addChild(new TrottoirQuadNormal(lctr+se,lctr+sw,lctr+q.corner[2],lctr+q.corner[1],th,E)); + addChild(new TrottoirQuadNormal(lctr+sw,lctr+nw,lctr+q.corner[3],lctr+q.corner[2],th,E)); + addChild(new TrottoirQuadNormal(lctr+nw,lctr+ne,lctr+q.corner[0],lctr+q.corner[3],th,E)); - q.offset(this->entry,-400); + q.corner[0] = q.corner[0] + Vertex(0,0,th); + q.corner[1] = q.corner[1] + Vertex(0,0,th); + q.corner[2] = q.corner[2] + Vertex(0,0,th); + q.corner[3] = q.corner[3] + Vertex(0,0,th); + + q.makeParallelogram(); + addChild(new BatimentQuadJardin(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3])); + + q.offset(this->entry,-400); + + addChild(new BatimentQuadMaison(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3])); + } - addChild(new BatimentQuadMaison(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3])); -*/ - addChild(new BatimentQuadPont(lctr+q.corner[0],lctr+q.corner[1],lctr+q.corner[2],lctr+q.corner[3],300)); return NULL; // pour compilation, à virer. } diff --git a/rules/batiment/batimentquadjardin.cpp b/rules/batiment/batimentquadjardin.cpp index 33943e6..43dca89 100644 --- a/rules/batiment/batimentquadjardin.cpp +++ b/rules/batiment/batimentquadjardin.cpp @@ -43,5 +43,8 @@ void BatimentQuadJardin::triangulation() { triangles.reserve(2); addTriangle(new Triangle(lctr+ne,lctr+nw,lctr+sw,0x12,0x64,0x12)); - addTriangle(new Triangle(lctr+sw,lctr+se,lctr+ne,0x10,0x60,0x10)); + addTriangle(new Triangle(lctr+sw,lctr+se,lctr+ne,0x12,0x64,0x12)); + + //addTriangle(new Triangle(lctr+ne,lctr+nw,lctr+sw,0xFF,0xFF,0x00)); + //addTriangle(new Triangle(lctr+sw,lctr+se,lctr+ne,0xFF,0xFF,0x00)); } diff --git a/rules/batiment/batimentquadpont.cpp b/rules/batiment/batimentquadpont.cpp index c7bfdb7..8bb1dfb 100644 --- a/rules/batiment/batimentquadpont.cpp +++ b/rules/batiment/batimentquadpont.cpp @@ -68,14 +68,10 @@ void BatimentQuadPont::triangulation() { int middle = steps/2; int n; - std::cout << std::endl << l1.norm() << std::endl; - addTriangle(new Triangle(lctr+pb,lctr+sw,lctr+swh,0x10,0xFF,0x10)); addTriangle(new Triangle(lctr+pa,lctr+nw,lctr+nwh,0x10,0xFF,0x10)); for(var=-1.7,n=0; var <= 1.7; var+=pas,n++) { - std::cout << var << std::endl; - //std::cout << var << " \t " << nt(var,height) << std::endl; q.offset(W,-n2); a = q.corner[3] + Vertex(0,0,nt(var,height)); b = q.corner[2] + Vertex(0,0,nt(var,height)); diff --git a/segment.cpp b/segment.cpp index ac24eba..b3717ff 100644 --- a/segment.cpp +++ b/segment.cpp @@ -6,6 +6,11 @@ int Segment::length() { return (u-v).norm(); } +Segment Segment::reduce(int value) { + float reduc = (float)length()/(float)value; + return Segment(u,u+((v - u) / reduc)); +} + int Segment::width() { return std::abs(u.x - v.x); } diff --git a/segment.hh b/segment.hh index ed7f4e7..74cc8b4 100644 --- a/segment.hh +++ b/segment.hh @@ -4,7 +4,7 @@ #include "all_includes.hh" class Segment { - private : + public : Vertex u; Vertex v; @@ -13,6 +13,7 @@ class Segment { int length(); int width(); int height(); + Segment reduce(int value); Vertex randomPos(int seed, int n, int a, int b); // Renvoir un vertex sur le segment [u,v], à une position entre a% and b%. };