File: FibonacciSquareSpiral.pm

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (183 lines) | stat: -rw-r--r-- 4,376 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright 2012, 2013 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.


# Grows too quickly to be interesting.
#
# math-image --path=FibonacciSquareSpiral --lines --scale=10
# math-image --path=FibonacciSquareSpiral --output=numbers


# #------------------------------------------------------------------------------
# # A138710 - abs(dX) but OFFSET=1
# 
# MyOEIS::compare_values
#   (anum => 'A138710',
#    func => sub {
#      my ($count) = @_;
#      my @got;
#      for (my $n = $path->n_start; @got < $count; $n++) {
#        my ($dx,$dy) = $path->n_to_dxdy($n);
#        push @got, abs($dx);
#      }
#      return \@got;
#    });
# 
# # A138709 - abs(dY) but OFFSET=1
# MyOEIS::compare_values
#   (anum => 'A138709',
#    func => sub {
#      my ($count) = @_;
#      my @got;
#      for (my $n = $path->n_start; @got < $count; $n++) {
#        my ($dx,$dy) = $path->n_to_dxdy($n);
#        push @got, abs($dy);
#      }
#      return \@got;
#    });


package Math::PlanePath::FibonacciSquareSpiral;
use 5.004;
use strict;
#use List::Util 'max';
*max = \&Math::PlanePath::_max;

use vars '$VERSION', '@ISA';
$VERSION = 104;
use Math::PlanePath 37;
@ISA = ('Math::PlanePath');

use Math::PlanePath::Base::Generic
  'is_infinite',
  'round_nearest';
use Math::PlanePath::Base::Digits
  'digit_split_lowtohigh';

# uncomment this to run the ### lines
#use Devel::Comments;

use constant default_n_start => 1;

sub new {
  my $self = shift->SUPER::new (@_);
  if (! defined $self->{'n_start'}) {
    $self->{'n_start'} = $self->default_n_start;
  }
  return $self;
}

sub n_to_xy {
  my ($self, $n) = @_;
  #### SquareSpiral n_to_xy: $n

  $n = $n - $self->{'n_start'};  # starting $n==0, warn if $n==undef
  if ($n < 0) {
    #### before n_start ...
    return;
  }

  my $f0 = int($n)*0;  # inherit BigInt zero
  my $f1 = $f0 + 1;    # inherit BigInt one
  my $x = 0;
  my $y = 0;
  my $dx = 1;
  my $dy = 0;
  while ($n > $f1) {
    $n -= $f1;
    $x += $dx * $f1;
    $y += $dy * $f1;
    ($f1,$f0) = ($f1+$f0,$f1);
    ($dx,$dy) = (-$dy,$dx); # rotate +90
  }
  return ($n*$dx + $x, $n*$dy + $y);
}

sub xy_to_n {
  my ($self, $x, $y) = @_;
  ### FibonacciSquareSpiral xy_to_n() ...

  return undef;
}

sub rect_to_n_range {
  my ($self, $x1,$y1, $x2,$y2) = @_;
  return ($self->{'n_start'},
          $self->{'n_start'} + max($x1,$x2,$y1,$y2)**2);
}

1;
__END__

=for stopwords eg Ryde

=head1 NAME

Math::PlanePath::FibonacciSquareSpiral -- spiral with Fibonacci number sides

=head1 SYNOPSIS

 use Math::PlanePath::FibonacciSquareSpiral;
 my $path = Math::PlanePath::FibonacciSquareSpiral->new;
 my ($x, $y) = $path->n_to_xy (123);

=head1 DESCRIPTION

This path is ...

=head1 FUNCTIONS

See L<Math::PlanePath/FUNCTIONS> for the behaviour common to all path
classes.

=over 4

=item C<$path = Math::PlanePath::FibonacciSquareSpiral-E<gt>new ()>

Create and return a new path object.

=back

=head1 SEE ALSO

L<Math::PlanePath>,
L<Math::PlanePath::SquareSpiral>

=head1 HOME PAGE

http://user42.tuxfamily.org/math-planepath/index.html

=head1 LICENSE

Copyright 2012, 2013 Kevin Ryde

This file is part of Math-PlanePath.

Math-PlanePath is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

Math-PlanePath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.

=cut