
Interpolate a sequence of values with Easing or Stepping Given Data Points
Source:R/seq_data_nl.R
interpolate.RdThis function generates a sequence of values based on a specified easing or stepping function. It supports linear, polynomial, exponential, and other smooth transitions, as well as stepped transitions.
Arguments
- data
Numeric vector, matrix, data frame, or list. The input data to be used for generating the sequence.
- type
Character string specifying the type of sequence. Supported types include:
"linear": Linear interpolation."quad": Quadratic easing."cubic": Cubic easing."quart": Quartic easing."quint": Quintic easing."exp": Exponential easing."circle": Circular easing."back": Back easing with overshoot."elastic": Elastic easing with oscillation."sine": Sine wave easing."bounce": Bouncing easing."step": Stepped transitions.
Defaults to
"linear".- step_count
Integer specifying the number of steps for the
"step"type. Must be between 1 and the length ofdata. Defaults toNULL.- ease
Character string specifying the direction of easing. Supported values are:
"in": Easing starts slow and accelerates."out": Easing starts fast and decelerates."in_out": Easing combines both behaviors.
Applicable only for non-linear types. Defaults to
NULL.
Value
A numeric vector containing the generated sequence.
For
"linear", a smoothly interpolated sequence is returned.For
"step", a sequence with distinct steps is generated.For other easing types, the sequence follows the specified smooth transition curve.
Details
The seq_data function calculates a sequence of values based on the specified type and ease.
The data input is used to determine the range (minimum and maximum) of the sequence to then be interpolated, and the resulting
sequence is normalized between 0 and 1 before applying the specified easing or stepping function.
For "step" type, the number of steps can be controlled using step_count. The ease parameter has no effect
when type is "linear" or "step".
Note
This function supports various easing functions commonly used in animations and graphics, as well as stepped sequences for discrete transitions. Invalid or unsupported inputs will result in informative error messages or warnings.
Examples
# Generate a linear sequence
interpolate(1:10, type = "linear")
#> [1] 1 2 3 4 5 6 7 8 9 10
# Generate a quadratic easing sequence
interpolate(rnorm(100,14,5), type = "quad", ease = "in_out")
#> Values:
#> 0.9383283 0.9438051 0.9602353 0.987619 1.025956 1.075247 1.135491 1.206689 1.28884 1.381944 1.486002 1.601014 1.726979 1.863897 2.011769 2.170595 2.340374 2.521106 2.712792 2.915431 3.129024 3.35357 3.58907 3.835523 4.09293 4.36129 4.640604 4.930871 5.232092 5.544266 5.867393 6.201475 6.546509 6.902497 7.269439 7.647334 8.036182 8.435984 8.846739 9.268448 9.701111 10.14473 10.5993 11.06482 11.5413 12.02872 12.52711 13.03644 13.55674 14.08798 14.62744 15.15868 15.67897 16.18831 16.68669 17.17412 17.6506 18.11612 18.57069 19.01431 19.44697 19.86868 20.27943 20.67923 21.06808 21.44598 21.81292 22.16891 22.51394 22.84802 23.17115 23.48332 23.78455 24.07481 24.35413 24.62249 24.87989 25.12635 25.36185 25.58639 25.79999 26.00262 26.19431 26.37504 26.54482 26.70365 26.85152 26.98844 27.1144 27.22941 27.33347 27.42658 27.50873 27.57993 27.64017 27.68946 27.7278 27.75518 27.77161 27.77709
#>
# Generate a stepped sequence with 5 steps
interpolate(rpois(100,3), type = "step", step_count = 5)
#> [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4
#> [19] 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 2.8 2.8 2.8 2.8 2.8 2.8
#> [37] 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 2.8 4.2 4.2 4.2 4.2
#> [55] 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 4.2 5.6 5.6
#> [73] 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6 5.6
#> [91] 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0