# LinearTransform and VectorSum

> LinearTransform is Manim's ApplyMatrix on a LinearTransformationScene.

Page: https://fframes-kit.pages.dev/docs/math/linear-transform-and-vector-sum/
Cargo feature: `math` (`fframes-kit = { version = "..", features = ["math"] }`)

`LinearTransform` is Manim's `ApplyMatrix` on a `LinearTransformationScene`. It moves a grid
and the two basis vectors from the identity to `matrix` over `run_time`, over the
original grid drawn faintly, and writes the matrix at `matrix_at` with the `Matrix`
component. `VectorSum` adds vectors tip to tail: each one moves from `origin` to the tip
of the one before, then the resultant draws with `resultant_enter`. `apply(m, p)` and
`towards(m, p)` are the matrix product and the interpolation from the identity they use.
Gallery scenes: `LinearTransform`, `VectorSum`.

Gallery clip of `LinearTransform`: https://fframes-kit.pages.dev/media/scenes/LinearTransform-9f8944ca.mp4

Gallery clip of `VectorSum`: https://fframes-kit.pages.dev/media/scenes/VectorSum-469bbb6f.mp4

```rust
use fframes_kit::prelude::*;
use fframes_kit::kits::math::apply;

fn figure<'a>(ui: &mut Ui<'a, '_, '_>) -> Svgr<'a> {
    let shear = ui.show(LinearTransform {
        matrix: [[1.0, 1.0], [0.0, 1.0]],
        delay: 1.0,
        ..ui.defaults()
    });
    let sum = ui.show(VectorSum {
        vectors: vec![(3.0, 1.0), (1.0, 2.0)],
        labels: vec!["u".into(), "v".into()],
        resultant_label: "u + v".into(),
        delay: 3.5,
        resultant_enter: Some(Create { delay: 5.0, ..Create::default() }.into()),
        resultant_label_enter: Some(FadeIn { delay: 5.5, ..FadeIn::default() }),
        ..ui.defaults()
    });
    svgr!(<g>{shear}{sum}</g>)
}

assert_eq!(apply([[1.0, 1.0], [0.0, 1.0]], (2.0, 3.0)), (5.0, 3.0));
```

## API

Components are plain structs. Set the fields you need and fill the rest with `..ui.defaults()`: the defaults below come from the current theme (`theme.` values are fields of `Theme`).

### LinearTransform

A grid of lines and the two basis vectors moved by `matrix` (Manim's `LinearTransformationScene` with `ApplyMatrix`): from the identity to the matrix over `run_time` seconds after `delay`, over the untransformed grid drawn faintly. The grid has a line at every integer of `x_range` and `y_range`, in units of `ui.space` around `center`, and the moved grid is clipped to its box; `key` tells the clip paths of two transforms apart. With `show_matrix` the matrix is written at `matrix_at` (pixels).

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `matrix` | `Mat2` | `[[1.0, 1.0], [0.0, 1.0]]` | The 2x2 matrix to apply, row by row; its columns are where the basis vectors `i` and `j` end up. |
| `x_range` | `Range` | `Range::new(-4.0, 4.0, 1.0)` | The grid's x extent in units of `ui.space` from `center`, with a vertical line at every tick. |
| `y_range` | `Range` | `Range::new(-3.0, 3.0, 1.0)` | The grid's y extent in units of `ui.space` from `center`, with a horizontal line at every tick. |
| `center` | `(f64, f64)` | `(0.0, 0.0)` | Where the grid's origin sits, in units of `ui.space` (y up). |
| `delay` | `f32` | `0.0` | Seconds after the container's start before the transformation begins. |
| `run_time` | `f32` | `2.0` | Seconds the grid takes to move from the identity to `matrix`. |
| `rate_func` | `RateFunc` | `RateFunc::default()` | Easing of the movement over its run time. |
| `line_color` | `Color` | `theme.colors.accent` | Colour of the moving grid lines; the untransformed grid is drawn faintly in the muted colour. |
| `stroke_width` | `f32` | `theme.stroke.thin` | Grid line width in pixels. |
| `i_color` | `Color` | `theme.colors.highlight` | Colour of the basis vector `i` (to `(1, 0)`). |
| `j_color` | `Color` | `theme.colors.caption` | Colour of the basis vector `j` (to `(0, 1)`). |
| `show_matrix` | `bool` | `true` | Writes `matrix` at `matrix_at` when `true`. |
| `matrix_at` | `(f32, f32)` | `(1600.0, 200.0)` | Where the written matrix sits, in pixels of the canvas. |
| `enter` | `Option<EnterAnim>` | `None` | How the grids and the vectors appear. |
| `matrix_enter` | `Option<FadeIn>` | `Some(FadeIn::themed(theme))` | Fade in of the written matrix's entries; `None` shows them from the start. |
| `key` | `Option<&str>` | `None` | Names the clip path when two transforms share a frame; `None` for the default. |

Feature `math` · [Source](https://github.com/khanhthanhdev/fframes-kit/blob/main/fframes-kit/src/kits/math/linear.rs#L46)

### VectorSum

Vectors added tip to tail (Manim's vector addition): every vector starts at `origin`, then moves over `run_time` seconds after `delay` to the tip of the one before, and the resultant from `origin` to the last tip draws with `resultant_enter` (a `Create` delayed past the move). Vectors are in units of `ui.space`; `labels` are written at the middle of the vectors, on their left, and `resultant_label` on the side of the resultant away from them.

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `vectors` | `Vec<(f64, f64)>` | `Vec::new()` | The vectors `(dx, dy)` in units of `ui.space` (y up), added in order tip to tail. |
| `origin` | `(f64, f64)` | `(0.0, 0.0)` | Where the first vector starts and the resultant begins, in units of `ui.space`. |
| `colors` | `Vec<Color>` | `Vec::new()` | Colours of the vectors in turn; empty for the theme's accent and highlight. |
| `labels` | `Vec<Cow<str>>` | `Vec::new()` | Text for each vector in turn, written at its middle; a vector without one has none. |
| `delay` | `f32` | `0.0` | Seconds after the container's start before the vectors begin to move into place. |
| `run_time` | `f32` | `1.5` | Seconds the vectors take to move to the tip of the one before. |
| `rate_func` | `RateFunc` | `RateFunc::default()` | Easing of the move over its run time. |
| `resultant` | `bool` | `true` | Draws the sum from `origin` to the last tip when `true`. |
| `resultant_color` | `Color` | `theme.colors.title` | Colour of the resultant. |
| `resultant_label` | `Cow<str>` | `Cow::Borrowed("")` | Text written beside the resultant; empty for none. |
| `enter` | `Option<EnterAnim>` | `None` | How each vector appears; `None` shows them from the start. |
| `resultant_enter` | `Option<EnterAnim>` | `None` | How the resultant appears; `None` shows it from the start. |
| `labels_enter` | `Option<FadeIn>` | `None` | Fade in of the vector labels; `None` for none. |
| `resultant_label_enter` | `Option<FadeIn>` | `None` | The resultant's label also fades with this, to wait for the resultant. |

Methods:

- `fn total(&self) -> (f64, f64)`: The sum of the vectors.

Feature `math` · [Source](https://github.com/khanhthanhdev/fframes-kit/blob/main/fframes-kit/src/kits/math/linear.rs#L251)
