### 6 releases (breaking)

0.5.0 | Jun 17, 2023 |
---|---|

0.4.0 | Jan 19, 2021 |

0.3.0 | Oct 6, 2020 |

0.2.0 | Aug 6, 2016 |

0.1.1 | Aug 5, 2016 |

#**420** in Algorithms

**24,130** downloads per month

Used in **11** crates

**MIT**license

12KB

127 lines

# Lerp

Linear interpolation and iteration, automatically implemented over most float-compatible types.

Just need to know what's halfway between three and five?

`use` `lerp``::`Lerp`;`
`assert_eq!``(``3.``0``.``lerp``(``5.``0``,` `0.``5``)``,` `4.``0``)``;`

Want to iterate across some points in that range?

`//` bring the trait into scope
`use` `lerp``::`LerpIter`;`
`//` iterate and produce four items evenly spaced between 3.0 and 5.0
`//` note that the default, open iterator does not include both endpoints
`//` this makes chaining lerping iterators simpler
`let` items`:` `Vec``<``_``>` `=` `3.``0_``f64``.``lerp_iter``(``5.``0``,` `4``)``.``collect``(``)``;`
`assert_eq!``(``vec!``[``3.``0``,` `3.``5``,` `4.``0``,` `4.``5``]``,` items`)``;`
`//` closed iterators return both ends
`assert_eq!``(``vec!``[``3.``0``,` `5.``0``]``,` `3.``0``.``lerp_iter_closed``(``5.``0``,` `2``)``.``collect``::``<``Vec``<``_``>``>``(``)``)``;`

Of course, the real benefit is that it's derivation is broad enough that it also
covers types such as

. If you have an array-processing library,
and the arrays are `num ::`Complex

`<`T`>``T``:` `Add``<`Output = T`>` `+` `Mul``<`F`:` Float, Output = T`>`

, it'll just
work for them as well.## Deriving `Lerp`

`Lerp`

As well as working for individual float values, the crate also provides a derive
macro, available with the

feature, which will be able to generate an
implementation automatically.`derive`

This derive implementation will lerp each field of the struct independently
and assumes a generic implementation of Lerp over

types. If any
of the fields is generic only over one of the float values (f32, f64) that
can be specified by the `Float`

or `#``[``lerp``(``f32``)``]`

attributes respectively.`#``[``lerp``(``f64``)``]`

If you would like for the lerp implementation to ignore a field (or if it does
not derive lerp) you can use the

or `#``[``lerp``(``skip``)``]`

attributes
which will produce the value, untouched from the left value.`#``[``lerp``(``ignore``)``]`

Not all types are supported in this derive macro. See the github issue for discussion and more information.

`[``dependencies``]`
`lerp = { version = "0.4", features ``=` `[``"`derive`"``]` }

`use` `lerp``::`Lerp`;`
`#``[``derive``(``Lerp``,` PartialEq`,` Debug`)``]`
`struct` `Data` `{`
`a``:` `f64`,
`b``:` `f64`
`}`
`assert_eq!``(`
Data `{` a`:` `0.``0``,` b`:` `1.``0` `}``.``lerp``(`Data `{` a`:` `1.``0``,` b`:` `0.``0` `}``,` `0.``5``)``,`
Data `{` a`:` `0.``5``,` b`:` `0.``5` `}`
`)``;`

More derive examples can be seen in the tests

## Usage

`[``dependencies``]`
`lerp ``=` `"`0.4`"`

## Documentation

Auto-built from Travis: https://coriolinus.github.io/lerp-rs/

#### Dependencies

~95–365KB