From ed0271cc8c6edc291ec08080522341d6902c9d86 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 31 Mar 2017 09:04:27 +0800 Subject: [PATCH] simplify commands --- docs/lib-turtle2d.md | 27 +++++++++++++-------------- src/test.scad | 36 ++++++++++++++++++++++++++++++++++++ src/turtle2d.scad | 22 ++++++++++++---------- 3 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 src/test.scad diff --git a/docs/lib-turtle2d.md b/docs/lib-turtle2d.md index fdcb3046..d5795b9c 100644 --- a/docs/lib-turtle2d.md +++ b/docs/lib-turtle2d.md @@ -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. - `"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)`. - - `"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)`. - - `"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)`. - - `"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])`. + - `"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. + - `"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. + - `"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. + - `"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"` : 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 - + include ; - include ; + include ; module turtle_spiral(t_before, times, side_leng, angle, width) { - $fn = 24; + $fn = 24; if(times != 0) { t_after_tr = turtle2d("turn", t_before, angle); t_after_fd = turtle2d("forward", t_after_tr, side_leng); - + line2d( - turtle2d("get_pt", t_before), - turtle2d("get_pt", t_after_fd), + turtle2d("pt", t_before), + turtle2d("pt", t_after_fd), width, p1Style = "CAP_ROUND", p2Style = "CAP_ROUND" ); - + 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); line2d( - turtle2d("get_pt", t_before), - turtle2d("get_pt", t_after), + turtle2d("pt", t_before), + turtle2d("pt", t_after), width, p1Style = "CAP_ROUND", p2Style = "CAP_ROUND" diff --git a/src/test.scad b/src/test.scad new file mode 100644 index 00000000..46474df0 --- /dev/null +++ b/src/test.scad @@ -0,0 +1,36 @@ + include ; + include ; + + 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 + ); \ No newline at end of file diff --git a/src/turtle2d.scad b/src/turtle2d.scad index b9c5f867..e0918632 100644 --- a/src/turtle2d.scad +++ b/src/turtle2d.scad @@ -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); function _turtle2d_two_args_command(cmd, arg1, arg2) = - cmd == "set_pt" ? _turtle2d_set_point(arg1, arg2) : ( - cmd == "set_x" ? _turtle2d_set_x(arg1, arg2) : ( - cmd == "set_y" ? _turtle2d_set_y(arg1, arg2) : ( - cmd == "set_a" ? _turtle2d_set_angle(arg1, arg2) : ( - cmd == "forward" ? _turtle2d_forward(arg1, arg2) : ( - cmd == "turn" ? _turtle2d_turn(arg1, arg2) : _turtle2d_one_arg_command(cmd, arg1) + arg2 == undef ? _turtle2d_one_arg_command(cmd, arg1) : ( + cmd == "pt" ? _turtle2d_set_point(arg1, arg2) : ( + cmd == "x" ? _turtle2d_set_x(arg1, arg2) : ( + cmd == "y" ? _turtle2d_set_y(arg1, arg2) : ( + cmd == "angle" ? _turtle2d_set_angle(arg1, arg2) : ( + 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) = - cmd == "get_x" ? _turtle2d_get_x(arg) : ( - cmd == "get_y" ? _turtle2d_get_y(arg) : ( - cmd == "get_a" ? _turtle2d_get_angle(arg) : ( - cmd == "get_pt" ? _turtle2d_get_pt(arg) : undef + cmd == "x" ? _turtle2d_get_x(arg) : ( + cmd == "y" ? _turtle2d_get_y(arg) : ( + cmd == "angle" ? _turtle2d_get_angle(arg) : ( + cmd == "pt" ? _turtle2d_get_pt(arg) : undef ) ) );