restart;Helper Functions for Turtle GraphicsThe helper functions forward, turnleft and turnright implement a set of functions to realize turtle graphics. They create a list of tuples (each tuple being a 2D coordinate, i.e. a list of two values) in the global variable points. The created list can be visualized using Maple's plot command. The last coordinate in the list is treated as the current position of the turtle. The current walking direction of the turtle is stored in the global variable TURTLEDIR, which can be changed by the functions turnleft and turnright.The function forward implements one turtle step in the current direction:forward := proc()
global points, TURTLEDIR;
local elem;
elem := points[-1];
points := [ op(points), [ elem[1]+TURTLEDIR[1], elem[2]+TURTLEDIR[2] ] ];
end proc:The function turnleft implements a 90-degree left rotation on the current turtle direction:turnleft := proc()
global TURTLEDIR;
TURTLEDIR := [ -TURTLEDIR[2], TURTLEDIR[1] ];
end proc:The function turnright implements a 90-degree right rotation on the current turtle direction:turnright := proc()
global TURTLEDIR;
TURTLEDIR := [ TURTLEDIR[2], -TURTLEDIR[1] ];
end proc:The function turndegree implements a rotation on the current turtle direction (angle given as parameter):turndegree := proc(deg::numeric)
global TURTLEDIR;
local c,s;
c := evalf( cos(deg*Pi/180)) ; s := evalf( sin(deg*Pi/180) );
TURTLEDIR := [ TURTLEDIR[1]*c - TURTLEDIR[2]*s, TURTLEDIR[1]*s + TURTLEDIR[2]*c ];
end proc:Helper Functions to Draw the Gosper Islands as Coloured Partitions of the Curvecolours := [red,green, black, yellow, cyan, magenta, blue, navy,
pink, grey, brown, khaki, coral];The function partition cuts a given list pts of points into number separated list of (nearly) the same size.partition := proc(pts::list, number::posint)
local parts,i;
parts := [ pts[ (number-1)*floor(nops(pts)/number)..-1 ] ];
for i from number-1 by -1 to 2 do
parts := [ pts[ (i-1)*floor(nops(pts)/number)..i*floor(nops(pts)/number) ],
op(parts) ];
end do;
parts := [ pts[ 1..floor(nops(pts)/number) ], op(parts) ];
return parts;
end proc:Turtle-Grammar Algorithm to Draw the Iterations of the Gosper CurveCompare Figure 7.10 and Equation (7.3) in Section 7.5 - gosper implements pattern G, rgosper implements pattern R:gosper := proc(depth::nonnegint)
if depth = 1
then forward(); turndegree(120);
forward(); turndegree(60);
forward(); turndegree(-120);
forward(); turndegree(-60);
forward(); turndegree(-60);
forward()
else
gosper(depth-1); turndegree(60); forward();
rgosper(depth-1); turndegree(60); forward();
rgosper(depth-1); forward(); turndegree(-60);
gosper(depth-1); forward(); turndegree(-60);
gosper(depth-1); turndegree(60); forward();
gosper(depth-1); forward(); turndegree(-60);
rgosper(depth-1);
end if;
end proc:rgosper := proc(depth::nonnegint)
if depth = 1
then forward(); turndegree(60);
forward(); turndegree(60);
forward(); turndegree(120);
forward(); turndegree(-60);
forward(); turndegree(-120);
forward()
else
gosper(depth-1); turndegree(60); forward();
rgosper(depth-1); forward(); turndegree(-60);
rgosper(depth-1); turndegree(60); forward();
rgosper(depth-1); turndegree(60); forward();
gosper(depth-1); forward(); turndegree(-60);
gosper(depth-1); forward(); turndegree(-60);
rgosper(depth-1);
end if;
end proc:Plotting the Iterations of the Gosper Curvepoints := [[0,0]];
TURTLEDIR := [1,0];
gosper(4):
plot( points, thickness=2, scaling=CONSTRAINED, axes=none);Plotting the Iterations and the Gosper Islands as Space-Fillersparts := partition(points,7):plot(parts,axes=NONE, scaling=CONSTRAINED, color=colours);JSFHAlgorithm to Draw the Approximating PolygonsCompare Exercise 7.3 and the respective solutiongospoly := proc(depth::nonnegint)
if depth = 1
then forward(); turndegree(60);
forward(); turndegree(120);
forward(); turndegree(-60);
forward(); turndegree(-120);
forward();
forward(); turndegree(-60);
forward()
else
gospoly(depth-1); turndegree(60);
rgospoly(depth-1); turndegree(60);
rgospoly(depth-1); turndegree(-60);
gospoly(depth-1); turndegree(-60);
gospoly(depth-1); turndegree(60);
gospoly(depth-1); turndegree(-60);
rgospoly(depth-1);
end if;
end proc:rgospoly := proc(depth::nonnegint)
if depth = 1
then forward(); turndegree(60);
forward();
forward(); turndegree(120);
forward(); turndegree(60);
forward(); turndegree(-120);
forward(); turndegree(-60);
forward()
else
gospoly(depth-1); turndegree(60);
rgospoly(depth-1); turndegree(-60);
rgospoly(depth-1); turndegree(60);
rgospoly(depth-1); turndegree(60);
gospoly(depth-1); turndegree(-60);
gospoly(depth-1); turndegree(-60);
rgospoly(depth-1);
end if;
end proc:Plotting the Polygonspoints := [[0,0]];
TURTLEDIR := [1,0];
gospoly(4):
plot( points, thickness=2, scaling=CONSTRAINED, axes=none);