.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Math::PlanePath::SquareSpiral 3pm" .TH Math::PlanePath::SquareSpiral 3pm "2021-01-23" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Math::PlanePath::SquareSpiral \-\- integer points drawn around a square (or rectangle) .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use Math::PlanePath::SquareSpiral; \& my $path = Math::PlanePath::SquareSpiral\->new; \& my ($x, $y) = $path\->n_to_xy (123); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This path makes a square spiral, .PP .Vb 10 \& 37\-\-36\-\-35\-\-34\-\-33\-\-32\-\-31 3 \& | | \& 38 17\-\-16\-\-15\-\-14\-\-13 30 2 \& | | | | \& 39 18 5\-\-\-4\-\-\-3 12 29 1 \& | | | | | | \& 40 19 6 1\-\-\-2 11 28 ... <\- Y=0 \& | | | | | | \& 41 20 7\-\-\-8\-\-\-9\-\-10 27 52 \-1 \& | | | | \& 42 21\-\-22\-\-23\-\-24\-\-25\-\-26 51 \-2 \& | | \& 43\-\-44\-\-45\-\-46\-\-47\-\-48\-\-49\-\-50 \-3 \& \& ^ \& \-3 \-2 \-1 X=0 1 2 3 4 .Ve .PP See \fIexamples/square\-numbers.pl\fR for a simple program printing these numbers. .SS "Ulam Spiral" .IX Subsection "Ulam Spiral" This path is well known from Stanislaw Ulam finding interesting straight lines when plotting the prime numbers on it. .Sp .RS 4 Stein, Ulam and Wells, \*(L"A Visual Display of Some Properties of the Distribution of Primes\*(R", American Mathematical Monthly, volume 71, number 5, May 1964, pages 516\-520. .RE .PP The cover of Scientific American March 1964 featured this spiral, .Sp .RS 4 .Sp .RE .PP See \fIexamples/ulam\-spiral\-xpm.pl\fR for a standalone program, or see math-image using this \f(CW\*(C`SquareSpiral\*(C'\fR to draw this pattern and more. .PP Stein, Ulam and Wells above also considered primes on the Math::PlanePath::Corner path, and on a half-plane like two corners. .SS "Straight Lines" .IX Subsection "Straight Lines" The perfect squares 1,4,9,16,25 fall on two diagonals with the even perfect squares going to the upper left and the odd squares to the lower right. The pronic numbers 2,6,12,20,30,42 etc k^2+k half way between the squares fall on similar diagonals to the upper right and lower left. The decagonal numbers 10,27,52,85 etc 4*k^2\-3*k go horizontally to the right at Y=\-1. .IX Xref "Square numbers Pronic numbers" .PP In general, straight lines and diagonals are 4*k^2 + b*k + c. b=0 is the even perfect squares up to the left, then incrementing b is an eighth turn anti-clockwise, or clockwise if negative. So b=1 is horizontal West, b=2 diagonally down South-West, b=3 down South, etc. .PP Honaker's prime-generating polynomial 4*k^2 + 4*k + 59 goes down to the right after the first 30 or so values loop around a bit. .SS "Wider" .IX Subsection "Wider" An optional \f(CW\*(C`wider\*(C'\fR parameter makes the path wider, becoming a rectangle spiral instead of a square. For example .PP .Vb 1 \& wider => 3 \& \& 29\-\-28\-\-27\-\-26\-\-25\-\-24\-\-23\-\-22 2 \& | | \& 30 11\-\-10\-\- 9\-\- 8\-\- 7\-\- 6 21 1 \& | | | | \& 31 12 1\-\- 2\-\- 3\-\- 4\-\- 5 20 <\- Y=0 \& | | | \& 32 13\-\-14\-\-15\-\-16\-\-17\-\-18\-\-19 \-1 \& | \& 33\-\-34\-\-35\-\-36\-... \-2 \& \& ^ \& \-4 \-3 \-2 \-1 X=0 1 2 3 .Ve .PP The centre horizontal 1 to 2 is extended by \f(CW\*(C`wider\*(C'\fR many further places, then the path loops around that shape. The starting point 1 is shifted to the left by ceil(wider/2) places to keep the spiral centred on the origin X=0,Y=0. .PP Widening doesn't change the nature of the straight lines which arise, it just rotates them around. For example in this wider=3 example the perfect squares are still on diagonals, but the even squares go towards the bottom left (instead of top left when wider=0) and the odd squares to the top right (instead of the bottom right). .PP Each loop is still 8 longer than the previous, since the widening is a constant amount in each loop. .SS "N Start" .IX Subsection "N Start" The default is to number points starting N=1 as shown above. An optional \&\f(CW\*(C`n_start\*(C'\fR can give a different start with the same shape. For example to start at 0, .PP .Vb 1 \& n_start => 0 \& \& 16\-15\-14\-13\-12 ... \& | | | \& 17 4\-\-3\-\-2 11 28 \& | | | | | \& 18 5 0\-\-1 10 27 \& | | | | \& 19 6\-\-7\-\-8\-\-9 26 \& | | \& 20\-21\-22\-23\-24\-25 .Ve .PP The only effect is to push the N values around by a constant amount. It might help match coordinates with something else zero-based. .SS "Corners" .IX Subsection "Corners" Other spirals can be formed by cutting the corners of the square so as to go around faster. See the following modules, .PP .Vb 6 \& Corners Cut Class \& \-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\- \& 1 HeptSpiralSkewed \& 2 HexSpiralSkewed \& 3 PentSpiralSkewed \& 4 DiamondSpiral .Ve .PP The \f(CW\*(C`PyramidSpiral\*(C'\fR is a re-shaped \f(CW\*(C`SquareSpiral\*(C'\fR looping at the same rate. It shifts corners but doesn't cut them. .SH "FUNCTIONS" .IX Header "FUNCTIONS" See \*(L"\s-1FUNCTIONS\*(R"\s0 in Math::PlanePath for behaviour common to all path classes. .ie n .IP """$path = Math::PlanePath::SquareSpiral\->new ()""" 4 .el .IP "\f(CW$path = Math::PlanePath::SquareSpiral\->new ()\fR" 4 .IX Item "$path = Math::PlanePath::SquareSpiral->new ()" .PD 0 .ie n .IP """$path = Math::PlanePath::SquareSpiral\->new (wider => $integer, n_start => $n)""" 4 .el .IP "\f(CW$path = Math::PlanePath::SquareSpiral\->new (wider => $integer, n_start => $n)\fR" 4 .IX Item "$path = Math::PlanePath::SquareSpiral->new (wider => $integer, n_start => $n)" .PD Create and return a new square spiral object. An optional \f(CW\*(C`wider\*(C'\fR parameter widens the spiral path, it defaults to 0 which is no widening. .ie n .IP """($x,$y) = $path\->n_to_xy ($n)""" 4 .el .IP "\f(CW($x,$y) = $path\->n_to_xy ($n)\fR" 4 .IX Item "($x,$y) = $path->n_to_xy ($n)" Return the X,Y coordinates of point number \f(CW$n\fR on the path. .Sp For \f(CW\*(C`$n < 1\*(C'\fR the return is an empty list, as the path starts at 1. .ie n .IP """$n = $path\->xy_to_n ($x,$y)""" 4 .el .IP "\f(CW$n = $path\->xy_to_n ($x,$y)\fR" 4 .IX Item "$n = $path->xy_to_n ($x,$y)" Return the point number for coordinates \f(CW\*(C`$x,$y\*(C'\fR. \f(CW$x\fR and \f(CW$y\fR are each rounded to the nearest integer, which has the effect of treating each N in the path as centred in a square of side 1, so the entire plane is covered. .SH "FORMULAS" .IX Header "FORMULAS" .SS "N to X,Y" .IX Subsection "N to X,Y" There's a few ways to break an N into a side and offset into the side. One convenient way is to treat a loop as starting at the bottom right turn, so N=2,10,26,50,etc, If the first loop at N=2 is reckoned loop number d=1 then the loop starts at .PP .Vb 3 \& Nbase = 4*d^2 \- 4*d + 2 \& = 2,10,26,50,... for d=1,2,3,4,... \& (A069894 but it going from d=0) .Ve .PP For example d=3 is Nbase=4*3^2\-4*3+2=26 at X=3,Y=\-2. The biggest d with Nbase <= N can be found by inverting with the usual quadratic formula .PP .Vb 1 \& d = floor( 1/2 + sqrt(N/4 \- 1/4) ) .Ve .PP For Perl it's good to keep the sqrt argument an integer (when a \s-1UV\s0 integer is bigger than an \s-1NV\s0 float, and for BigRat accuracy), so rearrange to .PP .Vb 1 \& d = floor( (1+sqrt(N\-1))/2 ) .Ve .PP So Nbase from this d leaves a remainder which is an offset into the loop .PP .Vb 2 \& Nrem = N \- Nbase \& = N \- (4*d^2 \- 4*d + 2) .Ve .PP The loop starts at X=d,Y=d\-1 and has sides length 2d, 2d+1, 2d+1 and 2d+2, .PP .Vb 10 \& 2d \& +\-\-\-\-\-\-\-\-\-\-\-\-+ <\- Y=d \& | | \& 2d | | 2d\-1 \& | . | \& | | \& | + X=d,Y=\-d+1 \& | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ <\- Y=\-d \& 2d+1 \& \& ^ \& X=\-d .Ve .PP The X,Y for an Nrem is then .PP .Vb 10 \& side Nrem range X,Y result \& \-\-\-\- \-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- \& right Nrem <= 2d\-1 X = d \& Y = \-d+1+Nrem \& top 2d\-1 <= Nrem <= 4d\-1 X = d\-(Nrem\-(2d\-1)) = 3d\-1\-Nrem \& Y = d \& left 4d\-1 <= Nrem <= 6d\-1 X = \-d \& Y = d\-(Nrem\-(4d\-1)) = 5d\-1\-Nrem \& bottom 6d\-1 <= Nrem X = \-d+(Nrem\-(6d\-1)) = \-7d+1+Nrem \& Y = \-d .Ve .PP The corners Nrem=2d\-1, Nrem=4d\-1 and Nrem=6d\-1 get the same result from the two sides that meet so it doesn't matter if the high comparison is \*(L"<\*(R" or \*(L"<=\*(R". .PP The bottom edge runs through to Nrem < 8d, but there's no need to check that since d=floor(\fBsqrt()\fR) above ensures Nrem is within the loop. .PP A small simplification can be had by subtracting an extra 4d\-1 from Nrem to make negatives for the right and top sides and positives for the left and bottom. .PP .Vb 3 \& Nsig = N \- Nbase \- (4d\-1) \& = N \- (4*d^2 \- 4*d + 2) \- (4d\-1) \& = N \- (4*d^2 + 1) \& \& side Nsig range X,Y result \& \-\-\-\- \-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- \& right Nsig <= \-2d X = d \& Y = d+(Nsig+2d) = 3d+Nsig \& top \-2d <= Nsig <= 0 X = \-d\-Nsig \& Y = d \& left 0 <= Nsig <= 2d X = \-d \& Y = d\-Nsig \& bottom 2d <= Nsig X = \-d+1+(Nsig\-(2d+1)) = Nsig\-3d \& Y = \-d .Ve .PP This calculation can be found in .Sp .RS 4 Ronald L. Graham, Donald E. Knuth, Oren Patashnik, \*(L"Concrete Mathematics\*(R", Addison-Wesley, 1989, chapter 3 \*(L"Integer Functions\*(R", exercise 40 page 99, answer page 498. .RE .PP They start the spiral from 0, and first step North so their x is \-Y here. Their formula for x(n) tests a floor(2*sqrt(N)) to decide whether on a horizontal and so whether to apply the equivalent of Nrem to the result. .SS "N to X,Y with Wider" .IX Subsection "N to X,Y with Wider" With the \f(CW\*(C`wider\*(C'\fR parameter stretching the spiral loops the formulas above become .PP .Vb 1 \& Nbase = 4*d^2 + (\-4+2w)*d + 2\-w \& \& d = floor ((2\-w + sqrt(4N + w^2 \- 4)) / 4) .Ve .PP Notice for Nbase the w is a term 2*w*d, being an extra 2*w for each loop. .PP The left offset ceil(w/2) described above (\*(L"Wider\*(R") for the N=1 starting position is written here as wl, and the other half wr arises too, .PP .Vb 2 \& wl = ceil(w/2) \& wr = floor(w/2) = w \- wl .Ve .PP The horizontal lengths increase by w, and positions shift by wl or wr, but the verticals are unchanged. .PP .Vb 10 \& 2d+w \& +\-\-\-\-\-\-\-\-\-\-\-\-+ <\- Y=d \& | | \& 2d | | 2d\-1 \& | . | \& | | \& | + X=d+wr,Y=\-d+1 \& | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ <\- Y=\-d \& 2d+1+w \& \& ^ \& X=\-d\-wl .Ve .PP The Nsig formulas then have w, wl or wr variously inserted. In all cases if w=wl=wr=0 then they simplify to the plain versions. .PP .Vb 2 \& Nsig = N \- Nbase \- (4d\-1+w) \& = N \- ((4d + 2w)*d + 1) \& \& side Nsig range X,Y result \& \-\-\-\- \-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- \& right Nsig <= \-(2d+w) X = d+wr \& Y = d+(Nsig+2d+w) = 3d+w+Nsig \& top \-(2d+w) <= Nsig <= 0 X = \-d\-wl\-Nsig \& Y = d \& left 0 <= Nsig <= 2d X = \-d\-wl \& Y = d\-Nsig \& bottom 2d <= Nsig X = \-d+1\-wl+(Nsig\-(2d+1)) = Nsig\-wl\-3d \& Y = \-d .Ve .SS "Rectangle to N Range" .IX Subsection "Rectangle to N Range" Within each row the minimum N is on the X=Y diagonal and N values increases monotonically as X moves away to the left or right. Similarly in each column there's a minimum N on the X=\-Y opposite diagonal, or X=\-Y+1 diagonal when X negative, and N increases monotonically as Y moves away from there up or down. When wider>0 the location of the minimum changes, but N is still monotonic moving away from the minimum. .PP On that basis the maximum N in a rectangle is at one of the four corners, .PP .Vb 8 \& | \& x1,y2 M\-\-\-|\-\-\-\-M x2,y2 corner candidates \& | | | for maximum N \& \-\-\-\-\-\-\-O\-\-\-\-\-\-\-\-\- \& | | | \& | | | \& x1,y1 M\-\-\-|\-\-\-\-M x1,y1 \& | .Ve .SH "OEIS" .IX Header "OEIS" This path is in Sloane's Online Encyclopedia of Integer Sequences in various forms. Summary at .Sp .RS 4 .RE .PP And various sequences, .Sp .RS 4 (etc), .RE .PP .Vb 6 \& wider=0 (the default) \& A174344 X coordinate \& A274923 Y coordinate \& A268038 negative Y \& A296030 X,Y pairs \& A214526 abs(X)+abs(Y) "Manhattan" distance \& \& A079813 abs(dY), being k 0s followed by k 1s \& A055086 direction (total turn) \& A000267 direction + 1 \& A063826 direction 1=right,2=up,3=left,4=down \& \& A027709 boundary length of N unit squares \& A078633 grid sticks to make N unit squares \& \& A240025 turn 1=left,0=straight (extra initial 1) \& \& A033638 N turn positions (extra initial 1, 1) \& A172979 N turn positions which are primes too \& A242601 X and Y location of origin then each turn, \& X=A242601(n+1), Y=A242601(n) \& A080037 N straight\-ahead (except initial 2) \& A248333 num straight points among the first N \& A083479 num non\-turn points among the first N \& (straight and the origin) \& \& A054552 N values on X axis (East) \& A054556 N values on Y axis (North) \& A054567 N values on negative X axis (West) \& A033951 N values on negative Y axis (South) \& A054554 N values on X=Y diagonal (NE) \& A054569 N values on negative X=Y diagonal (SW) \& A053755 N values on X=\-Y opp diagonal X<=0 (NW) \& A016754 N values on X=\-Y opp diagonal X>=0 (SE) \& A200975 N values on all four diagonals \& A317186 N on Y axis positive and negative \& A267682 N on Y axis positive and negative (origin twice) \& A265400 N inner neighbour \& \& A137928 N values on X=\-Y+1 opposite diagonal \& A002061 N values on X=Y diagonal pos and neg \& A016814 (4k+1)^2, every second N on south\-east diagonal \& \& A143856 N values on ENE slope dX=2,dY=1 \& A143861 N values on NNE slope dX=1,dY=2 \& A215470 N prime and >=4 primes among its 8 neighbours \& \& A214664 X coordinate of prime N (Ulam\*(Aqs spiral) \& A214665 Y coordinate of prime N (Ulam\*(Aqs spiral) \& A214666 \-X \e reckoning spiral starting West \& A214667 \-Y / \& \& A053999 prime[N] on X=\-Y opp diagonal X>=0 (SE) \& A054551 prime[N] on the X axis (E) \& A054553 prime[N] on the X=Y diagonal (NE) \& A054555 prime[N] on the Y axis (N) \& A054564 prime[N] on X=\-Y opp diagonal X<=0 (NW) \& A054566 prime[N] on negative X axis (W) \& \& A090925 permutation N at rotate +90 \& A090928 permutation N at rotate +180 \& A090929 permutation N at rotate +270 \& A090930 permutation N at clockwise spiralling \& A020703 permutation N at rotate +90 and go clockwise \& A090861 permutation N at rotate +180 and go clockwise \& A090915 permutation N at rotate +270 and go clockwise \& A185413 permutation N at 1\-X,Y \& being rotate +180, offset X+1, clockwise \& \& A068225 permutation N at X+1,Y \& A121496 run lengths of consecutive N in this permutation \& A068226 permutation N at X\-1,Y \& A334752 permutation N at X,Y+1 \& A334751 permutation N at X,Y\-1 \& A020703 permutation N at transpose Y,X \& (clockwise <\-> anti\-clockwise) \& \& A033952 digits on negative Y axis \& A033953 digits on negative Y axis, starting 0 \& A033988 digits on negative X axis, starting 0 \& A033989 digits on Y axis, starting 0 \& A033990 digits on X axis, starting 0 \& \& A062410 total sum previous row or column \& \& wider=1 \& A069894 N on South\-West diagonal .Ve .PP The following have \*(L"offset 0\*(R" S and therefore start from N=0. .PP .Vb 3 \& n_start=0 \& A180714 X+Y coordinate sum \& A053615 abs(X\-Y), runs n to 0 to n, distance to nearest pronic \& \& A001107 N on X axis \& A033991 N on Y axis \& A033954 N on negative Y axis, second 10\-gonals \& A002939 N on X=Y diagonal North\-East \& A016742 N on North\-West diagonal, 4*k^2 \& A002943 N on South\-West diagonal \& A156859 N on Y axis positive and negative .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Math::PlanePath, Math::PlanePath::PyramidSpiral .PP Math::PlanePath::DiamondSpiral, Math::PlanePath::PentSpiralSkewed, Math::PlanePath::HexSpiralSkewed, Math::PlanePath::HeptSpiralSkewed .PP Math::PlanePath::CretanLabyrinth .PP Math::NumSeq::SpiroFibonacci .PP X11 cursor font \*(L"box spiral\*(R" cursor which is this style (but going clockwise). .SH "HOME PAGE" .IX Header "HOME PAGE" .SH "LICENSE" .IX Header "LICENSE" Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Kevin Ryde .PP This file is part of Math-PlanePath. .PP Math-PlanePath is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. .PP Math-PlanePath is distributed in the hope that it will be useful, but \&\s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS FOR A PARTICULAR PURPOSE.\s0 See the \s-1GNU\s0 General Public License for more details. .PP You should have received a copy of the \s-1GNU\s0 General Public License along with Math-PlanePath. If not, see .