1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 14:18:13 +01:00

experimental t2d

This commit is contained in:
Justin Lin 2019-09-28 18:05:06 +08:00
parent 7488f62df7
commit 4fad8da2fa
2 changed files with 103 additions and 17 deletions

View File

@ -1,37 +1,45 @@
include <line2d.scad>;
include <polyline2d.scad>;
include <turtle/turtle2d.scad>;
include <turtle/t2d.scad>;
side_len = 100;
min_len = 4;
side_leng = 100;
min_leng = 4;
thickness = 0.5;
sierpinski_triangle(
turtle2d("create", 0, 0, 0),
side_len, min_len, thickness, $fn = 12
t2d(point = [0, 0], angle = 0),
side_leng, min_leng, thickness, $fn = 12
);
module triangle(t, side_leng, thickness) {
t2 = turtle2d("forward", t, side_leng);
t3 = turtle2d("forward", turtle2d("turn", t2, 120), side_leng);
t2 = t2d(t, "forward", leng = side_leng);
t3 = t2d(t2, cmds = [
["turn", 120],
["forward", side_leng]
]);
polyline2d([
turtle2d("pt", t),
turtle2d("pt", t2),
turtle2d("pt", t3),
turtle2d("pt", t)
t2d(t, "point"),
t2d(t2, "point"),
t2d(t3, "point"),
t2d(t, "point")
], thickness, startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND");
}
module sierpinski_triangle(t, side_len, min_len, thickness) {
triangle(t, side_len, thickness);
module sierpinski_triangle(t, side_leng, min_leng, thickness) {
triangle(t, side_leng, thickness);
if(side_len >= min_len) {
half_len = side_len / 2;
t2 = turtle2d("forward", t, half_len);
t3 = turtle2d("turn", turtle2d("forward", turtle2d("turn", t, 60), half_len), -60);
if(side_leng >= min_leng) {
half_leng = side_leng / 2;
t2 = t2d(t, "forward", leng = half_leng);
t3 = t2d(t, cmds = [
["turn", 60],
["forward", half_leng],
["turn", -60]
]);
for(turtle = [t, t2, t3]) {
sierpinski_triangle(turtle, half_len, min_len, thickness);
sierpinski_triangle(turtle, half_leng, min_leng, thickness);
}
}
}

78
src/turtle/t2d.scad Normal file
View File

@ -0,0 +1,78 @@
/**
* turtle2d.scad
*
* @copyright Justin Lin, 2019
* @license https://opensource.org/licenses/lgpl-3.0.html
*
* @see https://openhome.cc/eGossip/OpenSCAD/lib2-t2d.html
*
**/
function _t2d_turtle(point, angle) = [point, angle];
function _t2d_set_point(turtle, point) = [point, _t2d_get_angle(turtle)];
function _t2d_set_x(turtle, x) = [[x, _t2d_get_y(turtle)], _t2d_get_angle(turtle)];
function _t2d_set_y(turtle, y) = [[_t2d_get_x(turtle), y], _t2d_get_angle(turtle)];
function _t2d_set_angle(turtle, angle) = [_t2d_get_pt(turtle), angle];
function _t2d_forward(turtle, leng) =
_t2d_turtle(
[
_t2d_get_x(turtle) + leng * cos(_t2d_get_angle(turtle)),
_t2d_get_y(turtle) + leng * sin(_t2d_get_angle(turtle))
],
_t2d_get_angle(turtle)
);
function _t2d_turn(turtle, angle) = [_t2d_get_pt(turtle), _t2d_get_angle(turtle) + angle];
function _t2d_get_x(turtle) = turtle[0][0];
function _t2d_get_y(turtle) = turtle[0][1];
function _t2d_get_pt(turtle) = turtle[0];
function _t2d_get_angle(turtle) = turtle[1];
function _t2d_two_args_command(cmd, arg1, arg2) =
is_undef(arg2) ? _t2d_one_arg_command(cmd, arg1) :
cmd == "point" ? _t2d_set_point(arg1, arg2) :
cmd == "x" ? _t2d_set_x(arg1, arg2) :
cmd == "y" ? _t2d_set_y(arg1, arg2) :
cmd == "angle" ? _t2d_set_angle(arg1, arg2) :
cmd == "forward" ? _t2d_forward(arg1, arg2) :
cmd == "turn" ? _t2d_turn(arg1, arg2) : assert(false, "unknown command");
function _t2d_one_arg_command(cmd, arg) =
cmd == "x" ? _t2d_get_x(arg) :
cmd == "y" ? _t2d_get_y(arg) :
cmd == "angle" ? _t2d_get_angle(arg) :
cmd == "point" ? _t2d_get_pt(arg) : assert(false, "unknown command");
function _t2d_cmdline(cmd, arg1, arg2) =
_t2d_two_args_command(cmd, arg1, arg2);
function _t2d_set(t, point, angle, x, y) =
!is_undef(point) ? _t2d_set_point(t, point) :
!is_undef(angle) ? _t2d_set_angle(t, angle) :
!is_undef(x) ? _t2d_set_x(t, x) :
!is_undef(y) ? _t2d_set_y(t, y) :
assert(false, "no target to set");
function _t2d_cmds(t, cmds, i = 0) =
i == len(cmds) ? t :
let(
cmd = cmds[i][0],
arg = cmds[i][1]
)
_t2d_cmds(_t2d_cmdline(cmd, t, arg), cmds, i + 1);
function t2d(t, cmd, cmds, point, angle, x, y, leng) =
is_undef(t) ? _t2d_turtle(point, angle) :
cmd == "set" ? _t2d_set(t, point, angle, x, y) :
cmd == "forward" ? _t2d_forward(t, leng) :
cmd == "turn" ? _t2d_turn(t, angle) :
cmd == "point" ? _t2d_get_pt(t) :
cmd == "angle" ? _t2d_get_angle(t) :
cmd == "x" ? _t2d_get_x(t) :
cmd == "y" ? _t2d_get_y(t) :
!is_undef(cmds) ? _t2d_cmds(t, cmds) :
assert(false, "unknown command");