NAME
Math::PlanePath::GrayCode  Gray code coordinates
SYNOPSIS
use Math::PlanePath::GrayCode;
my $path = Math::PlanePath::GrayCode>new;
my ($x, $y) = $path>n_to_xy (123);
DESCRIPTION
This path is a mapping of N to X,Y using Gray codes.
7  6362 5756 3938 3332
    
6  6061 5859 3637 3435

5  5150 5352 4342 4544
    
4  4849 5455 4041 4647

3  1514 98 2322 1716
    
2  1213 1011 2021 1819

1  32 54 2726 2928
    
Y=0  01 67 2425 3031

+
X=0 1 2 3 4 5 6 7
The default is the form by Faloutsos which is an X,Y split in binary reflected Gray code.
Christos Faloutsos, "Gray Codes for Partial Match and Range Queries", IEEE Transactions on Software Engineering (TSE), volume 14, number 10, October 1988, pages 13811393. DOI 10.1109/32.6184
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.
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.
Radix
The default is binary. Option radix => $r
can select another radix. This radix is used for both the Gray code and the digit splitting. For example radix => 4
,
radix => 4

127126125124 9998979695949392 67666564
   
120121122123 100101102103 88899091 68697071
   
119118117116 107106105104 87868584 75747372
   
112113114115 108109110111 80818283 76777879
15141312 19181716 47464544 51504948
   
8 91011 20212223 40414243 52535455
   
7 6 5 4 27262524 39383736 59585756
   
0 1 2 3 2829303132333435 60616263
Apply Type
Option apply_type => $str
controls how Gray codes are applied to N and X,Y. It can be one of
"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
"T" means integertoGray, "F" means integerfromGray, and omitted means no transformation. For example the following is "Ts" which means N to Gray then split, leaving Gray coded values for X,Y.
apply_type => "Ts"
7  5150 5253 4445 4342
    
6  4849 5554 4746 4041

5  6061 5958 3534 3637 ...66
     
4  6362 5657 3233 3938 6465

3  1213 1110 1918 2021
    
2  1514 8 9 1617 2322

1  3 2 4 5 2829 2726
    
Y=0  0 1 7 6 3130 2425

+
X=0 1 2 3 4 5 6 7
This "Ts" is quite attractive because a step from N to N+1 changes just one bit in X or Y alternately, giving 2D singlebit changes. For example N=19 at X=4 followed by N=20 at X=6 is a single bit change in X.
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.
The "Fs", "FsT" and "sF" forms effectively treat the input N as a Gray code and convert from it to integers, either before or after split. For "Fs" the effect is little Z parts in various orientations.
apply_type => "sF"
7  3233 3736 5253 4948
 / \ / \
6  3435 3938 5455 5150

5  4243 4746 6263 5958
 \ / \ /
4  4041 4544 6061 5756

3  8 9 1312 2829 2524
 / \ / \
2  1011 1514 3031 2726

1  2 3 7 6 2223 1918
 \ / \ /
Y=0  0 1 5 4 2021 1716

+
X=0 1 2 3 4 5 6 7
Gray Type
The gray_type
option selects the type of Gray code. The choices are
"reflected" increment to radix1 then decrement (default)
"modular" increment to radix1 then cycle back to 0
For example in decimal,
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
Notice on reaching "19" the reflected type runs the least significant digit back down 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.
In binary, the modular and reflected forms are the same (see "Equivalent Combinations" below).
There's various other systematic ways to make a Gray code changing a single digit successively. But many ways are implicitly based on a predetermined fixed number of bits or digits, which doesn't suit an unlimited path like here.
Equivalent Combinations
Some option combinations are equivalent,
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
In radix=2 binary, the "modular" and "reflected" Gray codes are the same because there's only digits 0 and 1 so going forward or backward is the same.
For odd radix and reflected Gray code, the "to Gray" and "from Gray" 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.
integer Gray
"reflected" (written in ternary)
000 000
001 001
002 002
010 012
011 011
012 010
020 020
021 021
022 022
For even radix and reflected Gray code, "TsF" is equivalent to "Fs", and also "Ts" equivalent to "FsT". 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 3D X,Y,Z it's not the same.)
The net effect for distinct paths is
condition distinct combinations
 
radix=2 four TsF==Fs, Ts==FsT, sT, sF
radix>2 odd / three reflected TsF==FsT, Ts==Fs, sT==sF
\ six modular TsF, Ts, Fs, FsT, sT, sF
radix>2 even / four reflected TsF==Fs, Ts==FsT, sT, sF
\ six modular TsF, Ts, Fs, FsT, sT, sF
Peano Curve
In radix => 3
and other odd radices, the "reflected" Gray type gives the Peano curve (see Math::PlanePath::PeanoCurve). This is since the "reflected" encoding is equivalent to Peano's "xk" and "yk" complementing.
radix => 3, gray_type => "reflected"

535251 383736353433
  
484950 394041 303132
  
474645444342 292827

6 7 8 91011 242526
  
5 4 3 141312 232221
  
0 1 2 151617181920
FUNCTIONS
See "FUNCTIONS" in Math::PlanePath for the behaviour common to all path classes.
$path = Math::PlanePath::GrayCode>new ()
$path = Math::PlanePath::GrayCode>new (radix => $r, apply_type => $str, gray_type => $str)

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. $n = $path>n_start ()

Return the first N on the path, which is 0.
Level Methods
FORMULAS
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,
count_low_0_bits(floor((N+1)/2))
if even then turn 90 left
if odd then turn 180 reverse
Or equivalently
floor((N+1)/2) lowest nonzero digit in base 4,
1 or 3 = turn 90 left
2 = turn 180 reverse
The 180 degree reversals are all horizontal. They occur because at those N the three N1,N,N+1 converted to Gray code have the same bits at odd positions and therefore the same Y coordinate.
See "N to Turn" in Math::PlanePath::KochCurve for similar turns based on low zero bits (but by +60 and 120 degrees).
OEIS
This path is in Sloane's Online Encyclopedia of Integer Sequences in a few forms,
http://oeis.org/A163233 (etc)
apply_type="TsF" (="Fs"), radix=2 (the defaults)
A059905 X xor Y
A039963 turn sequence, 1=+90 left, 0=180 reverse
A035263 turn undoubled, at N=2n and N=2n+1
A065882 base4 lowest nonzero,
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
TsF 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 "count low 0bits odd/even" which is the same undoubled turn sequence.
apply_type="Ts", radix=2
A309952 X coordinate (XOR bit pairs)
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
Gray code conversions themselves (not directly offered by the PlanePath code here) are variously
A003188 binary
A014550 binary written in binary
A055975 increments
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
A226134 inverse, Gray>integer
SEE ALSO
Math::PlanePath, Math::PlanePath::ZOrderCurve, Math::PlanePath::PeanoCurve, Math::PlanePath::CornerReplicate
HOME PAGE
http://user42.tuxfamily.org/mathplanepath/index.html
LICENSE
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde
This file is part of MathPlanePath.
MathPlanePath 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.
MathPlanePath 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 MathPlanePath. If not, see <http://www.gnu.org/licenses/>.