.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" 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 turned on, 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Math::PlanePath::GrayCode 3pm" .TH Math::PlanePath::GrayCode 3pm "2014-08-30" "perl v5.20.1" "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::GrayCode \-\- Gray code coordinates .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Math::PlanePath::GrayCode; \& \& my $path = Math::PlanePath::GrayCode\->new; \& my ($x, $y) = $path\->n_to_xy (123); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a mapping of N to X,Y using Gray codes. The default is the form by Christos Faloutsos which is an X,Y split in binary reflected Gray code. .IX Xref "Faloutsos, Christos Gray code" .PP .Vb 10 \& 7 | 63\-62 57\-56 39\-38 33\-32 \& | | | | | \& 6 | 60\-61 58\-59 36\-37 34\-35 \& | \& 5 | 51\-50 53\-52 43\-42 45\-44 \& | | | | | \& 4 | 48\-49 54\-55 40\-41 46\-47 \& | \& 3 | 15\-14 9\-\-8 23\-22 17\-16 \& | | | | | \& 2 | 12\-13 10\-11 20\-21 18\-19 \& | \& 1 | 3\-\-2 5\-\-4 27\-26 29\-28 \& | | | | | \& Y=0 | 0\-\-1 6\-\-7 24\-25 30\-31 \& | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& X=0 1 2 3 4 5 6 7 .Ve .PP N is converted to a Gray code, then split by bits to X,Y, and those X,Y converted back from Gray to integer indices. Stepping from N to N+1 changes just one bit of the Gray code and therefore changes just one of X or Y each time. .PP Y axis N=0,3,12,15,48,etc are values with only digits 0,3 in base 4. X axis N=0,1,6,7,24,25,etc are values 2k and 2k+1 where k uses only digits 0,3 in base 4. .SS "Radix" .IX Subsection "Radix" The default is binary. The \f(CW\*(C`radix => $r\*(C'\fR option can select another radix. This is used for both the Gray code and the digit splitting. For example \f(CW\*(C`radix => 4\*(C'\fR, .PP .Vb 1 \& radix => 4 \& \& | \& 127\-126\-125\-124 99\-\-98\-\-97\-\-96\-\-95\-\-94\-\-93\-\-92 67\-\-66\-\-65\-\-64 \& | | | | \& 120\-121\-122\-123 100\-101\-102\-103 88\-\-89\-\-90\-\-91 68\-\-69\-\-70\-\-71 \& | | | | \& 119\-118\-117\-116 107\-106\-105\-104 87\-\-86\-\-85\-\-84 75\-\-74\-\-73\-\-72 \& | | | | \& 112\-113\-114\-115 108\-109\-110\-111 80\-\-81\-\-82\-\-83 76\-\-77\-\-78\-\-79 \& \& 15\-\-14\-\-13\-\-12 19\-\-18\-\-17\-\-16 47\-\-46\-\-45\-\-44 51\-\-50\-\-49\-\-48 \& | | | | \& 8\-\- 9\-\-10\-\-11 20\-\-21\-\-22\-\-23 40\-\-41\-\-42\-\-43 52\-\-53\-\-54\-\-55 \& | | | | \& 7\-\- 6\-\- 5\-\- 4 27\-\-26\-\-25\-\-24 39\-\-38\-\-37\-\-36 59\-\-58\-\-57\-\-56 \& | | | | \& 0\-\- 1\-\- 2\-\- 3 28\-\-29\-\-30\-\-31\-\-32\-\-33\-\-34\-\-35 60\-\-61\-\-62\-\-63 .Ve .SS "Apply Type" .IX Subsection "Apply Type" Option \f(CW\*(C`apply_type => $str\*(C'\fR controls how Gray codes are applied to N and X,Y. It can be one of .PP .Vb 6 \& "TsF" to Gray, split, from Gray (default) \& "Ts" to Gray, split \& "Fs" from Gray, split \& "FsT" from Gray, split, to Gray \& "sT" split, to Gray \& "sF" split, from Gray .Ve .PP \&\*(L"T\*(R" means integer-to-Gray, \*(L"F\*(R" means integer-from-Gray, and omitted means no transformation. For example the following is \*(L"Ts\*(R" which means N to Gray then split, leaving Gray coded values for X,Y. .PP .Vb 1 \& apply_type => "Ts" \& \& 7 | 51\-\-50 52\-\-53 44\-\-45 43\-\-42 \& | | | | | \& 6 | 48\-\-49 55\-\-54 47\-\-46 40\-\-41 \& | \& 5 | 60\-\-61 59\-\-58 35\-\-34 36\-\-37 ...\-66 \& | | | | | | \& 4 | 63\-\-62 56\-\-57 32\-\-33 39\-\-38 64\-\-65 \& | \& 3 | 12\-\-13 11\-\-10 19\-\-18 20\-\-21 \& | | | | | \& 2 | 15\-\-14 8\-\- 9 16\-\-17 23\-\-22 \& | \& 1 | 3\-\- 2 4\-\- 5 28\-\-29 27\-\-26 \& | | | | | \& Y=0 | 0\-\- 1 7\-\- 6 31\-\-30 24\-\-25 \& | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& X=0 1 2 3 4 5 6 7 .Ve .PP This \*(L"Ts\*(R" is quite attractive because a step from N to N+1 changes just one bit in X or Y alternately, giving 2\-D single-bit changes. For example N=19 at X=4 followed by N=20 at X=6 is a single bit change in X. .PP N=0,2,8,10,etc on the leading diagonal X=Y is numbers using only digits 0,2 in base 4. N=0,3,15,12,etc on the Y axis is numbers using only digits 0,3 in base 4, but in a Gray code order. .PP The \*(L"Fs\*(R", \*(L"FsT\*(R" and \*(L"sF\*(R" forms effectively treat the input N as a Gray code and convert from it to integers, either before or after split. For \*(L"Fs\*(R" the effect is little Z parts in various orientations. .PP .Vb 1 \& apply_type => "sF" \& \& 7 | 32\-\-33 37\-\-36 52\-\-53 49\-\-48 \& | / \e / \e \& 6 | 34\-\-35 39\-\-38 54\-\-55 51\-\-50 \& | \& 5 | 42\-\-43 47\-\-46 62\-\-63 59\-\-58 \& | \e / \e / \& 4 | 40\-\-41 45\-\-44 60\-\-61 57\-\-56 \& | \& 3 | 8\-\- 9 13\-\-12 28\-\-29 25\-\-24 \& | / \e / \e \& 2 | 10\-\-11 15\-\-14 30\-\-31 27\-\-26 \& | \& 1 | 2\-\- 3 7\-\- 6 22\-\-23 19\-\-18 \& | \e / \e / \& Y=0 | 0\-\- 1 5\-\- 4 20\-\-21 17\-\-16 \& | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& X=0 1 2 3 4 5 6 7 .Ve .SS "Gray Type" .IX Subsection "Gray Type" The \f(CW\*(C`gray_type\*(C'\fR option selects what type of Gray code is used. The choices are .PP .Vb 2 \& "reflected" increment to radix\-1 then decrement (default) \& "modular" cycle from radix\-1 back to 0 .Ve .PP For example in decimal, .PP .Vb 10 \& integer Gray Gray \& "reflected" "modular" \& \-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\- \& 0 0 0 \& 1 1 1 \& 2 2 2 \& ... ... ... \& 8 8 8 \& 9 9 9 \& 10 19 19 \& 11 18 10 \& 12 17 11 \& 13 16 12 \& 14 15 13 \& ... ... ... \& 17 12 16 \& 18 11 17 \& 19 10 18 .Ve .PP Notice on reaching \*(L"19\*(R" the reflected type runs the least significant digit downwards from 9 to 0, which is a reverse or reflection of the preceding 0 to 9 upwards. The modular form instead continues to increment that least significant digit, wrapping around from 9 to 0. .PP In binary the modular and reflected forms are the same (see \*(L"Equivalent Combinations\*(R" below). .PP There's various other systematic ways to make a Gray code changing a single digit successively. But many ways are implicitly based on a pre-determined fixed number of bits or digits, which doesn't suit an unlimited path as given here. .SS "Equivalent Combinations" .IX Subsection "Equivalent Combinations" Some option combinations are equivalent, .PP .Vb 4 \& condition equivalent \& \-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- \& radix=2 modular==reflected \& and TsF==Fs, Ts==FsT \& \& radix>2 odd reflected TsF==FsT, Ts==Fs, sT==sF \& because T==F \& \& radix>2 even reflected TsF==Fs, Ts==FsT .Ve .PP In radix=2 binary the \*(L"modular\*(R" and \*(L"reflected\*(R" Gray codes are the same because there's only digits 0 and 1 so going forward or backward is the same. .PP For odd radix and reflected Gray code, the \*(L"to Gray\*(R" and \*(L"from Gray\*(R" operations are the same. For example the following table is ternary radix=3. Notice how integer value 012 maps to Gray code 010, and in turn integer 010 maps to Gray code 012. All values are either pairs like that or unchanged like 021. .PP .Vb 11 \& integer Gray \& "reflected" (written in ternary) \& 000 000 \& 001 001 \& 002 002 \& 010 012 \& 011 011 \& 012 010 \& 020 020 \& 021 021 \& 022 022 .Ve .PP For even radix and reflected Gray code, \*(L"TsF\*(R" is equivalent to \*(L"Fs\*(R", and also \*(L"Ts\*(R" equivalent to \*(L"FsT\*(R". This arises from the way the reversing behaves when split across digits of two X,Y values. (In higher dimensions such as a split to 3\-D X,Y,Z it's not the same.) .PP The net effect for distinct paths is .PP .Vb 7 \& condition distinct combinations \& \-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& radix=2 four TsF==Fs, Ts==FsT, sT, sF \& radix>2 odd / three reflected TsF==FsT, Ts==Fs, sT==sF \& \e six modular TsF, Ts, Fs, FsT, sT, sF \& radix>2 even / four reflected TsF==Fs, Ts==FsT, sT, sF \& \e six modular TsF, Ts, Fs, FsT, sT, sF .Ve .SS "Peano Curve" .IX Subsection "Peano Curve" In \f(CW\*(C`radix => 3\*(C'\fR and other odd radices the \*(L"reflected\*(R" Gray type gives the Peano curve (see Math::PlanePath::PeanoCurve). The \*(L"reflected\*(R" encoding is equivalent to Peano's \*(L"xk\*(R" and \*(L"yk\*(R" complementing. .PP .Vb 1 \& radix => 3, gray_type => "reflected" \& \& | \& 53\-\-52\-\-51 38\-\-37\-\-36\-\-35\-\-34\-\-33 \& | | | \& 48\-\-49\-\-50 39\-\-40\-\-41 30\-\-31\-\-32 \& | | | \& 47\-\-46\-\-45\-\-44\-\-43\-\-42 29\-\-28\-\-27 \& | \& 6\-\- 7\-\- 8\-\- 9\-\-10\-\-11 24\-\-25\-\-26 \& | | | \& 5\-\- 4\-\- 3 14\-\-13\-\-12 23\-\-22\-\-21 \& | | | \& 0\-\- 1\-\- 2 15\-\-16\-\-17\-\-18\-\-19\-\-20 .Ve .SH "FUNCTIONS" .IX Header "FUNCTIONS" See \*(L"\s-1FUNCTIONS\*(R"\s0 in Math::PlanePath for the behaviour common to all path classes. .ie n .IP """$path = Math::PlanePath::GrayCode\->new ()""" 4 .el .IP "\f(CW$path = Math::PlanePath::GrayCode\->new ()\fR" 4 .IX Item "$path = Math::PlanePath::GrayCode->new ()" .PD 0 .ie n .IP """$path = Math::PlanePath::GrayCode\->new (radix => $r, apply_type => $str, gray_type => $str)""" 4 .el .IP "\f(CW$path = Math::PlanePath::GrayCode\->new (radix => $r, apply_type => $str, gray_type => $str)\fR" 4 .IX Item "$path = Math::PlanePath::GrayCode->new (radix => $r, apply_type => $str, gray_type => $str)" .PD Create and return a new path object. .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. Points begin at 0 and if \f(CW\*(C`$n < 0\*(C'\fR then the return is an empty list. .ie n .IP """$n = $path\->n_start ()""" 4 .el .IP "\f(CW$n = $path\->n_start ()\fR" 4 .IX Item "$n = $path->n_start ()" Return the first N on the path, which is 0. .SS "Level Methods" .IX Subsection "Level Methods" .ie n .IP """($n_lo, $n_hi) = $path\->level_to_n_range($level)""" 4 .el .IP "\f(CW($n_lo, $n_hi) = $path\->level_to_n_range($level)\fR" 4 .IX Item "($n_lo, $n_hi) = $path->level_to_n_range($level)" Return \f(CW\*(C`(0, $radix**(2*$level) \- 1)\*(C'\fR. .SH "FORMULAS" .IX Header "FORMULAS" .SS "Turn" .IX Subsection "Turn" The turns in the default binary TsF curve are either to the left +90 or a reverse 180. For example at N=2 the curve turns left, then at N=3 it reverses back 180 to go to N=4. The turn is given by the low zero bits of (N+1)/2, .PP .Vb 3 \& count_low_0_bits(floor((N+1)/2)) \& if even then turn 90 left \& if odd then turn 180 reverse .Ve .PP Or equivalently .PP .Vb 3 \& floor((N+1)/2) lowest non\-zero digit in base 4, \& 1 or 3 = turn 90 left \& 2 = turn 180 reverse .Ve .PP The 180 degree reversals are all horizontal. They occur because at those N the three N\-1,N,N+1 converted to Gray code have the same bits at odd positions and therefore the same Y coordinate. .PP See \*(L"N to Turn\*(R" in Math::PlanePath::KochCurve for similar turns based on low zero bits (but by +60 and \-120 degrees). .SH "OEIS" .IX Header "OEIS" This path is in Sloane's Online Encyclopedia of Integer Sequences in a few forms, .Sp .RS 4 (etc) .RE .PP .Vb 9 \& apply_type="TsF", radix=2 (the defaults) \& A039963 turn sequence, 1=+90 left, 0=180 reverse \& A035263 turn undoubled, at N=2n and N=2n+1 \& A065882 base4 lowest non\-zero, \& turn undoubled 1,3=left 2=180rev at N=2n,2n+1 \& A003159 (N+1)/2 of positions of Left turns, \& being n with even number of low 0 bits \& A036554 (N+1)/2 of positions of Right turns \& being n with odd number of low 0 bits .Ve .PP The turn sequence goes in pairs, so N=1 and N=2 left then N=3 and N=4 reverse. A039963 includes that repetition, A035263 is just one copy of each and so is the turn at each pair N=2k and N=2k+1. There's many sequences like A065882 which when taken mod2 equal the \*(L"count low 0\-bits odd/even\*(R" which is the same undoubled turn sequence. .PP .Vb 7 \& apply_type="sF", radix=2 \& A163233 N values by diagonals, same axis start \& A163234 inverse permutation \& A163235 N values by diagonals, opp axis start \& A163236 inverse permutation \& A163242 N sums along diagonals \& A163478 those sums divided by 3 \& \& A163237 N values by diagonals, same axis, flip digits 2,3 \& A163238 inverse permutation \& A163239 N values by diagonals, opp axis, flip digits 2,3 \& A163240 inverse permutation \& \& A099896 N values by PeanoCurve radix=2 order \& A100280 inverse permutation \& \& apply_type="FsT", radix=3, gray_type=modular \& A208665 N values on X=Y diagonal, base 9 digits 0,3,6 .Ve .PP Gray code conversions themselves (not directly offered by the PlanePath code here) are variously .PP .Vb 9 \& A003188 binary \& A014550 binary with values written in binary \& A006068 inverse, Gray\->integer \& A128173 ternary reflected (its own inverse) \& A105530 ternary modular \& A105529 inverse, Gray\->integer \& A003100 decimal reflected \& A174025 inverse, Gray\->integer \& A098488 decimal modular .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Math::PlanePath, Math::PlanePath::ZOrderCurve, Math::PlanePath::PeanoCurve, Math::PlanePath::CornerReplicate .SH "HOME PAGE" .IX Header "HOME PAGE" .SH "LICENSE" .IX Header "LICENSE" Copyright 2011, 2012, 2013, 2014 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 .