From b6eb09dc656bcc0f05c276319e411e81fb1ca8f2 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 23 Apr 2021 08:02:14 +0800 Subject: [PATCH] sum up totalWeights directly --- src/experimental/_impl/_tiles_wfc_impl.scad | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/experimental/_impl/_tiles_wfc_impl.scad b/src/experimental/_impl/_tiles_wfc_impl.scad index 788e3a89..cb5d8a63 100644 --- a/src/experimental/_impl/_tiles_wfc_impl.scad +++ b/src/experimental/_impl/_tiles_wfc_impl.scad @@ -1,6 +1,5 @@ use ; use ; -use ; use ; use ; use ; @@ -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(