fig. a

Variables

We need 3 input variables, so let's define and assign them:

Functions

The best way to think about this is multiple sequences. At least to me. Perhaps there's some obscure high-level branch of mathematics that can deal with this in more universally understandable terms, but we're going to think of it as a series of numbers. Multiple series of numbers.

Okay, so formalizing it didn't make it too much more understandable, but now we have what we need to work through an actual problem step by step.

Working out

We did it! Let's keep going.

We'll do each step by finding the next element in a. For the first term and beyond, we need to use the functions defined above. For our example, we have 0+20≡20 mod256 and 20<30. So a1=(0+20) mod256=20. That inequality is used to contruct a rule for both sequences, so we can also find the next element in s. s1=s0=0+.

Let's do the next 4 steps, but do them faster:

  1. [a1+k mod256]=[20+20 mod256]=40 and 40≥30, so we use the second function for the next term of both sequences.
    • a2=[a1+k-j mod256]=[20+20-30 mod256]=10
    • s2=s1+1=0+1=1
      Sequences:
    • a=0, 20, 10, …
    • s=0, 0, 1, …
  2. [a2+k mod256]=[10+20 mod256]=30 and 30≥30, so we use the second function for the next term of both sequences.
    • a3=[a2+k-j mod256]=[10+20-30 mod256]=0
    • s3=s2+1=1+1=2
      Sequences:
    • a=0, 20, 10, 0, …
    • s=0, 0, 1, 2, …
  3. [a3+k mod256]=[0+20 mod256]=0 and 0<30, so we use the first function for the next term of both sequences.
    • a4=[a3+k mod256]=[0+20 mod256]=20
    • s4=s3=2
      Sequences:
    • a=0, 20, 10, 0, 20, …
    • s=0, 0, 1, 2, 2, …
  4. [a4+k mod256]=[20+20 mod256]=40 and 40≥30, so we use the second function for the next term of both sequences.
    • a5=[a4+k-j mod256]=[20+20-30 mod256]=10
    • s5=s4+1=2+1=3
      Sequences:
    • a=0, 20, 10, 0, 20, 10
    • s=0, 0, 1, 2, 2, 3

Phew. We're done. What we care the most about is the final term of sequence s, which, in this case, is 3.

In other words, the axis that had the larger displacement (the Y-axis when both are equal) receives the base speed b as the magnitude of its velocity, with the sign matching the displacement along the axis. The smaller displacement (the X-axis when both are equal) receives the final value calculated from the loop, again, matching the sign of the displacement.

Our example problem used a small input for b, otherwise the algorithm would have been more difficult and tedious to follow. As such, the resulting output being small is not surprising. Perhaps this example is more enlightening as to why b=0 results in the behavior that it does. Working out of such an example is left as an exercise to the reader.