# fig. a

## Variables

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

• `b` - The base speed we want to "vectorize"
• `x` - The displacement of our object from Link along the X-axis
• `y` - The displacement of our object from Link along the Y-axis

## 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.

Before looking at the functions we'll use to describe each sequence, let's define three more variables that will be constant:
• `j` - The larger magnitude of our two displacements; `j=max(|x|,|y|)`
• `k` - The smaller magnitude of our two displacements; `k=min(|x|,|y|)`
• `l` - All sequences we have will be of length `l=b+1`, unless `b=0` in which case they will be of length `l=257`.
And now we can define our sequences:
• `a` will describe our collection plate. It has the following rules:
• `a0=0`
• `an=(an-1+k) mod256` when `(an-1+k) mod256<j`
• `an=(an-1+k-j) mod256` when `(an-1+k) mod256≥j`
• `s` will describe our main output. It has the following rules:
• `s0=0`
• `sn=sn-1` when `(an-1+k) mod256<j`
• `sn=sn-1+1` when `(an-1+k) mod256≥j`

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.

For our example, let's use the following inputs:
• `b=5`
• `x=20`
• `y=-30`
In addition to our input variables, we have 3 constants from these inputs:
• `l=5`, as `b` was nonzero; `b≠0`
• `j=30`, as `y` has a larger magnitude; `y>x`
• `k=20`, as `x` has a smaller magnitude; `x<y`

## Working out

Step one is easy enough:
• `a=0, …`
• `s=0, …`

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+`.

Now, our sequences look like this:
• `a=0, 20, …`
• `s=0, 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`.

Now we look at this set of rules to determine our final output:
• `vx` is the velocity along the X-axis
• `vy` is the velocity along the Y-axis
The values of `vx` and `vy` are determined by the original displacements:
• When `|x|≥|y|`:
• `vx=b×(x/|x|)`
• `vy=al×(y/|y|)`
• When `|x|<|y|`:
• `vx=al×(x/|x|)`
• `vy=b×(y/|y|)`
Our final values are thus:
• `vx=5`
• `vy=−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.