MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Hexagonal indexing?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67694] Re: Hexagonal indexing?
  • From: "ben" <benjamin.friedrich at gmail.com>
  • Date: Wed, 5 Jul 2006 04:17:10 -0400 (EDT)
  • References: <e8d12i$54k$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I would be interested in the answer myself.

Recently I programed a biased random walk on a hexagonal lattice,
facing similiar problems with indexing: if indices i and j cound
rows and columns in your hexagonal lattice, the formula for the
midpoint
of hexagon [i,j] has to use the parity of j.

Maybe the code below is of some help.

Bye
Ben

hexagon=Transpose@({Re[#],Im[#]})&@Table[Exp[2\[Pi] I i/6],{i,0,6}]
\!\({{1, 0}, {1\/2, \@3\/2}, {\(-\(1\/2\)\), \@3\/2}, {\(-1\),
      0}, {\(-\(1\/2\)\), \(-\(\@3\/2\)\)}, {1\/2, \(-\(\@3\/2\)\)},
{1, 0}}\)

(* Colors *)
White=RGBColor[1,1,1];
Black=RGBColor[0,0,0];
Red=RGBColor[1,0,0];
 Blue=RGBColor[0,0,1];
Green=RGBColor[0,1,0];

PutHexAt[x_,y_,col_]:=Graphics@{col,Polygon[hexagon+Table[{x,y},{i,7}]]};

imax=10; jmax=20;

hexlist0=Table[
      PutHexAt[3 i+1.5 Mod[j,2] ,Sqrt[3]/2
j,RGBColor[i/imax,i/imax,1]],{i,
        imax},{j,jmax}];

Show[hexlist0,AspectRatio\[Rule]1];

pos0={imax/2,jmax/2+1};
framelist={};

(* little program *)
pos=pos0;
hexlist=hexlist0;
iframe=0;
frames={};
While[pos[[1]]>1 && pos[[1]]<imax && pos[[2]]>1 && pos[[2]]<jmax,
  dice=Random[Integer,5];

  If[dice>3,nsteps=2,nsteps=1];
  Do[
    dir=Switch[Mod[pos[[2]],2],
        1,Switch[dice,
          0,{0,2},
          1,{1,1},
          2,{1,-1},
          3,{0,-2},
          4,{0,-1},
          5,{0,1}],
        0,Switch[dice,
          0,{0,2},
          1,{0,1},
          2,{0,-1},
          3,{0,-2},
          4,{-1,-1},
          5,{-1,1}]];

    (* mark old pos *)
    i=pos[[1]]; j=pos[[2]];
    hexlist[[i,j]]=PutHexAt[3 i+1.5 Mod[j,2] ,Sqrt[3]/2 j,Green];
    (* new pos *)
    pos=pos+dir;
    (* mark new pos *)
    i=pos[[1]]; j=pos[[2]];
    hexlist[[i,j]]=PutHexAt[3 i+1.5 Mod[j,2] ,Sqrt[3]/2 j,Red];

    AppendTo[frames,Show[hexlist,DisplayFunction\[Rule]Identity]],

    {dummy,nsteps}];
  ]
AppendTo[framelist,frames];
Show[frames,DisplayFunction\[Rule]$DisplayFunction];

AES schrieb:

> Partly out of a practical problem, partly out of curiousity, are there
> any standard conventions for "hexagonal indexing"? -- that is, for
> attaching a single index  k  or a double index  [m, n]  to the points in
> a planar hexagonal array so that one can conveniently do things like
>
> --FInd the 6 nearest neighbors {m', n'] to a point [n, m] ?
>
> --Find the coordinates of an arbitrary array point relative to an
> optimally chosen origin or center?
>
> --Find the distances between two array points [m', n] and [m", n"] ?
>
> --Find all the array points on the outer rim of a finite hexagonal array
> having 6 identical flat faces, or a finite hexagonal array maximally
> filling a spherical shell?
>
> --Efficiently convert the double indices (if used) to a single Mathematica
> style array index?
>
> I can surely work out some of these answers for myself, but I've never
> encountered a "standard method" for doing these, and wonder if there is
> an optimal or conventional approach?


  • Prev by Date: Re: Re: Re: Limit of an expression?
  • Next by Date: Re: Iterated Function System
  • Previous by thread: Re: random inside module
  • Next by thread: RE: Hexagonal indexing?