The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Turtle - Turtle Movement and State Operations

VERSION

version 0.0207

SYNOPSIS

  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);
  }

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.

For examples with GD and Imager, please see the files in the eg/ distribution directory.

METHODS

new

  $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.

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 = black
      pen_size  = 1

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.

  $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 x and y.

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, 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.

backward

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

Move backward the given number of steps, and return a list defining a line with these values:

  x0, y0, x, y, pen_color, pen_size

mirror

  $turtle->mirror;

Reflect the heading (by multiplying by -1).

goto

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

Move the pen to the given coordinate, and return a list defining a line with these values:

  x0, y0, x, y, pen_color, pen_size

SEE ALSO

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

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

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.