1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-05 22:37:35 +02:00

sum up totalWeights directly

This commit is contained in:
Justin Lin
2021-04-23 08:02:14 +08:00
parent 675a3ef519
commit b6eb09dc65

View File

@@ -1,6 +1,5 @@
use <util/flat.scad>;
use <util/has.scad>;
use <util/sum.scad>;
use <util/rand.scad>;
use <util/slice.scad>;
use <util/some.scad>;
@@ -83,12 +82,16 @@ function wf_collapse(wf, x, y) =
if(w != undef)
[state, w]
],
totalWeights = sum([for(w = weights_xy) w[1]]),
totalWeights = _totalWeights(weights_xy, len(weights_xy)),
threshold = rand() * totalWeights,
states_weights = weights_xy
)
_wf_collapse(wf, x, y, states_weights, len(states_weights), threshold);
function _totalWeights(weights_xy, leng, i = 0) =
i == leng ? 0 :
weights_xy[i][1] + _totalWeights(weights_xy, leng, i + 1);
function _wf_collapse(wf, x, y, states_weights, leng, threshold, i = 0) =
i == leng ? wf :
let(