mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-17 22:28:16 +01:00
3.3 KiB
3.3 KiB
turtle2d
An OpenSCAD implementation of Turtle Graphics. It moves on the xy plane. You can get the cooridinate [x, y]
or angle
of its current position.
Parameters
cmd
: A string command. Different commands use different numbers of arguments."create"
: Creates a turtle data. The command needs three argumentsx
,y
andangle
. For example, useturtle2d("create", 5, 10, 30)
to create a turtle located at[0, 0]
with an angle30
degrees."set_x"
: Sets thex
coordinate of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is thex
coordinate. For example,turtle2d("set_x", turtle, 20)
."set_y"
: Sets they
coordinate of a turtle. The command needs two arguments. The first one is a turtle data, and the second one is they
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])
."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 <line2d.scad>;
include <turtle2d.scad>;
module turtle_spiral(t_before, times, side_leng, angle, width) {
$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),
width,
p1Style = "CAP_ROUND",
p2Style = "CAP_ROUND"
);
turtle_spiral(t_after_fd, times - 1, side_leng, angle, width);
}
}
side_leng = 10;
angle = 144;
width = 1;
times = 5;
turtle_spiral(turtle2d("create", 0, 0, 0), times, side_leng, angle, width);
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("get_pt", t_before),
turtle2d("get_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
);