| 1 | package Game::Terminal::Kit::Shape; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | sub line { | ||||||||||||||||||||||||||
|
Subroutine
line: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 10 | my ($x, $y, $to_x, $to_y) = map { int $_ } @_; | |||||||||||||||||||||||||||
| 11 | 10 | my ($across, $down) = (abs($to_x - $x), abs($to_y - $y)); | |||||||||||||||||||||||||||
| 12 | 10 | ▶ | my ($step_x, $step_y) = ($x < $to_x ? 1 : -1, $y < $to_y ? 1 : -1); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 13 | 10 | my $error = $across - $down; | |||||||||||||||||||||||||||
| 14 | 10 | my @points; | |||||||||||||||||||||||||||
| 15 | 10 | while (1) { | |||||||||||||||||||||||||||
| 16 | 65 | push @points, [ $x, $y ]; | |||||||||||||||||||||||||||
| 17 | 65 | ▶ | last if $x == $to_x && $y == $to_y; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x == $to_x and $y == $to_y
MC/DC100%2/2
$x == $to_x and $y == $to_y
$x == $to_x
$y == $to_y
Truth table100%3/3
A and B
A= $x == $to_xB= $y == $to_y
| |||||||||||||||||||||||||||||
| 18 | 55 | my $twice = 2 * $error; | |||||||||||||||||||||||||||
| 19 | 55 | ▶ | if ($twice > -$down) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 51 | $error -= $down; | |||||||||||||||||||||||||||
| 21 | 51 | $x += $step_x; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 55 | ▶ | if ($twice < $across) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 29 | $error += $across; | |||||||||||||||||||||||||||
| 25 | 29 | $y += $step_y; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 10 | return @points; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | sub rect { | ||||||||||||||||||||||||||
|
Subroutine
rect: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 3 | my ($x, $y, $width, $height) = map { int $_ } @_; | |||||||||||||||||||||||||||
| 33 | 3 | my @points; | |||||||||||||||||||||||||||
| 34 | 3 | for my $row ($y .. $y + $height - 1) { | |||||||||||||||||||||||||||
| 35 | 4 | push @points, map { [ $_, $row ] } $x .. $x + $width - 1; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 3 | return @points; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | ▶ | sub outline { | ||||||||||||||||||||||||||
|
Subroutine
outline: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 7 | my ($x, $y, $width, $height) = map { int $_ } @_; | |||||||||||||||||||||||||||
| 42 | 7 | ▶ | return () if $width < 1 || $height < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$width < 1 or $height < 1
MC/DC50%1/2
$width < 1 or $height < 1
$width < 1
$height < 1
Truth table66%2/3
A or B
A= $width < 1B= $height < 1
| |||||||||||||||||||||||||||||
| 43 | 6 | my ($right, $bottom) = ($x + $width - 1, $y + $height - 1); | |||||||||||||||||||||||||||
| 44 | 6 | my @points = map { [ $_, $y ] } $x .. $right; | |||||||||||||||||||||||||||
| 45 | 6 | for my $row ($y + 1 .. $bottom - 1) { | |||||||||||||||||||||||||||
| 46 | 5 | push @points, [ $x, $row ]; | |||||||||||||||||||||||||||
| 47 | 5 | ▶ | push @points, [ $right, $row ] if $right > $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 6 | ▶ | push @points, map { [ $_, $bottom ] } $x .. $right if $bottom > $y; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | return @points; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 15 | ▶ | sub disc { | ||||||||||||||||||||||||||
|
Subroutine
disc: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 15 | my ($x, $y, $radius) = map { int $_ } @_; | |||||||||||||||||||||||||||
| 55 | 15 | ▶ | return () if $radius < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 14 | my $reach = $radius * $radius + $radius; | |||||||||||||||||||||||||||
| 57 | 14 | my @points; | |||||||||||||||||||||||||||
| 58 | 14 | for my $down (-$radius .. $radius) { | |||||||||||||||||||||||||||
| 59 | 118 | my $half = int sqrt($reach - $down * $down); | |||||||||||||||||||||||||||
| 60 | 118 | push @points, map { [ $_, $y + $down ] } $x - $half .. $x + $half; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 14 | return @points; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | ▶ | sub ring { | ||||||||||||||||||||||||||
|
Subroutine
ring: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 12 | my ($x, $y, $radius) = map { int $_ } @_; | |||||||||||||||||||||||||||
| 67 | 12 | ▶ | return () if $radius < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 12 | my $reach = $radius * $radius + $radius; | |||||||||||||||||||||||||||
| 69 | 12 | my @points; | |||||||||||||||||||||||||||
| 70 | 12 | for my $down (-$radius .. $radius) { | |||||||||||||||||||||||||||
| 71 | 110 | my $half = int sqrt($reach - $down * $down); | |||||||||||||||||||||||||||
| 72 | 110 | ▶ | my $after = abs($down) == $radius ? -1 : int sqrt($reach - (abs($down) + 1) ** 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 110 | ▶ | my $inner = $after + 1 < $half ? $after + 1 : $half; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 110 | push @points, map { [ $x - $_, $y + $down ] } reverse $inner .. $half; | |||||||||||||||||||||||||||
| 75 | 110 | push @points, map { [ $x + $_, $y + $down ] } grep { $_ > 0 } $inner .. $half; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 12 | return @points; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | 1; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||