| 1 | package Data::Turtle; | ||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||
| 3 | |||||||||||||
| 4 | # ABSTRACT: Turtle Movement and State Operations | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Math::Trig qw(:pi); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use POSIX qw(ceil); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | our $VERSION = '0.0207'; | |||||||||||
| 11 | |||||||||||||
| 12 | 2 | ▶ | use constant K => pi / 180; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | |||||||||||||
| 15 | 2 | ▶ | sub _init_x { | ||||||||||
|
Subroutine
_build_x: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 2 | my $self = shift; | |||||||||||
| 17 | 2 | return $self->width / 2; | |||||||||||
| 18 | } | ||||||||||||
| 19 | |||||||||||||
| 20 | 2 | ▶ | sub _init_y { | ||||||||||
|
Subroutine
_build_y: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 2 | my $self = shift; | |||||||||||
| 22 | 2 | return $self->height / 2; | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | 2 | ▶ | sub _init_heading { | ||||||||||
|
Subroutine
_build_heading: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 2 | my $self = shift; | |||||||||||
| 27 | 2 | return -90 % 360; | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | |||||||||||||
| 31 | has width => ( | ||||||||||||
| 32 | is => 'rw', | ||||||||||||
| 33 | 1 | ▶ | default => sub { 500 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 2 | ); | |||||||||||
| 35 | |||||||||||||
| 36 | has height => ( | ||||||||||||
| 37 | is => 'rw', | ||||||||||||
| 38 | 1 | ▶ | default => sub { 500 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 2 | ); | |||||||||||
| 40 | |||||||||||||
| 41 | |||||||||||||
| 42 | 2 | has x => ( | |||||||||||
| 43 | is => 'rw', | ||||||||||||
| 44 | lazy => 1, | ||||||||||||
| 45 | builder => \&_init_x, | ||||||||||||
| 46 | ); | ||||||||||||
| 47 | |||||||||||||
| 48 | 2 | has y => ( | |||||||||||
| 49 | is => 'rw', | ||||||||||||
| 50 | lazy => 1, | ||||||||||||
| 51 | builder => \&_init_y, | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | |||||||||||||
| 54 | 2 | has heading => ( | |||||||||||
| 55 | is => 'rw', | ||||||||||||
| 56 | builder => \&_init_heading, | ||||||||||||
| 57 | ); | ||||||||||||
| 58 | |||||||||||||
| 59 | |||||||||||||
| 60 | has pen_status => ( | ||||||||||||
| 61 | is => 'rw', | ||||||||||||
| 62 | 1 | ▶ | default => sub { 1 }, # Pen down | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 2 | ); | |||||||||||
| 64 | |||||||||||||
| 65 | has pen_color => ( | ||||||||||||
| 66 | is => 'rw', | ||||||||||||
| 67 | 1 | ▶ | default => sub { 'black' }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | 2 | ); | |||||||||||
| 69 | |||||||||||||
| 70 | has pen_size => ( | ||||||||||||
| 71 | is => 'rw', | ||||||||||||
| 72 | 1 | ▶ | default => sub { 1 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | 2 | ); | |||||||||||
| 74 | |||||||||||||
| 75 | |||||||||||||
| 76 | 1 | ▶ | sub home { | ||||||||||
|
Subroutine
home: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 77 | 1 | my $self = shift; | |||||||||||
| 78 | 1 | $self->x( $self->_init_x ); | |||||||||||
| 79 | 1 | $self->y( $self->_init_y ); | |||||||||||
| 80 | 1 | $self->heading( $self->_init_heading ); | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | |||||||||||||
| 84 | 1 | ▶ | sub pen_up { | ||||||||||
|
Subroutine
pen_up: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 1 | my $self = shift; | |||||||||||
| 86 | 1 | $self->pen_status(0); | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | |||||||||||||
| 90 | 1 | ▶ | sub pen_down { | ||||||||||
|
Subroutine
pen_down: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 91 | 1 | my $self = shift; | |||||||||||
| 92 | 1 | $self->pen_status(1); | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | |||||||||||||
| 96 | 2 | ▶ | sub right { | ||||||||||
|
Subroutine
right: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 97 | 2 | my $self = shift; | |||||||||||
| 98 | 2 | ▶ | my $degrees = shift // 0; | ||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 99 | 2 | $self->heading(($self->heading + $degrees) % 360); | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | |||||||||||||
| 103 | 1 | ▶ | sub left { | ||||||||||
|
Subroutine
left: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 104 | 1 | my $self = shift; | |||||||||||
| 105 | 1 | ▶ | my $degrees = shift // 0; | ||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 106 | 1 | $self->heading(($self->heading - $degrees) % 360); | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | |||||||||||||
| 110 | 3 | ▶ | sub position { | ||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 111 | 3 | my $self = shift; | |||||||||||
| 112 | 3 | return ceil($self->x), ceil($self->y); | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | |||||||||||||
| 116 | 3 | ▶ | sub get_state { | ||||||||||
|
Subroutine
get_state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 117 | 3 | my $self = shift; | |||||||||||
| 118 | return | ||||||||||||
| 119 | 3 | ceil($self->x), | |||||||||||
| 120 | ceil($self->y), | ||||||||||||
| 121 | $self->heading, | ||||||||||||
| 122 | $self->pen_status, | ||||||||||||
| 123 | $self->pen_color, | ||||||||||||
| 124 | $self->pen_size; | ||||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | |||||||||||||
| 128 | 1 | ▶ | sub set_state { | ||||||||||
|
Subroutine
set_state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 129 | 1 | my $self = shift; | |||||||||||
| 130 | 1 | my ($x, $y, $heading, $pen_status, $pen_color, $pen_size) = @_; | |||||||||||
| 131 | 1 | $self->x($x); | |||||||||||
| 132 | 1 | $self->y($y); | |||||||||||
| 133 | 1 | $self->heading($heading); | |||||||||||
| 134 | 1 | $self->pen_status($pen_status); | |||||||||||
| 135 | 1 | $self->pen_color($pen_color); | |||||||||||
| 136 | 1 | $self->pen_size($pen_size); | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | |||||||||||||
| 140 | 2 | ▶ | sub forward { | ||||||||||
|
Subroutine
forward: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 141 | 2 | my $self = shift; | |||||||||||
| 142 | 2 | ▶ | my $step = shift // 1; | ||||||||||
|
Condition50%1/2
shift() // 1
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 143 | |||||||||||||
| 144 | 2 | my $x = $step * cos($self->heading * K); | |||||||||||
| 145 | 2 | my $y = $step * sin($self->heading * K); | |||||||||||
| 146 | |||||||||||||
| 147 | 2 | my $xo = $self->x; | |||||||||||
| 148 | 2 | my $yo = $self->y; | |||||||||||
| 149 | |||||||||||||
| 150 | 2 | $self->x($x + $xo); | |||||||||||
| 151 | 2 | $self->y($y + $yo); | |||||||||||
| 152 | |||||||||||||
| 153 | 2 | return ceil($xo), ceil($yo), | |||||||||||
| 154 | ceil($self->x), ceil($self->y), | ||||||||||||
| 155 | $self->pen_color, $self->pen_size; | ||||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | |||||||||||||
| 159 | 1 | ▶ | sub backward { | ||||||||||
|
Subroutine
backward: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 160 | 1 | my $self = shift; | |||||||||||
| 161 | 1 | my $step = shift; | |||||||||||
| 162 | 1 | $self->forward(-$step) | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | |||||||||||||
| 166 | 1 | ▶ | sub mirror { | ||||||||||
|
Subroutine
mirror: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 167 | 1 | my $self = shift; | |||||||||||
| 168 | 1 | $self->heading($self->heading * -1); | |||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | 1 | ▶ | sub goto { | ||||||||||
|
Subroutine
goto: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 173 | 1 | my $self = shift; | |||||||||||
| 174 | 1 | my ($x, $y) = @_; | |||||||||||
| 175 | |||||||||||||
| 176 | 1 | my $xo = $self->x; | |||||||||||
| 177 | 1 | my $yo = $self->y; | |||||||||||
| 178 | |||||||||||||
| 179 | 1 | $self->x($x); | |||||||||||
| 180 | 1 | $self->y($y); | |||||||||||
| 181 | |||||||||||||
| 182 | 1 | return ceil($xo), ceil($yo), | |||||||||||
| 183 | ceil($self->x), ceil($self->y), | ||||||||||||
| 184 | $self->pen_color, $self->pen_size; | ||||||||||||
| 185 | } | ||||||||||||
| 186 | |||||||||||||
| 187 | 2 | 1; | |||||||||||
| 188 | |||||||||||||
| 189 | __END__ | ||||||||||||