1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-26 16:04:46 +02:00
This commit is contained in:
Justin Lin
2020-04-13 08:51:30 +08:00
parent a89c56d36b
commit 2f796164e3
2 changed files with 26 additions and 36 deletions

View File

@@ -18,48 +18,41 @@ function _derive(base, rule, n, i = 0) =
function derive(rule, n) = function derive(rule, n) =
_derive(assoc_lookup(rule, "S"), rule, n); _derive(assoc_lookup(rule, "S"), rule, n);
function cmd(symbol, args) = function _next_stack(t, code, stack) =
symbol == "F" ? ["forward", args[0]] : code == "[" ? concat([t], stack) :
symbol == "M" ? ["move", args[0]] :
symbol == "+" ? ["turn", args[1]] :
symbol == "-" ? ["turn", -args[1]] :
symbol == "[" ? ["push"] :
symbol == "]" ? ["pop"] : [];
function _fd_if_mv(cmd) =
cmd == "move" ? "forward" : cmd;
function _next_stack(t, cmd, stack) =
cmd[0] == "push" ? concat([t], stack) :
let(leng = len(stack)) let(leng = len(stack))
cmd[0] == "pop" ? code == "]" ?
(leng > 1 ? [for(i = [1:leng - 1]) stack[i]] : []) : (leng > 1 ? [for(i = [1:leng - 1]) stack[i]] : []) :
stack; stack;
function _next_t1(t1, t2, cmd, stack) = function _next_t1(t1, t2, code, stack) =
cmd[0] == "push" ? t1 : code == "[" ? t1 :
cmd[0] == "pop" ? stack[0] : t2; code == "]" ? stack[0] : t2;
function _next_t2(t, cmd) = function _next_t2(t, code, angle, leng) =
is_undef(cmd) || cmd[0] == "push" || cmd[0] == "pop" ? t : turtle2d(_fd_if_mv(cmd[0]), t, cmd[1]); is_undef(code) || code == "[" || code == "]" ? t :
code == "F" ? turtle2d("forward", t, leng) :
code == "M" ? turtle2d("forward", t, leng) :
code == "+" ? turtle2d("turn", t, angle) :
code == "-" ? turtle2d("turn", t, -angle) : t;
// It doesn't use recursion to avoid recursion error. // It doesn't use recursion to avoid recursion error.
function _lines(t, cmds) = function _lines(t, codes, angle, leng) =
let(leng = len(cmds)) let(codes_leng = len(codes))
[ [
for( for(
i = 0, i = 0,
stack = [], stack = [],
t1 = t, t1 = t,
t2 = _next_t2(t1, cmds[i]); t2 = _next_t2(t1, codes[i], angle, leng);
i < leng; i < codes_leng;
t1 = _next_t1(t1, t2, cmds[i], stack), t1 = _next_t1(t1, t2, codes[i], stack),
stack = _next_stack(t1, cmds[i], stack), stack = _next_stack(t1, codes[i], stack),
i = i + 1, i = i + 1,
t2 = _next_t2(t1, cmds[i]) t2 = _next_t2(t1, codes[i], angle, leng)
) )
if(cmds[i][0] != "move" && cmds[i][0] != "push" && cmds[i][0] != "pop") if(search(codes[i], "F+-") != [])
[turtle2d("pt", t1), turtle2d("pt", t2)] [turtle2d("pt", t1), turtle2d("pt", t2)]
]; ];

View File

@@ -4,18 +4,15 @@ use <turtle/turtle2d.scad>;
function lsystem2(rule, n, angle, leng = 1, heading = 0, start = [0, 0], forward_chars = "F") = function lsystem2(rule, n, angle, leng = 1, heading = 0, start = [0, 0], forward_chars = "F") =
let( let(
derived = derive(rule, n), derived = derive(rule, n),
replaced = _join([ codes = _join([
for(c = derived) for(c = derived)
let(idx = search(c, forward_chars)) let(idx = search(c, forward_chars))
idx == [] ? c : "F" idx == [] ? c : "F"
]), ])
cmds = [
for(s = replaced)
let(c = cmd(s, [leng, angle]))
if(c != []) c
]
) )
_lines( _lines(
turtle2d("create", start[0], start[0], heading), turtle2d("create", start[0], start[0], heading),
cmds codes,
angle,
leng
); );