Generate seeded random values to produce stable output for test cases.

master
Torsten Paul 2015-03-03 21:29:43 +01:00
parent dc160d410d
commit ffb0129d03
1 changed files with 11 additions and 5 deletions

View File

@ -13,8 +13,13 @@ thickness = 5; // thickness of the first segment
// the identity matrix
identity = [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];
// random generator
function rnd(s, e) = rands(s, e, 1)[0];
// random generator, to generate always the same output for the example,
// this uses a seed for rands() and stores the array of random values in
// the random variable. To generate different output, remove the seed or
// replace the function rnd() to just call rands(s, e, 1)[0].
rcnt = 1000;
random = rands(0, 1, rcnt, 18);
function rnd(s, e, r) = random[r % rcnt] * (e - s) + s;
// generate 4x4 translation matrix
function mt(x, y) = [ [ 1, 0, 0, x ], [ 0, 1, 0, y ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];
@ -22,14 +27,15 @@ function mt(x, y) = [ [ 1, 0, 0, x ], [ 0, 1, 0, y ], [ 0, 0, 1, 0 ], [ 0, 0, 0,
// generate 4x4 rotation matrix around Z axis
function mr(a) = [ [ cos(a), -sin(a), 0, 0 ], [ sin(a), cos(a), 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];
module tree(length, thickness, count, m = identity) {
module tree(length, thickness, count, m = identity, r = 1) {
echo(r);
color([0, 1 - (0.8 / levels * count), 0])
multmatrix(m)
cube([thickness, length, thickness]);
if (count > 0) {
tree(rnd(0.6, 0.8) * length, 0.8 * thickness, count - 1, m * mt(0, length) * mr(rnd(20, 35)));
tree(rnd(0.6, 0.8) * length, 0.8 * thickness, count - 1, m * mt(0, length) * mr(-rnd(20, 35)));
tree(rnd(0.6, 0.8, r) * length, 0.8 * thickness, count - 1, m * mt(0, length) * mr(rnd(20, 35, r + 1)), 8 * r);
tree(rnd(0.6, 0.8, r + 1) * length, 0.8 * thickness, count - 1, m * mt(0, length) * mr(-rnd(20, 35, r + 3)), 8 * r + 4);
}
}