blib/lib/Math/Spiral.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 8 / 8 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 100% 75 / 75 SCAR 24.0
CC
11
Cov
100%
Next
9.0
new
3.0
BEGIN
1.0
CRAP 11.0
↑ Summary
1 package Math::Spiral;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # perl -MPod::Markdown -e 'Pod::Markdown->new->filter(@ARGV)' lib/Math/Spiral.pm > README.md
7  
8 =head1 NAME
9  
10 Math::Spiral - Perl extension to return an endless stream of X, Y offset coordinates which represent a spiral shape
11  
12 =head1 SYNOPSIS
13  
14  
15 #!/usr/bin/perl -w
16
17 use Math::Spiral;
18  
19 my $s = new Math::Spiral();
20 my($xo,$yo)=$s->Next();
21  
22  
23 # perl -MMath::Spiral -e '$s=new Math::Spiral(); foreach(0..9) { ($xo,$yo)=$s->Next(); $chart[2+$xo][2+$yo]=$_; } foreach $y (0..4){foreach $x(0..4){if(defined($chart[$x][$y])){print $chart[$x][$y]} else {print " ";} } print "\n"}'
24  
25 =head1 DESCRIPTION
26  
27 This module outputs an infinte sequence of coordinate offsets, which you can use to plot things in a spiral shape.
28 The numbers return "clockwise"; negate one if you want to go anti-clockwise instead.
29  
30 It is useful for charting things where you need to concentrate something around the center of the chart.
31  
32 =head2 EXAMPLE
33  
34 #!/usr/bin/perl -w
35
36 use Math::Spiral;
37  
38 my $s = new Math::Spiral();
39  
40 foreach(0..9) {
41 ($xo,$yo)=$s->Next(); # Returns a sequnce like (0,0) (1,0) (1,1) (0,1) (-1,1) (-1,0) (-1,-1) (0,-1) (1,-1) (2,-1) ... etc
42 $chart[2+$xo][2+$yo]=$_;
43 }
44  
45 foreach $y (0..4) {
46 foreach $x(0..4) {
47 if(defined($chart[$x][$y])) {
48 print $chart[$x][$y]
49 } else {
50 print " ";
51 }
52 }
53 print "\n"
54 }
55  
56 =head3 Prints
57  
58 6789
59 501
60 432
61  
62  
63 =head2 EXPORT
64  
65 None by default.
66  
67  
68 =head2 Notes
69  
70 =head2 new
71  
72 Usage is
73  
74 my $s = new Math::Spiral();
75  
76  
77 =head2 Next
78  
79 Returns the next x and y offsets (note that these start at 0,0 and will go negative to circle around this origin)
80  
81 Usage is
82  
83 my($xo,$yo)=$s->Next();
84 # Returns a sequnce like (0,0) (1,0) (1,1) (0,1) (-1,1) (-1,0) (-1,-1) (0,-1) (1,-1) (2,-1) ... etc (i.e. the x,y coordinates for a spiral)
85  
86 =cut
87  
88 1 require Exporter;
89  
90 1 our @ISA = qw(Exporter);
91 1 our($VERSION)='1.01';
92 1 our($UntarError) = '';
93  
94 1 our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
95  
96 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
97  
98 1 our @EXPORT = qw( );
99  
100  
101 sub new {
102 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
103 1 my $this={};
104 1 foreach(qw(x xmin y ydir ymin)){ $this->{$_}=0; }
105 1 foreach(qw(xdir xmax ymax)){ $this->{$_}=1; }
106 # my($x,$xdir,$xmax,$xmin) = (0,1,1,0);
107 # my($y,$ydir,$ymax,$ymin) = (0,0,1,0);
108 1 bless $this,$class;
109 1 return $this;
110 } # new
111  
112  
113  
114 sub Next {
115 10 ▶ my $this = shift;
Subroutine Next: called CC=9 SCAR=22.0
Pod documented
116  
117 10 my @ret=($this->{x},$this->{y});
118  
119 10 $this->{x}+=$this->{xdir};
120 10 $this->{y}+=$this->{ydir};
121  
122 10 ▶ if( ($this->{x}>=$this->{xmax})&&($this->{xdir})) {
Branch: 4 branches
BranchTrueFalseTotal
if ($this->{'x'} >= $this->{'xmax'} and $this->{'xdir'}) { } 2 8 10
elsif ($this->{'x'} <= $this->{'xmin'} and $this->{'xdir'}) { } 1 7 8
elsif ($this->{'y'} >= $this->{'ymax'} and $this->{'ydir'}) { } 1 6 7
elsif ($this->{'y'} <= $this->{'ymin'} and $this->{'ydir'}) { } 1 8 9
Condition100%3/3
$this->{"x"} >= $this->{"xmax"} and $this->{"xdir"}
!ll&&!rl&&r
622
MC/DC100%2/2
$this->{"x"} >= $this->{"xmax"} and $this->{"xdir"}
$this->{"x"} >= $this->{"xmax"} $this->{"xdir"}
Truth table100%3/3
A and B
A= $this->{"x"} >= $this->{"xmax"}B= $this->{"xdir"}
ABresult
0X0
100
111
123 2 $this->{xmin}=-$this->{xmax}; $this->{xdir}=0; $this->{ydir}=1;
124 ▶ } elsif(($this->{x}<=$this->{xmin})&&($this->{xdir})) {
Condition100%3/3
$this->{"x"} <= $this->{"xmin"} and $this->{"xdir"}
!ll&&!rl&&r
521
MC/DC100%2/2
$this->{"x"} <= $this->{"xmin"} and $this->{"xdir"}
$this->{"x"} <= $this->{"xmin"} $this->{"xdir"}
Truth table100%3/3
A and B
A= $this->{"x"} <= $this->{"xmin"}B= $this->{"xdir"}
ABresult
0X0
100
111
125 1 $this->{xmax}=-$this->{xmin}+1; $this->{xdir}=0; $this->{ydir}=-1;
126 ▶ } elsif(($this->{y}>=$this->{ymax})&&($this->{ydir})) {
Condition100%3/3
$this->{"y"} >= $this->{"ymax"} and $this->{"ydir"}
!ll&&!rl&&r
511
MC/DC100%2/2
$this->{"y"} >= $this->{"ymax"} and $this->{"ydir"}
$this->{"y"} >= $this->{"ymax"} $this->{"ydir"}
Truth table100%3/3
A and B
A= $this->{"y"} >= $this->{"ymax"}B= $this->{"ydir"}
ABresult
0X0
100
111
127 1 $this->{ymin}=-$this->{ymax}; $this->{xdir}=-1; $this->{ydir}=0;
128 ▶ } elsif(($this->{y}<=$this->{ymin})&&($this->{ydir})) {
Condition100%3/3
$this->{"y"} <= $this->{"ymin"} and $this->{"ydir"}
!ll&&!rl&&r
321
MC/DC100%2/2
$this->{"y"} <= $this->{"ymin"} and $this->{"ydir"}
$this->{"y"} <= $this->{"ymin"} $this->{"ydir"}
Truth table100%3/3
A and B
A= $this->{"y"} <= $this->{"ymin"}B= $this->{"ydir"}
ABresult
0X0
100
111
129 1 $this->{ymax}=-$this->{ymin}+1; $this->{xdir}=1; $this->{ydir}=0;
130 }
131  
132 10 return @ret;
133 } # Next
134  
135 # testing # perl -MMath::Spiral -e '$s=new Math::Spiral(); foreach(0..25) { ($xo,$yo)=$s->Next(); $chart[3+$xo][3+$yo]=$_; } foreach $y (0..6){foreach $x(0..6){if(defined($chart[$x][$y])){print chr(97+$chart[$x][$y])} else {print " ";} } print "\n"}'
136  
137  
138  
139 1 1;
140  
141 __END__
↑ Summary