| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Math::Spiral; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
53335
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
24
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
410
|
|
|
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
|
|
|
|
|
|
|
require Exporter; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
91
|
|
|
|
|
|
|
our($VERSION)='1.01'; |
|
92
|
|
|
|
|
|
|
our($UntarError) = ''; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
our @EXPORT = qw( ); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub new { |
|
102
|
1
|
|
|
1
|
1
|
68
|
my $class = shift; |
|
103
|
1
|
|
|
|
|
3
|
my $this={}; |
|
104
|
1
|
|
|
|
|
3
|
foreach(qw(x xmin y ydir ymin)){ $this->{$_}=0; } |
|
|
5
|
|
|
|
|
8
|
|
|
105
|
1
|
|
|
|
|
3
|
foreach(qw(xdir xmax ymax)){ $this->{$_}=1; } |
|
|
3
|
|
|
|
|
7
|
|
|
106
|
|
|
|
|
|
|
# my($x,$xdir,$xmax,$xmin) = (0,1,1,0); |
|
107
|
|
|
|
|
|
|
# my($y,$ydir,$ymax,$ymin) = (0,0,1,0); |
|
108
|
1
|
|
|
|
|
3
|
bless $this,$class; |
|
109
|
1
|
|
|
|
|
2
|
return $this; |
|
110
|
|
|
|
|
|
|
} # new |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub Next { |
|
115
|
10
|
|
|
10
|
1
|
47
|
my $this = shift; |
|
116
|
|
|
|
|
|
|
|
|
117
|
10
|
|
|
|
|
17
|
my @ret=($this->{x},$this->{y}); |
|
118
|
|
|
|
|
|
|
|
|
119
|
10
|
|
|
|
|
12
|
$this->{x}+=$this->{xdir}; |
|
120
|
10
|
|
|
|
|
12
|
$this->{y}+=$this->{ydir}; |
|
121
|
|
|
|
|
|
|
|
|
122
|
10
|
100
|
100
|
|
|
49
|
if( ($this->{x}>=$this->{xmax})&&($this->{xdir})) { |
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
|
123
|
2
|
|
|
|
|
5
|
$this->{xmin}=-$this->{xmax}; $this->{xdir}=0; $this->{ydir}=1; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
3
|
|
|
124
|
|
|
|
|
|
|
} elsif(($this->{x}<=$this->{xmin})&&($this->{xdir})) { |
|
125
|
1
|
|
|
|
|
3
|
$this->{xmax}=-$this->{xmin}+1; $this->{xdir}=0; $this->{ydir}=-1; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
2
|
|
|
126
|
|
|
|
|
|
|
} elsif(($this->{y}>=$this->{ymax})&&($this->{ydir})) { |
|
127
|
1
|
|
|
|
|
2
|
$this->{ymin}=-$this->{ymax}; $this->{xdir}=-1; $this->{ydir}=0; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1
|
|
|
128
|
|
|
|
|
|
|
} elsif(($this->{y}<=$this->{ymin})&&($this->{ydir})) { |
|
129
|
1
|
|
|
|
|
3
|
$this->{ymax}=-$this->{ymin}+1; $this->{xdir}=1; $this->{ydir}=0; |
|
|
1
|
|
|
|
|
10
|
|
|
|
1
|
|
|
|
|
3
|
|
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
10
|
|
|
|
|
17
|
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; |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__ |