Data::Turtle - Turtle Movement and State Operations
version 0.0207
use Data::Turtle; my $turtle = Data::Turtle->new; $turtle->pen_up; $turtle->right(45); $turtle->forward(10); $turtle->goto(100, 100); $turtle->mirror; $turtle->backward(10); $turtle->pen_down; my ($x, $y, $heading, $status, $color, $size) = $turtle->get_state; $turtle->set_state($x, $y, $heading, $status, $color, $size); for my $i (1 .. 4) { my @line = $turtle->forward(50); if ($turtle->pen_down) { # Draw it! # ...insert your favorite graphics method... } $turtle->right(90); }
This module enables basic turtle movement and state operations without requiring any particular graphics package.
The methods don't draw anything. They just set or output coordinates and values for drawing by your favorite graphics package.
For examples with GD and Imager, please see the files in the eg/ distribution directory.
$turtle = Data::Turtle->new; $turtle = Data::Turtle->new( width => $width, height => $height, x => $x0, y => $y0, heading => $heading, pen_status => $pen_status, pen_color => $pen_color, pen_size => $pen_size, );
Return a Data::Turtle object.
Data::Turtle
Attributes:
width, height
Drawing surface dimensions. Defaults:
width = 500 height = 500
x, y, heading
Coordinate parameters. Defaults:
x = width / 2 y = height / 2 heading = 270 (degrees)
pen_status, pen_color, pen_size
Is the pen is up or down? Default: 1 (down position)
1
Pen properties. Defaults:
pen_color = black pen_size = 1
$turtle->home;
Move the turtle cursor to the starting x,y position and heading.
$turtle->pen_up;
Raise the pen head to stop drawing.
$turtle->pen_down;
Lower the pen head to begin drawing.
$turtle->right($degrees);
Turn to the right.
$turtle->left($degrees);
Turn to the left.
@pos = $turtle->position;
Return the current pen position as a list of x and y.
@state = $turtle->get_state;
Return the following settings as a list:
x, y, heading, pen_status, pen_color, pen_size
$turtle->set_state( $x, $y, $heading, $pen_status, $pen_color, $pen_size );
Set the turtle state with the given parameters.
@line = $turtle->forward($steps);
Move forward the given number of steps, and return a list defining a line with these values:
x0, y0, x, y, pen_color, pen_size
Where a step is given by
x = step * cos(heading * K) y = step * sin(heading * K)
and K is pi / 180.
K
pi / 180
@line = $turtle->backward($steps);
Move backward the given number of steps, and return a list defining a line with these values:
$turtle->mirror;
Reflect the heading (by multiplying by -1).
@line = $turtle->goto($x, $y);
Move the pen to the given coordinate, and return a list defining a line with these values:
Moo
Math::Trig
POSIX
GD::Simple has built-in turtle graphics
https://metacpan.org/source/YVESP/llg-1.07/Turtle.pm
https://en.wikipedia.org/wiki/Turtle_graphics
Gene Boggs <gene@cpan.org>
This software is copyright (c) 2015-2023 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Data::Turtle, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Turtle
CPAN shell
perl -MCPAN -e shell install Data::Turtle
For more information on module installation, please visit the detailed CPAN module installation guide.