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

simplify commands

This commit is contained in:
Justin Lin 2017-03-31 09:04:27 +08:00
parent 6f2441d7eb
commit ed0271cc8c
3 changed files with 61 additions and 24 deletions

View File

@ -6,33 +6,32 @@ An OpenSCAD implementation of Turtle Graphics. It moves on the xy plane. You can
- `cmd` : A string command. Different commands use different numbers of arguments. - `cmd` : A string command. Different commands use different numbers of arguments.
- `"create"` : Creates a turtle data. The command needs three arguments `x`, `y` and `angle`. For example, use `turtle2d("create", 5, 10, 30)` to create a turtle located at `[0, 0]` with an angle `30` degrees. - `"create"` : Creates a turtle data. The command needs three arguments `x`, `y` and `angle`. For example, use `turtle2d("create", 5, 10, 30)` to create a turtle located at `[0, 0]` with an angle `30` degrees.
- `"set_x"` : Sets the `x` coordinate of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the `x` coordinate. For example, `turtle2d("set_x", turtle, 20)`. - `"x"` : Sets or gets the `x` coordinate of a turtle. If you provide a turtle data and the `x` coordinate, such as `turtle2d("x", turtle, 20)`, it will sets the `x` coordinate. Giving a turtle data only will return the `x` coordinate.
- `"set_y"` : Sets the `y` coordinate of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the `y` coordinate. For example, `turtle2d("set_y", turtle, 20)`. - `"y"` : Sets or gets the `y` coordinate of a turtle. If you provide a turtle data and the `y` coordinate, such as `turtle2d("y", turtle, 20)`, it will sets the `y` coordinate. Giving a turtle data only will return the `y` coordinate.
- `"set_a"` : Sets the angle of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the angle. For example, `turtle2d("set_a", turtle, 45)`. - `"angle"` : Sets or gets the angle of a turtle. If you provide a turtle data and the angle, such as `turtle2d("angle", turtle, 45)`, it will sets the angle. Giving a turtle data only will return the angle.
- `"set_pt"` : Sets `[x, y]` of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is `[x, y]`. For example, `turtle2d("set_pt", turtle, [x, y])`. - `"pt"` : Sets or Gets `[x, y]` of a turtle. If you provide a turtle data and the coordinates, such as `turtle2d("pt", turtle, [10, 20])`, it will sets the coordinates. Giving a turtle data only will return the coordinates.
- `"forward"` : Forwards a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the length. For example, `turtle2d("forward", turtle, 100)`. - `"forward"` : Forwards a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the length. For example, `turtle2d("forward", turtle, 100)`.
- `"forward"` : Turns a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the angle. For example, `turtle2d("turn", turtle, 180)`. - `"forward"` : Turns a turtle. The command needs two arguments. The first one is a turtle data, and the second one is the angle. For example, `turtle2d("turn", turtle, 180)`.
- `"get_x"`, `"get_y"`, `"get_a"`, `"get_pt"` : All these commands needs only one argument, the turtle data.
## Examples ## Examples
include <line2d.scad>; include <line2d.scad>;
include <turtle2d.scad>; include <turtle2d.scad>;
module turtle_spiral(t_before, times, side_leng, angle, width) { module turtle_spiral(t_before, times, side_leng, angle, width) {
$fn = 24; $fn = 24;
if(times != 0) { if(times != 0) {
t_after_tr = turtle2d("turn", t_before, angle); t_after_tr = turtle2d("turn", t_before, angle);
t_after_fd = turtle2d("forward", t_after_tr, side_leng); t_after_fd = turtle2d("forward", t_after_tr, side_leng);
line2d( line2d(
turtle2d("get_pt", t_before), turtle2d("pt", t_before),
turtle2d("get_pt", t_after_fd), turtle2d("pt", t_after_fd),
width, width,
p1Style = "CAP_ROUND", p1Style = "CAP_ROUND",
p2Style = "CAP_ROUND" p2Style = "CAP_ROUND"
); );
turtle_spiral(t_after_fd, times - 1, side_leng, angle, width); turtle_spiral(t_after_fd, times - 1, side_leng, angle, width);
} }
@ -56,8 +55,8 @@ An OpenSCAD implementation of Turtle Graphics. It moves on the xy plane. You can
t_after = turtle2d("forward", turtle2d("turn", t_before, angle), side_leng); t_after = turtle2d("forward", turtle2d("turn", t_before, angle), side_leng);
line2d( line2d(
turtle2d("get_pt", t_before), turtle2d("pt", t_before),
turtle2d("get_pt", t_after), turtle2d("pt", t_after),
width, width,
p1Style = "CAP_ROUND", p1Style = "CAP_ROUND",
p2Style = "CAP_ROUND" p2Style = "CAP_ROUND"

36
src/test.scad Normal file
View File

@ -0,0 +1,36 @@
include <line2d.scad>;
include <turtle2d.scad>;
module turtle_spiral(t_before, side_leng, d_step, min_leng, angle, width) {
$fn = 24;
if(side_leng > min_leng) {
t_after = turtle2d("forward", turtle2d("turn", t_before, angle), side_leng);
line2d(
turtle2d("pt", t_before),
turtle2d("pt", t_after),
width,
p1Style = "CAP_ROUND",
p2Style = "CAP_ROUND"
);
turtle_spiral(t_after, side_leng - d_step, d_step, min_leng, angle, width);
}
}
side_leng = 50;
d_step = 1;
min_leng = 1;
angle = 90;
width = 1;
turtle_spiral(
turtle2d("create", 0, 0, 0),
side_leng,
d_step,
min_leng,
angle,
width
);

View File

@ -39,12 +39,14 @@ function _turtle2d_three_args_command(cmd, arg1, arg2, arg3) =
cmd == "create" ? _turtle2d_turtle(arg1, arg2, arg3) : _turtle2d_two_args_command(cmd, arg1, arg2); cmd == "create" ? _turtle2d_turtle(arg1, arg2, arg3) : _turtle2d_two_args_command(cmd, arg1, arg2);
function _turtle2d_two_args_command(cmd, arg1, arg2) = function _turtle2d_two_args_command(cmd, arg1, arg2) =
cmd == "set_pt" ? _turtle2d_set_point(arg1, arg2) : ( arg2 == undef ? _turtle2d_one_arg_command(cmd, arg1) : (
cmd == "set_x" ? _turtle2d_set_x(arg1, arg2) : ( cmd == "pt" ? _turtle2d_set_point(arg1, arg2) : (
cmd == "set_y" ? _turtle2d_set_y(arg1, arg2) : ( cmd == "x" ? _turtle2d_set_x(arg1, arg2) : (
cmd == "set_a" ? _turtle2d_set_angle(arg1, arg2) : ( cmd == "y" ? _turtle2d_set_y(arg1, arg2) : (
cmd == "forward" ? _turtle2d_forward(arg1, arg2) : ( cmd == "angle" ? _turtle2d_set_angle(arg1, arg2) : (
cmd == "turn" ? _turtle2d_turn(arg1, arg2) : _turtle2d_one_arg_command(cmd, arg1) cmd == "forward" ? _turtle2d_forward(arg1, arg2) : (
cmd == "turn" ? _turtle2d_turn(arg1, arg2) : undef
)
) )
) )
) )
@ -52,10 +54,10 @@ function _turtle2d_two_args_command(cmd, arg1, arg2) =
); );
function _turtle2d_one_arg_command(cmd, arg) = function _turtle2d_one_arg_command(cmd, arg) =
cmd == "get_x" ? _turtle2d_get_x(arg) : ( cmd == "x" ? _turtle2d_get_x(arg) : (
cmd == "get_y" ? _turtle2d_get_y(arg) : ( cmd == "y" ? _turtle2d_get_y(arg) : (
cmd == "get_a" ? _turtle2d_get_angle(arg) : ( cmd == "angle" ? _turtle2d_get_angle(arg) : (
cmd == "get_pt" ? _turtle2d_get_pt(arg) : undef cmd == "pt" ? _turtle2d_get_pt(arg) : undef
) )
) )
); );