| 1 | package Term::Fabulous::Render::Geometry; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.32; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | our @EXPORT_OK = qw(cell_rect intersect_cell_rects visible_cell_rect rects_overlap row_spans_outside cell_coordinate line_steps); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use POSIX qw(floor); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub cell_rect ($bbox) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cell_rect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my ( $x, $y ) = @{$bbox}{qw(x y)}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | return ( floor($x), floor($y), floor( $x + $bbox->{width} ), floor( $y + $bbox->{height} ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub intersect_cell_rects ( $first, $second ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
intersect_cell_rects: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $x0 = max( $first->[0], $second->[0] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my $y0 = max( $first->[1], $second->[1] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $x1 = max( $x0, min( $first->[2], $second->[2] ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my $y1 = max( $y0, min( $first->[3], $second->[3] ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | return [ $x0, $y0, $x1, $y1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub visible_cell_rect ( $bbox, $clip ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visible_cell_rect: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( [ cell_rect($bbox) ], $clip ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return if $x0 >= $x1 || $y0 >= $y1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x0 >= $x1 or $y0 >= $y1
MC/DC0%0/2
$x0 >= $x1 or $y0 >= $y1
$x0 >= $x1
$y0 >= $y1
Truth table0%0/3
A or B
A= $x0 >= $x1B= $y0 >= $y1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | return ( $x0, $y0, $x1, $y1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # Rounds down; int() and a comparison are much cheaper than POSIX::floor. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub cell_coordinate ( $what, $value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cell_coordinate: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | die "Term::Fabulous::Render::Geometry: $what must be a finite number, got " . ( defined $value ? "'$value'" : 'undef' ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
&looks_like_number($value) and $value == $value and $value - $value == 0
&looks_like_number($value) and $value == $value
MC/DC0%0/3
&looks_like_number($value) and $value == $value and $value - $value == 0
&looks_like_number($value)
$value == $value
$value - $value == 0
Truth table0%0/4
A and B and C
A= &looks_like_number($value)B= $value == $valueC= $value - $value == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | unless looks_like_number($value) && $value == $value && $value - $value == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $whole = int $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return $whole <= $value ? $whole : $whole - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Bresenham: the line passes every pixel from one end to the other, both | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # included. Along the longer (major) axis every step moves one pixel, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # after $i steps the shorter axis has moved round($i * minor / major) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # pixels, halves rounded up. The steps whose major coordinate lies in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # raster are computed directly, so a line far longer than the raster | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # costs no more than one across it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 272 | ▶ | sub line_steps ( $x0, $y0, $x1, $y1, $width, $height ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
line_steps: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 272 | my ( $dx, $dy ) = ( abs( $x1 - $x0 ), abs( $y1 - $y0 ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 272 | ▶ | my ( $step_x, $step_y ) = ( $x0 < $x1 ? 1 : -1, $y0 < $y1 ? 1 : -1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 272 | ▶ | if ( $dx >= $dy ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 179 | my ( $first, $last ) = _steps_inside( $x0, $step_x, $dx, $width ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 179 | return ( $first, map { [ $x0 + $step_x * $_, $y0 + $step_y * _minor_steps( $_, $dy, $dx ) ] } $first .. $last ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 93 | my ( $first, $last ) = _steps_inside( $y0, $step_y, $dy, $height ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 93 | return ( $first, map { [ $x0 + $step_x * _minor_steps( $_, $dx, $dy ), $y0 + $step_y * $_ ] } $first .. $last ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # The first and last step $i of 0 .. $length at which $start + $step * $i | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # lies in 0 .. $limit - 1 (first above last when there is none). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 272 | ▶ | sub _steps_inside ( $start, $step, $length, $limit ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_steps_inside: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 272 | ▶ | my ( $first, $last ) = $step > 0 ? ( -$start, $limit - 1 - $start ) : ( $start - $limit + 1, $start ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 272 | return ( max( $first, 0 ), min( $last, $length ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1207 | ▶ | sub _minor_steps ( $i, $minor, $major ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_minor_steps: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1207 | ▶ | return 0 unless $major; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1206 | return int( ( 2 * $minor * $i + $major ) / ( 2 * $major ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub rects_overlap ( $first, $second ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rects_overlap: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( $first, $second ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $x0 < $x1 && $y0 < $y1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$x0 < $x1 && $y0 < $y1
MC/DC0%0/2
$x0 < $x1 && $y0 < $y1
$x0 < $x1
$y0 < $y1
Truth table0%0/3
A && B
A= $x0 < $x1B= $y0 < $y1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub row_spans_outside ( $y, $x0, $x1, @rects ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
row_spans_outside: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my @holes = sort { $a->[0] <=> $b->[0] } map { [ max( $x0, $_->[0] ), min( $x1, $_->[2] ) ] } grep { $y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0 } @rects; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0
$y >= $_->[1] and $y < $_->[3] and $_->[0] < $x1
$y >= $_->[1] and $y < $_->[3]
MC/DC0%0/4
$y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0
$y >= $_->[1]
$y < $_->[3]
$_->[0] < $x1
$_->[2] > $x0
Truth table0%0/5
A and B and C && D
A= $y >= $_->[1]B= $y < $_->[3]C= $_->[0] < $x1D= $_->[2] > $x0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my ( @spans, $from ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $from = $x0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | foreach my $hole (@holes) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | push @spans, [ $from, $hole->[0] ] if $hole->[0] > $from; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $from = max( $from, $hole->[1] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | push @spans, [ $from, $x1 ] if $x1 > $from; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return @spans; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||