Function Repository Resource:

TurtleArt

Source Notebook

Make art with code

Contributed by: Brian Silverman and Daniel Sanchez

ResourceFunction["TurtleArt"]["field"]

returns the specified ResourceFunction["TurtleArt"] field.

ResourceFunction["TurtleArt"]["command"][]

executes the specified ResourceFunction["TurtleArt"] command.

ResourceFunction["TurtleArt"]["command"][params]

executes the command with the parameters params.

Details

A turtle follows a sequence of commands: draw lines and arcs, rotate, draw in different colors, go to a specific place on the screen, etc.
ResourceFunction["TurtleArt"] was inspired by the LOGO programming language and was designed to be easy enough for children and yet powerful enough for people of all ages.
The following fields are supported:
TurtleArt["Background"]the screen color
TurtleArt["Color"]the color of the turtle's pen
TurtleArt["Heading"]the heading of the turtle
TurtleArt["PenQ"]True if the pen is down; False otherwise
TurtleArt["Pensize"]the width of the turtle's pen
TurtleArt["Shade"]the shade of the turtle's pen
TurtleArt["X"]the x (horizontal) coordinate of the turtle
TurtleArt["Y"]the y (vertical) coordinate of the turtle
For appropriate fields, ResourceFunction["TurtleArt"]["field"]=val can be used to set the value of a field.
The following commands are supported:
TurtleArt[“Arc”][a,r]move the turtle in an arc with an angle a and a distance of r units
TurtleArt[“Back”][n]move the turtle backward n units
TurtleArt[“Clean”][]clear the screen and reposition the turtle to the center
TurtleArt["Draw"][]draw to the screen
TurtleArt[“FillScreen”][c,s]fill the screen with a color c and a shade s
TurtleArt["Forward"][n]move the turtle forward n units
TurtleArt["HideTurtle"][]hide the turtle icon
TurtleArt["Left"][n]turn the turtle left n units
ResourceFunction["TurtleArt"]["PenDown"][]lower the turtle's pen so that it will draw
ResourceFunction["TurtleArt"]["PenUp"][]raise the turtle's pen so that it will not draw
TurtleArt["Right"][n]turn the turtle right n units
TurtleArt["SetColor"][c]set the color of the turtle's pen to c
TurtleArt["SetHeading"][h]set the heading of the turtle to h
TurtleArt["SetPensize"][w]set the width of the turtle's pen to w
TurtleArt["SetShade"][s]set the shade of the turtle's pen to s
TurtleArt["SetXY"][x,y]set the position of the turtle to {x,y}
TurtleArt["ShowTurtle"][]show the turtle icon
ResourceFunction["TurtleArt"] works with a coordinate system. The turtle's default position is {0,0}, which is the middle of the screen:
The heading of the turtle is given in degrees, where 0 degrees means the turtle is directed upwards.
In ResourceFunction["TurtleArt"], the color and shade numbers are between 0 and 100. The default color is 0 and the default shade is 50:

Examples

Basic Examples (9) 

Obtain the turtle's initial position, heading and background:

In[1]:=
{{ResourceFunction["TurtleArt"]["X"], ResourceFunction["TurtleArt"]["Y"]}, ResourceFunction["TurtleArt"]["Heading"], ResourceFunction["TurtleArt"]["Background"]}
Out[1]=

Set the screen color:

In[2]:=
ResourceFunction["TurtleArt"]["FillScreen"][70, 98]

Show the turtle icon:

In[3]:=
ResourceFunction["TurtleArt"]["ShowTurtle"][]

Move forward the turtle a distance of 100 units:

In[4]:=
ResourceFunction["TurtleArt"]["Forward"][100]

Rotate the turtle 35° to the right:

In[5]:=
ResourceFunction["TurtleArt"]["Right"][35]

Move forward again 100 units:

In[6]:=
ResourceFunction["TurtleArt"]["Forward"][100]

Draw the turtle's art:

In[7]:=
ResourceFunction["TurtleArt"]["Draw"][]
Out[7]=

Obtain the new values of position, heading and background:

In[8]:=
{{ResourceFunction["TurtleArt"]["X"], ResourceFunction["TurtleArt"]["Y"]}, ResourceFunction["TurtleArt"]["Heading"], ResourceFunction["TurtleArt"]["Background"]}
Out[8]=

Clean and hide the turtle:

In[9]:=
ResourceFunction["TurtleArt"]["Clean"][];
ResourceFunction["TurtleArt"]["HideTurtle"][];
{{ResourceFunction["TurtleArt"]["X"], ResourceFunction["TurtleArt"]["Y"]}, ResourceFunction["TurtleArt"]["Heading"], ResourceFunction["TurtleArt"]["Background"]}
Out[11]=

Possible Issues (3) 

Invalid fields return unevaluated code:

In[12]:=
ResourceFunction["TurtleArt"]["Z"]
Out[12]=

Invalid commands return a Failure object:

In[13]:=
ResourceFunction["TurtleArt"]["ArcRight"][40, 30]
Out[13]=

Invalid command parameters return a Failure object:

In[14]:=
ResourceFunction["TurtleArt"]["SetXY"][
RGBColor[0, 0, 0]]
Out[14]=

Neat Examples (15) 

Dandelion (1) 

Draw dandelion:

In[15]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/d6518c34-cb96-43eb-8e38-a3328855fb52"]
Out[15]=

Neonsqi (1) 

Draw neonsqi:

In[16]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/843f969b-288a-4ea8-9e5b-228d6082037d"]
Out[16]=

Beginning (1) 

Draw beginning:

In[17]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/fce87d51-72cb-4186-b0eb-10ac17ecfd76"]
Out[17]=

Curls (1) 

Draw curls:

In[18]:=
Module[{curl, box1, box2},
 curl[] := Module[{box3 = 0},
   ResourceFunction["TurtleArt"]["SetHeading"][-45];
   Do[
    box3 += 2;
    ResourceFunction["TurtleArt"]["Arc"][90, box3]
    ,
    20
    ]
   ]; ResourceFunction["TurtleArt"]["Clean"][];
 ResourceFunction["TurtleArt"]["FillScreen"][0, 0];
 box1 = -280;
 ResourceFunction["TurtleArt"]["SetXY"][-280, -236]; Do[
  box2 = -236;
  Do[
   ResourceFunction["TurtleArt"]["SetXY"][box1, box2];
   ResourceFunction["TurtleArt"]["SetColor"][
    RandomInteger[{0, 100}]];
   curl[];
   box2 += 79
   ,
   7
   ];
  box1 += 80
  ,
  8
  ]; ResourceFunction["TurtleArt"]["Draw"][]
 ]
Out[18]=

Waves (1) 

Draw waves:

In[19]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/e9c4f3c8-5d5c-4243-84c2-954747993811"]
Out[19]=

Old Sol (1) 

Draw old sol:

In[20]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/3babacc8-28f9-4cf0-8385-a854734d5004"]
Out[20]=

Spaceframe (1) 

Draw spaceframe:

In[21]:=
(
 ResourceFunction["TurtleArt"]["Clean"][];
 ResourceFunction["TurtleArt"]["FillScreen"][80, 20];
 ResourceFunction["TurtleArt"]["SetPensize"][2];
 ResourceFunction["TurtleArt"]["SetColor"][15];
 Do[
  ResourceFunction["TurtleArt"]["SetXY"][RandomInteger[{-350, 350}], RandomInteger[{-280, 280}]];
  ResourceFunction["TurtleArt"]["Forward"][100];
  ResourceFunction["TurtleArt"]["Right"][120]
  ,
  400
  ]; ResourceFunction["TurtleArt"]["Draw"][]
 )
Out[21]=

Brushes (1) 

Draw brushes:

In[22]:=
Module[{zig},
 zig[] :=
  Do[
   ResourceFunction["TurtleArt"]["Forward"][RandomInteger[{100, 130}]];
   ResourceFunction["TurtleArt"]["Right"][RandomInteger[{179, 181}]];
   ResourceFunction["TurtleArt"]["Forward"][RandomInteger[{100, 130}]];
   ResourceFunction["TurtleArt"]["Left"][RandomInteger[{179, 181}]]
   ,
   100
   ]; ResourceFunction["TurtleArt"]["Clean"][];
 ResourceFunction["TurtleArt"]["SetPensize"][1];
 ResourceFunction["TurtleArt"]["SetColor"][60];
 Do[
  ResourceFunction["TurtleArt"]["SetShade"][RandomInteger[{0, 100}]];
  ResourceFunction["TurtleArt"]["SetXY"][RandomInteger[{-300, 300}], RandomInteger[{-300, 300}]];
  zig[]
  ,
  1000
  ]; ResourceFunction["TurtleArt"]["Draw"][]
 ]
Out[22]=

Fireworks (1) 

Draw fireworks:

In[23]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/d5d26aad-6718-4037-92dd-f0d72808c477"]
Out[23]=

Dark Sun (1) 

Draw dark sun:

In[24]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/74242091-c9ad-4863-802d-17fff6b36b7d"]
Out[24]=

Whole Flower (1) 

Draw whole flower:

In[25]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/5bea58c2-b086-40a6-8025-e482fb447f7d"]
Out[25]=

Pinwheel (1) 

Draw pinwheel:

In[26]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/1dc8e67d-ba60-421a-8a23-a6f45ae254cc"]
Out[26]=

Springy (1) 

Draw springy:

In[27]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/b3d057f4-8524-4895-ac80-7eb60fac7952"]
Out[27]=

Yellow Jacket (1) 

Draw yellow jacket:

In[28]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/206a02e6-5d56-4550-9f2c-5016c6e15617"]
Out[28]=

Three Spirals (1) 

Draw three spirals:

In[29]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/62ed5dd1-2adc-4b65-937d-f8bd49023d7b"]
Out[29]=

Interactive Examples (2) 

UFO (2) 

Setup a dynamic screen:

In[30]:=
ResourceFunction["TurtleArt"]["Clean"][];
Dynamic[ResourceFunction["TurtleArt"]["Draw"][]]
Out[31]=

Execute the following code and visualize the artwork:

In[32]:=
Module[{feather},
 feather[] := Module[{box1, box2},
   box1 = 100;
   box2 = 1;
   Do[
    ResourceFunction["TurtleArt"]["Arc"][100, box1*box2];
    box1 -= RandomInteger[{0, 5}];
    ResourceFunction["TurtleArt"]["SetShade"][
     ResourceFunction["TurtleArt"]@"Shade" + .3];
    box2 *= -1
    ,
    100
    ]
   ]; ResourceFunction["TurtleArt"]["Clean"][];
 ResourceFunction["TurtleArt"]["FillScreen"][0, 0];
 ResourceFunction["TurtleArt"]["SetColor"][12];
 ResourceFunction["TurtleArt"]["SetPensize"][1];
 Do[
  ResourceFunction["TurtleArt"]["SetXY"][RandomInteger[{-259, 259}], RandomInteger[{-259, 259}]];
  feather[]
  ,
  40
  ]
 ]

Publisher

Daniel Sanchez

Version History

  • 1.0.0 – 25 January 2022

Related Resources

License Information