| 1 | package Game::Battleship::Grid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | $Game::Battleship::Grid::VERSION = '0.0602'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Game::Battleship::Craft; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Types::Standard qw( ArrayRef Int ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | has dimension => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | isa => ArrayRef[Int], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | default => sub { [ 9, 9 ] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | has fleet => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # Place the array reference of craft on the grid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=15 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Initialize the matrix. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | for my $i (0 .. $self->dimension->[0]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 50 | for my $j (0 .. $self->dimension->[1]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 500 | $self->{matrix}[$i][$j] = '.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Place the fleet on the grid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | for my $craft (@{ $self->{fleet} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 15 | my ($ok, $x0, $y0, $x1, $y1, $orient); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | if (defined $craft->position) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ($x0, $y0) = ($craft->position->[0], $craft->position->[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Set the craft orientation and tail coordinates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ($orient, $x1, $y1) = _tail_coordinates( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | $x0, $y0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | $craft->points - 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | $craft->orient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # XXX This looping is needlessly brutish. refactoring please | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 15 | ▶ | while (not $ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # Grab a random coordinate that we haven't seen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 22 | $x0 = int(rand($self->dimension->[0] + 1)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 22 | $y0 = int(rand($self->dimension->[1] + 1)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Set the craft orientation and tail coordinates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 22 | ($orient, $x1, $y1) = _tail_coordinates( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | $x0, $y0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | $craft->points - 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | $craft->orient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # If the craft is not placed off the grid and it does | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # not collide with another craft, then we are ok to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # move on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # XXX regex constraint rules here? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 22 | ▶ | if ($x1 <= $self->dimension->[0] && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x1 <= $self->dimension->[0] and $y1 <= $self->dimension->[1]
MC/DC50%1/2
$x1 <= $self->dimension->[0] and $y1 <= $self->dimension->[1]
$x1 <= $self->dimension->[0]
$y1 <= $self->dimension->[1]
Truth table66%2/3
A and B
A= $x1 <= $self->dimension->[0]B= $y1 <= $self->dimension->[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | $y1 <= $self->dimension->[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # For each craft (except the current one) that has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # a position, do the craft share a common point? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 20 | my $collide = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 20 | for (@{ $self->{fleet} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Ships can't be the same. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 85 | ▶ | if ($craft->name ne $_->name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # Ships can't intersect. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 70 | ▶ | if (defined $_->position && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_->position and _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
MC/DC100%2/2
defined $_->position and _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
defined $_->position
_segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
Truth table100%3/3
A and B
A= defined $_->positionB= _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | _segment_intersection( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | $x0, $y0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | $x1, $y1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 40 | @{ $_->position->[0] }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 40 | @{ $_->position->[1] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | $collide = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 20 | ▶ | $ok = 1 unless $collide; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Set the craft position. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | $craft->{position} = [[$x0, $y0], [$x1, $y1]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | #warn "$craft->{name}: [$x0, $y0], [$x1, $y1], $craft->{points}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # Add the craft to the grid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 15 | for my $n (0 .. $craft->points - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 51 | ▶ | if ($orient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 28 | $self->{matrix}[$x0 + $n][$y0] = $craft->{id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 23 | $self->{matrix}[$x0][$y0 + $n] = $craft->{id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 22 | ▶ | sub _tail_coordinates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tail_coordinates: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # Get the coordinates of the end of the segment based on a given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # span. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 22 | my ($x0, $y0, $span, $orient) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Set orientation to 0 (vertical) or 1 (horizontal). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 22 | ▶ | $orient = int rand 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | unless defined $orient; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 22 | my ($x1, $y1) = ($x0, $y0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 22 | ▶ | if ($orient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 12 | $x1 += $span; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 10 | $y1 += $span; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 22 | return $orient, $x1, $y1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 40 | ▶ | sub _segment_intersection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_segment_intersection: called CC=26 SCAR=41.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # 0 - Intersection doesn't exist. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # 1 - Intersection exists. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # NOTE: In Battleship, we only care about yes/no, but the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # original code can tell much more: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # 0 (was 2) - line segments are parallel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # 0 (was 3) - line segments are collinear but do not overlap. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # 4 - line segments are collinear and share an end point. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # 5 - line segments are collinear and overlap. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 40 | ▶ | croak "segment_intersection needs 4 points\n" unless @_ == 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | my( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 40 | $x0, $y0, $x1, $y1, # AB segment 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | $x2, $y2, $x3, $y3 # CD segment 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | #warn "[$x0, $y0]-[$x1, $y1], [$x2, $y2]-[$x3, $y3]\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 40 | my $xba = $x1 - $x0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 40 | my $yba = $y1 - $y0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 40 | my $xdc = $x3 - $x2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 40 | my $ydc = $y3 - $y2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 40 | my $xca = $x2 - $x0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 40 | my $yca = $y2 - $y0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 40 | my $delta = $xba * $ydc - $yba * $xdc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 40 | my $t1 = $xca * $ydc - $yca * $xdc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 40 | my $t2 = $xca * $yba - $yca * $xba; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 40 | ▶ | if ($delta != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 17 | my $u = $t1 / $delta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 17 | my $v = $t2 / $delta; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Two segments intersect (including at end points). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 17 | ▶ | return ($u <= 1 && $u >= 0 && $v <= 1 && $v >= 0) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$u <= 1 and $u >= 0 and $v <= 1 and $v >= 0
$u <= 1 and $u >= 0 and $v <= 1
$u <= 1 and $u >= 0
MC/DC100%4/4
$u <= 1 and $u >= 0 and $v <= 1 and $v >= 0
$u <= 1
$u >= 0
$v <= 1
$v >= 0
Truth table100%5/5
A and B and C and D
A= $u <= 1B= $u >= 0C= $v <= 1D= $v >= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # AB & CD are parallel. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 23 | ▶ | return 0 if $t1 != 0 && $t2 != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t1 != 0 and $t2 != 0
MC/DC50%1/2
$t1 != 0 and $t2 != 0
$t1 != 0
$t2 != 0
Truth table66%2/3
A and B
A= $t1 != 0B= $t2 != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # NOTE: We just care about yes/no, so this is the old way: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # return 2 if $t1 != 0 && $t2 != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # When AB & CD are collinear... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | my ($a, $b, $c, $d); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # If AB isn't a vertical line segment, project to x-axis. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 5 | ▶ | if ($x0 != $x1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # < is min, > is max | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | ▶ | $a = $x0 < $x1 ? $x0 : $x1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | $b = $x0 > $x1 ? $x0 : $x1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | ▶ | $c = $x2 < $x3 ? $x2 : $x3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 5 | ▶ | $d = $x2 > $x3 ? $x2 : $x3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | ▶ | if ($d < $a || $c > $b) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$d < $a or $c > $b
MC/DC50%1/2
$d < $a or $c > $b
$d < $a
$c > $b
Truth table66%2/3
A or B
A= $d < $aB= $c > $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # NOTE: We just care about yes/no. The old way returns 3: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | return 0;#3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | ▶ | elsif ($d == $a || $c == $b) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$d == $a or $c == $b
MC/DC50%1/2
$d == $a or $c == $b
$d == $a
$c == $b
Truth table66%2/3
A or B
A= $d == $aB= $c == $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | return 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | return 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # If AB is a vertical line segment, project to y-axis. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # < is min, > is max | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $a = $y0 < $y1 ? $y0 : $y1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | $b = $y0 > $y1 ? $y0 : $y1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $c = $y2 < $y3 ? $y2 : $y3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | $d = $y2 > $y3 ? $y2 : $y3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($d < $a || $c > $b) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$d < $a or $c > $b
MC/DC0%0/2
$d < $a or $c > $b
$d < $a
$c > $b
Truth table0%0/3
A or B
A= $d < $aB= $c > $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # NOTE: We just care about yes/no. The old way returns 3: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | return 0;#3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ▶ | elsif ($d == $a || $c == $b) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$d == $a or $c == $b
MC/DC0%0/2
$d == $a or $c == $b
$d == $a
$c == $b
Truth table0%0/3
A or B
A= $d == $aB= $c == $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | return 4; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | return 5; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||