blib/lib/Term/Fabulous/Chart/Radial.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 55 CC 3 SCAR 11.7
CC
3
Cov
0%
circle_frame
6.0
polar_of
6.0
point_at
2.0
CRAP 3.2
1 package Term::Fabulous::Chart::Radial;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.02';
9  
10 use Exporter 'import';
11 our @EXPORT_OK = qw(CELL_ASPECT TAU circle_frame polar_of point_at);
12  
13 use List::Util qw(min);
14  
15 # A terminal cell is about twice as high as it is wide; round charts
16 # measure in cell widths and stretch rows by this factor.
17 use constant CELL_ASPECT => 2;
18 use constant TAU => 8 * atan2( 1, 1 );
19  
20 # The largest circle that fits an area of cells: its center in cells
21 # (continuous) and its radius in cell widths. $margin cell widths stay
22 # free around it.
23 sub circle_frame ( $x, $y, $width, $height, $margin = 0 ) {
24 my $radius = min( $width / 2, $height * CELL_ASPECT / 2 ) - $margin;
25 return { center_x => $x + $width / 2, center_y => $y + $height / 2, radius => $radius > 0 ? $radius : 0 };
26 }
27  
28 # The polar coordinates of a point in cells: the distance from the
29 # center in cell widths and the angle in turns (0 to 1) clockwise from
30 # $start (in turns; 0 is 12 o'clock).
31 sub polar_of ( $frame, $x, $y, $start = 0 ) {
32 my $dx = $x - $frame->{center_x};
33 my $dy = ( $y - $frame->{center_y} ) * CELL_ASPECT;
34 my $angle = atan2( $dx, -$dy ) / TAU - $start;
35 $angle -= int($angle);
36 $angle += 1 if $angle < 0;
37 return ( sqrt( $dx**2 + $dy**2 ), $angle );
38 }
39  
40 # The point in cells at a distance (cell widths) and an angle (turns
41 # clockwise from 12 o'clock, after $start).
42 sub point_at ( $frame, $distance, $angle, $start = 0 ) {
43 my $radians = ( $angle + $start ) * TAU;
44 return ( $frame->{center_x} + $distance * sin($radians), $frame->{center_y} - $distance * cos($radians) / CELL_ASPECT );
45 }
46  
47 1;
48  
49 __END__