## Stroke end

The position modeling algorithm will lag behind the raw input by some distance. This algorithm iterates the previous dynamical system a few additional time using the raw input position as the anchor to allow a `catch up` of the stroke (though this prediction is only given by `predict`, so is not part of the `results` and becomes obsolete on the next input).

Algorithm :Stroke end
Input:

- Final anchor position p [ end ] = ( x [ end ] , y [ end ] ) p\left\lbrack \text{end} \right\rbrack = \left( x\left\lbrack \text{end} \right\rbrack,y\left\lbrack \text{end} \right\rbrack \right) (From the original input stream)

- final tip state q f [ end ] = ( p f [ end ] = ( x f [ end ] , y f [ end ] ) , v f [ end ] , a f [ end ] ) q_{f}\left\lbrack \text{end} \right\rbrack = \left( p_{f}\left\lbrack \text{end} \right\rbrack = \left( x_{f}\left\lbrack \text{end} \right\rbrack,y_{f}\left\lbrack \text{end} \right\rbrack \right),v_{f}\left\lbrack \text{end} \right\rbrack,a_{f}\left\lbrack \text{end} \right\rbrack \right) (returned from the physical modeling from the last section, ⋅ f \cdot_{f} signifies that we are looking at the filtered output)

- K max K_{\text{max}} max number of iterations (`sampling_end_of_stroke_max_iterations`)

- Δ target \Delta_{\text{target}} the target time delay between stroke (1/`sampling_min_output_rate`)

- d stop d_{\text{stop}} stopping distance (`sampling_end_of_stroke_stopping_distance`)

- k spring k_{\text{spring}} and k drag k_{\text{drag}} the modeling coefficients

initialize the vector q o q_{o} with q 0 [ 0 ] = ( p f [ end ] ⏟ p o [ 0 ] , v f [ end ] ⏟ v o [ 0 ] , a f [ end ] ⏟ a 0 [ 0 ] ) q_{0}\lbrack 0\rbrack = \left. \left( \underset{p_{o}\lbrack 0\rbrack}{\underbrace{p_{f}\left\lbrack \text{end} \right\rbrack}},\underset{v_{o}\lbrack 0\rbrack}{\underbrace{v_{f}\left\lbrack \text{end} \right\rbrack}},\underset{a_{0}\lbrack 0\rbrack}{\underbrace{a_{f}\left\lbrack \text{end} \right\rbrack}} \right) \right.
initialize Δ t = Δ target \Delta t = \Delta_{\text{target}}
for 1 ≤ k ≤ K max 1 \leq k \leq K_{\text{max}}
- calculate the next candidate a c = p [ end ] − p o [ end ] k spring − k drag v 0 [ end ] v c = v o [ 0 ] + Δ t a c p c = p o [ 0 ] + Δ v c \begin{aligned} a_{c} & = \frac{p\left\lbrack \text{end} \right\rbrack - p_{o}\left\lbrack \text{end} \right\rbrack}{k_{\text{spring}}} - k_{\text{drag }}v_{0}\left\lbrack \text{end} \right\rbrack \\ v_{c} & = v_{o}\lbrack 0\rbrack + \Delta ta_{c} \\ p_{c} & = p_{o}\lbrack 0\rbrack + \Delta v_{c} \end{aligned}

- if ∥ p c − p [ end ] ∥ < d stop \left. \parallel{p_{c} - p\left\lbrack \text{end} \right\rbrack} \right.\parallel < d_{\text{stop}} (further iterations won’t be able to catch up and won’t move closer to the anchor, we stop here), - return q 0 q_{0}
- endif

- if ⟨ p c − p o [ end ] , p [ end ] − p o [ end ] ⟩ < ∥ p c − p o [ end ] ∥ \langle p_{c} - p_{o}\left\lbrack \text{end} \right\rbrack,p\left\lbrack \text{end} \right\rbrack - p_{o}\left\lbrack \text{end} \right\rbrack\rangle < \left. \parallel{p_{c} - p_{o}\left\lbrack \text{end} \right\rbrack} \right.\parallel (we’ve overshot the anchor, we retry with a smaller step)

  - Δ t ← Δ t 2 \Delta t \leftarrow \frac{\Delta t}{2} ,

  - continue (this candidate will be discarded, try again with a smaller time step instead),

- else
- q 0 [ e n d + 1 ] = ( p c , v c , q c ) q_{0}\lbrack end\ +1\rbrack = \left( p_{c},v_{c},q_{c} \right) (We append the result to the end of the q 0 q_{0} vector)
- endif

- if ∥ p c − p [ end ] ∥ < d stop \left. \parallel{p_{c} - p\left\lbrack \text{end} \right\rbrack} \right.\parallel < d_{\text{stop}} ,

  - return (We are within tolerance of the anchor, we stop iterating),

- endif,

Output : { q o [ k ] = ( s o [ k ] , v o [ k ] , a o [ k ] ) , 0 ≤ k ≤ n ( ≤ K max − 1 ) } \left\{ q_{o}\lbrack k\rbrack = \left( s_{o}\lbrack k\rbrack,v_{o}\lbrack k\rbrack,a_{o}\lbrack k\rbrack \right),0 \leq k \leq n\left( \leq K_{\text{max }} - 1 \right) \right\}