NAME

Data::Turtle - Turtle Movement and State Operations

VERSION

version 0.0107

SYNOPSIS

use Data::Turtle;
my $turtle = Data::Turtle->new;
$turtle->pen_up;
$turtle->turn(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 there is a line and the pen is down, draw it!
    $turtle->right(90);
}

DESCRIPTION

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.

Please see the eg/ distribution directory for example code, with GD and Imager.

METHODS

new

Data::Turtle->new();
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.

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)

    Pen properties. Defaults: pen_color = the string 'black', pen_size = 1 (pixel)

home

$turtle->home;

Move the turtle cursor to the starting x,y position and heading.

pen_up

$turtle->pen_up;

Raise the pen head to stop drawing.

pen_down

$turtle->pen_down;

Lower the pen head to begin drawing.

turn

$turtle->right($degrees);

Set the heading to the given degrees.

$turtle->right($degrees);

Turn to the right.

left

$turtle->left($degrees);

Turn to the left.

position

@pos = $turtle->position;

Return the current pen position as a list of the x and y values.

get_state

@state = $turtle->get_state;

Return the following settings as a list:

x, y, heading, pen_status, pen_color, pen_size

set_state

$turtle->set_state( $x, $y, $heading, $pen_status, $pen_color, $pen_size );

Set the turtle state with the given parameters.

forward

@line = $turtle->forward($steps);

Move forward the given number of steps.

backward

@line = $turtle->backward($steps);

Move backward the given number of steps.

mirror

$turtle->mirror;

Reflect the heading by multiplying by -1.

goto

@line = $turtle->goto( $x, $y );

Move the pen to the given coordinate.

SEE ALSO

Moo

POSIX

https://metacpan.org/source/YVESP/llg-1.07/Turtle.pm

https://en.wikipedia.org/wiki/Turtle_graphics

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 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.