The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::PlanePath::MooreSpiral -- 9-segment self-similar spiral

SYNOPSIS

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

DESCRIPTION

This is an integer version of a 9-segment self-similar curve by ...

                               61-62 67-68-69-70            4
                                |  |  |        |
                               60 63 66 73-72-71            3
                                |  |  |  |
                               59 64-65 74-75-76            2
                                |              |
    11-10  5--4--3--2          58-57-56 83-82 77            1
     |  |  |        |                 |  |  |  |
    12  9  6     0--1          53-54-55 84 81 78       <- Y=0
     |  |  |                    |        |  |  |
    13  8--7                   52-51-50 85 80-79           -1
     |                                |  |
    14-15-16 25-26 31-32-33-34 43-44 49 86-87-88 97-98     -2
           |  |  |  |        |  |  |  |        |  |  |
    19-18-17 24 27 30 37-36-35 42 45 48 91-90-89 96 99     -3
     |        |  |  |  |        |  |  |  |        |  |
    20-21-22-23 28-29 38-39-40-41 46-47 92-93-94-95 ...    -4

    -4 -3 -2 -1 X=0 1  2  3  4  5  6  7  8  9 10 11 12

The base pattern is the N=0 to N=9 shape. Then there's 9 copies of that shape in the same relative directions as those segments and with reversals in the 3,6,7,8 parts. The first reversed section is N=3*9=27 to N=4*9=36.

                       rev
              5------4------3------2
              |                    |
              |                    |
       9      6             0------1
       |      |rev
    rev|      |
       8------7
         rev

Notice the points N=9,18,27,...,81 are the base shape rotated 180 degrees. Likewise for N=81,162,etc and any multiples of N=9^level, with each successive level being rotated 180 degrees relative to the preceding. The effect is to spiral around with an ever fatter 3^level width,

    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ******************************************************
    ***************************                  *********
    ***************************                  *********
    ***************************                  *********
    ***************************         ******   *********
    ***************************         *** **   *********
    ***************************         ***      *********
    ***************************         ******************
    ***************************         ******************
    ***************************         ******************
    ***************************
    ***************************
    ***************************
    ***************************
    ***************************
    ***************************
    ***************************
    ***************************
    ***************************

Arms

The optional arms => 2 parameter can give a second copy of the spiral rotated 180 degrees. With two arms all points of the plane are covered.

     93--91  81--79--77--75  57--55  45--43--41--39 122-124  ..
      |   |   |           |   |   |   |           |   |   |   |
     95  89  83  69--71--73  59  53  47  33--35--37 120 126 132 
      |   |   |   |           |   |   |   |           |   |   | 
     97  87--85  67--65--63--61  51--49  31--29--27 118 128-130 
      |                                           |   |
     99-101-103  22--20  10-- 8-- 6-- 4  13--15  25 116-114-112 
              |   |   |   |           |   |   |   |           | 
    109-107-105  24  18  12   1   0-- 2  11  17  23 106-108-110 
      |           |   |   |   |           |   |   |   |         
    111-113-115  26  16--14   3-- 5-- 7-- 9  19--21 104-102-100 
              |   |                                           | 
    129-127 117  28--30--32  50--52  62--64--66--68  86--88  98 
      |   |   |           |   |   |   |           |   |   |   |
    131 125 119  38--36--34  48  54  60  74--72--70  84  90  96 
      |   |   |   |           |   |   |   |           |   |   | 
     .. 123-121  40--42--44--46  56--58  76--78--80--82  92--94 

The first arm is the even numbers N=0,2,4,etc and the second arm is the odd numbers N=1,3,5,etc.

Wunderlich Serpentine Curve

The way the ends join makes little "S" shapes similar to the PeanoCurve. The first is at N=5 to N=13,

    11-10  5
     |  |  |
    12  9  6
     |  |  | 
    13  8--7 

The wider parts then have these sections alternately horizontal or vertical in the style of Walter Wunderlich's "serpentine" type 010 101 010 curve. For example the 9x9 block N=41 to N=101,

    61--62  67--68--69--70 115-116 121
     |   |   |           |   |   |   |
    60  63  66  73--72--71 114 117 120
     |   |   |   |           |   |   |
    59  64--65  74--75--76 113 118-119
     |                   |   |        
    58--57--56  83--82  77 112-111-110
             |   |   |   |           |
    53--54--55  84  81  78 107-108-109
     |           |   |   |   |        
    52--51--50  85  80--79 106-105-104
             |   |                   |
    43--44  49  86--87--88  97--98 103
     |   |   |           |   |   |   |
    42  45  48  91--90--89  96  99 102
     |   |   |   |           |   |   |
    41  46--47  92--93--94--95 100-101

The whole curve is in fact like the Wunderlich serpentine started from the middle. This can be seen in the two arms picture above (in mirror image of the usual PlanePath start direction for Wunderlich's curve).

FUNCTIONS

See "FUNCTIONS" in Math::PlanePath for the behaviour common to all path classes.

$path = Math::PlanePath::MooreSpiral->new ()

Create and return a new path object.

($x,$y) = $path->n_to_xy ($n)

Return the X,Y coordinates of point number $n on the path. Points begin at 0 and if $n < 0 then the return is an empty list.

FORMULAS

X,Y to N

The correspondence to Wunderlich's 3x3 serpentine curve can be used to turn X,Y coordinates in base 3 into an N. Reckoning the innermost 3x3 as level=1 then the smallest abs(X) or abs(Y) in a level is

    Xlevelmin = (3^level + 1) / 2
    eg. level=2 Xlevelmin=5

which can be reversed as

    level = log3floor( max(abs(X),abs(Y)) * 2 - 1 )
    eg. X=7 level=log3floor(2*7-1)=2

An offset can be applied to put X,Y in the range 0 to 3^level-1,

    offset = (3^level-1)/2
    eg. level=2 offset=4

Then a table can give the N base-9 digit corresponding to X,Y digits

    Y=2   4   3   2      N digit
    Y=1  -1   0   1
    Y=0  -2  -3  -4
         X=0 X=1 X=2

A current rotation maintains the "S" part directions and is updated by a table

    Y=2   0  +3   0     rotation when descending
    Y=1  +1  +2  +1     into sub-part
    Y=0   0  +3   0
         X=0 X=1 X=2

The negative digits of N represent backing up a little in some higher part. If N goes negative at any state then X,Y was off the main curve and instead on the second arm. If the second arm is not of interest the calculation can stop at that stage.

It no doubt would also work to take take X,Y as balanced ternary digits 1,0,-1, but it's not clear that would be any faster or easier to calculate.

SEE ALSO

Math::PlanePath, Math::PlanePath::PeanoCurve

HOME PAGE

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

LICENSE

Copyright 2011, 2012 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/>.