commit d9dcbdb206e07e95aaa997733db67a284484172e
parent c136313efbde4467d3ba005f9f344abaa7a4efae
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Tue, 10 Jan 2012 12:13:50 +0100
Méthode Quad::randomPoint .
Diffstat:
8 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/all_includes.hh b/all_includes.hh
@@ -54,9 +54,7 @@ class Chose;
#include "rules/quartier/quartiertritrapeze.hh"
#include "rules/quartier/quartiertricentre.hh"
-#include "rules/route/routequadcarrefour.hh"
#include "rules/route/routequadchaussee.hh"
-#include "rules/route/routetrichaussee.hh"
#include "rules/route/trottoirquadnormal.hh"
diff --git a/geometry/quad.cpp b/geometry/quad.cpp
@@ -33,7 +33,6 @@ Quad Quad::insetOpp(Cardinal side, float offset) const {
qb = qb.inset(N,offset);
return Quad(q[NE],qb[NE],qb[NW],q[NW]);
-
}
Quad Quad::makeParallelogram() const {
@@ -112,12 +111,20 @@ Quad operator+(const Quad& q, const Vertex& v) {
return Quad(q[NE] + v, q[SE] + v, q[SW] + v, q[NW] + v);
}
-/*
-void Quad::cutCornerCorner(Coin from, float cutwidth) const {
- Triangle left = Triangle(c[from-2], c[from-1], c[from]).offset(BASE, cutwidth);
- Triangle right = Triangle(c[from], c[from+1], c[from+2]).offset(BASE, cutwidth);
- Quad cut(right[LEFT], right[RIGHT], left[LEFT], left[RIGHT]); // + c[from+2] avant le 1er sommet, et c[from] après le 2e.
- Triangle cutFrom(left[RIGHT], c[from], right[LEFT]);
- Triangle cutTo(right[LEFT], c[from+2], left[RIGHT]);
+Vertex Quad::randomPoint(int seed, int n) const {
+ Triangle ne(c[NW], c[NE], c[SE]);
+ Triangle sw(c[SE], c[SW], c[NW]);
+ float surfacene = ne.surface();
+ float surfacesw = sw.surface();
+ if (proba(seed, n, surfacene, surfacene + surfacesw)) {
+ return ne.randomPoint(seed, hash2(n, 42));
+ } else {
+ return sw.randomPoint(seed, hash2(n, 42));
+ }
+}
+
+float Quad::surface() const {
+ Triangle ne(c[NW], c[NE], c[SE]);
+ Triangle sw(c[SE], c[SW], c[NW]);
+ return ne.surface() + sw.surface();
}
-*/
diff --git a/geometry/quad.hh b/geometry/quad.hh
@@ -37,6 +37,8 @@ class Quad {
float maxLength() const;
float minAngle() const;
float maxAngle() const;
+ Vertex randomPoint(int seed, int n) const;
+ float surface() const;
//void cutCornerCorner(Coin from) const;
Quad makeParallelogram() const;
};
diff --git a/geometry/triangle.cpp b/geometry/triangle.cpp
@@ -51,7 +51,13 @@ Triangle operator+(const Triangle& t, const Vertex& v) {
}
Vertex Triangle::randomPoint(int seed, int n) const {
- float rndl = floatInRange(seed, n, 0, 100);
- float rndr = floatInRange(seed, hash2(n, 42), 0, 100 - rndl);
- return c[TOP] + (c[LEFT] - c[TOP]) * (rndl/100.f) + (c[RIGHT] - c[TOP]) * (rndr/100.f);
+ float rndl = floatInRange(seed, n, 0, 1);
+ float rndr = floatInRange(seed, hash2(n, 42), 0, 1 - rndl);
+ return c[TOP] + (c[LEFT] - c[TOP]) * (rndl) + (c[RIGHT] - c[TOP]) * (rndr);
+}
+
+float Triangle::surface() const {
+ float hauteur = Segment(c[TOP], (c[TOP] - c[LEFT]).projectOn(c[RIGHT] - c[LEFT])).length();
+ float base = Segment(c[LEFT], c[RIGHT]).length();
+ return (base * hauteur) / 2.f;
}
diff --git a/geometry/triangle.hh b/geometry/triangle.hh
@@ -29,6 +29,7 @@ class Triangle {
float maxAngle() const; // angle maximum du triangle (en LEFT, TOP ou RIGHT).
float minLength() const;
float maxLength() const;
+ float surface() const;
Triangle inset(CoteTriangle side, float offset) const;
Triangle insetLTR(float offset) const;
Vertex randomPoint(int seed, int n) const;
diff --git a/hash.cpp b/hash.cpp
@@ -29,8 +29,8 @@ float floatInRange(int seed, int n, float a, float b) {
return (float)(hash2(seed, n) & 0xffffff) / (float)(0x1000000) * (b-a) + a;
}
-bool proba(int seed, int n, unsigned int a, unsigned int b) {
- return ((hash2(seed, n) % b) < a);
+bool proba(int seed, int n, float a, float b) {
+ return floatInRange(seed, n, 0, b) < a;
}
unsigned int float2uint(float f) {
diff --git a/hash.hh b/hash.hh
@@ -7,7 +7,7 @@ int random_seed();
unsigned int hash2(unsigned int a, unsigned int b);
float floatInRange(int seed, int n, float a, float b); // Renvoie le n-ième nombre aléatoire dérivé de seed entre a et b (a inclus, b non inclus).
-bool proba(int seed, int n, unsigned int a, unsigned int b); // Renvoie vrai avec `a` fois sur `b`.
+bool proba(int seed, int n, float a, float b); // Renvoie vrai avec `a` fois sur `b`.
typedef union FloatUIntUnion {
float f;
unsigned int ui;
diff --git a/main.cpp b/main.cpp
@@ -5,7 +5,7 @@
int main() {
// Générer une tile de base
std::cout << "Initial seed = " << Chose::initialSeed << std::endl;
- float size = 20000;
+ float size = 10;
Vertex ne(size, size, 0);
Vertex se(size, 0, 0);
Vertex sw(0, 0, 0);