MathGroup Archive 2005

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

Search the Archive

Re: Re: spiral cipher


zak wrote:
> Thanks dh at metrohm.ch for your letter
> i mean the following
> moves={0, 5, 2, 7, 4, 6}
> so the movement of the point will trace across the following points:
> dat = {{0, 0}, {5, 0}, {5, 2}, {5 - 7, 2}, {5 - 7, 2 - 4}, {5 - 7 + 6, 2 - 4}}
> 
> Show[Graphics[Line[dat]]];
> 
> will draw the spiral.
> 
> the problem how to construct (dat List) from (moves List)
> 
> regards
> [...]

There are various ways to do this. One is to use the basic four 
directions in a repeating list of the length of your input moves. Note 
that I remove the initial 0 in your moves list because it is not clear 
to me what it is doing there.

moves = {5,2,7,4,6};
directions = {{1,0},{0,1},{-1,0},{0,-1}};
increments = moves * PadRight[{}, Length[moves], directions];

Now use FoldList to iteratively add the increments, beginning at the origin.

In[11]:= dat = FoldList[Plus, {0,0}, increments]
Out[11]= {{0, 0}, {5, 0}, {5, 2}, {-2, 2}, {-2, -2}, {4, -2}}

Note that this is reasonably efficient. It will handle a million moves 
in under 2 seconds on my 1400 GHz machine.

moves = Table[Random[Integer,10],{10^6}];

In[14]:= Timing[
   increments = moves * PadRight[{}, Length[moves], directions];
   dat = FoldList[Plus, {0,0}, increments];
   ]
Out[14]= {1.76 Second, Null}


Daniel Lichtblau
Wolfram Research



  • Prev by Date: Re: Sorting complex points
  • Next by Date: Re: Version differences in Integrate with Min[] in the integrand
  • Previous by thread: Re: spiral cipher
  • Next by thread: Computation speeds: numerical vs symbolic