Skip to main content

waft_material/
arc.rs

1//! Flutter counterpart: material_ui's `arc.dart`.
2
3use std::f64::consts::PI;
4
5use waft_animation::Animatable;
6use waft_embedder::{Offset, Rect, lerp_double};
7use waft_foundation::App;
8use waft_widgets::RectTweenObject;
9
10// How close the begin and end points must be to an axis to be considered
11// vertical or horizontal.
12const K_ON_AXIS_DELTA: f64 = 2.0;
13
14/// Dart's `num.sign`: `-1.0`, `0.0` or `1.0`. `f64::signum` answers `±1.0` for `±0.0`.
15fn sign(value: f64) -> f64 {
16    if value > 0.0 {
17        1.0
18    } else if value < 0.0 {
19        -1.0
20    } else {
21        value
22    }
23}
24
25/// A tween that interpolates an [`Offset`] along a circular arc.
26///
27/// This class specializes the interpolation of `Tween<Offset>` so that instead
28/// of a straight line, the intermediate points follow the arc of a circle in a
29/// manner consistent with Material Design principles.
30///
31/// The arc's radius is related to the bounding box that contains the
32/// [`begin`](Self::begin) and [`end`](Self::end) points. If the bounding box is
33/// taller than it is wide, then the center of the circle will be horizontally
34/// aligned with the end point. Otherwise the center of the circle will be aligned
35/// with the begin point. The arc's sweep is always less than or equal to 90
36/// degrees.
37///
38/// The arc is worked out when [`begin`](Self::begin) and [`end`](Self::end) are
39/// both set, so the geometry getters and [`lerp`](Self::lerp) read it. A tween
40/// is a plain value: [`Animatable::animate`] takes a copy, so a later
41/// [`set_begin`](Self::set_begin) is not seen by an animation already driven.
42///
43/// See also:
44///
45///  * `Tween`, for a discussion on how to use interpolation objects.
46///  * [`MaterialRectArcTween`], which extends this concept to interpolating [`Rect`]s.
47#[derive(Clone, Debug)]
48pub struct MaterialPointArcTween {
49    begin: Option<Offset>,
50    end: Option<Offset>,
51    center: Option<Offset>,
52    radius: Option<f64>,
53    begin_angle: Option<f64>,
54    end_angle: Option<f64>,
55}
56
57impl MaterialPointArcTween {
58    /// Creates a tween for animating [`Offset`]s along a circular arc.
59    ///
60    /// The [`begin`](Self::begin) and [`end`](Self::end) properties must be
61    /// non-null before the tween is first used, but the arguments can be null if
62    /// the values are going to be filled in later.
63    pub fn new(begin: Option<Offset>, end: Option<Offset>) -> MaterialPointArcTween {
64        let mut tween = MaterialPointArcTween {
65            begin,
66            end,
67            center: None,
68            radius: None,
69            begin_angle: None,
70            end_angle: None,
71        };
72        tween.initialize();
73        tween
74    }
75
76    fn initialize(&mut self) {
77        let (Some(begin), Some(end)) = (self.begin, self.end) else {
78            return;
79        };
80
81        // An explanation with a diagram can be found at https://docs.google.com/document/d/1kF7vhX_RpQCIjQYT6lZdLFs4Dl6hHxoXNkTQfzyHNlw/
82        let delta = end - begin;
83        let delta_x = delta.dx().abs();
84        let delta_y = delta.dy().abs();
85        let distance_from_a_to_b = delta.distance();
86        let c = Offset::new(end.dx(), begin.dy());
87
88        let sweep_angle = |radius: f64| 2.0 * (distance_from_a_to_b / (2.0 * radius)).asin();
89
90        if delta_x > K_ON_AXIS_DELTA && delta_y > K_ON_AXIS_DELTA {
91            if delta_x < delta_y {
92                let radius =
93                    distance_from_a_to_b * distance_from_a_to_b / (c - begin).distance() / 2.0;
94                self.radius = Some(radius);
95                self.center = Some(Offset::new(
96                    end.dx() + radius * sign(begin.dx() - end.dx()),
97                    end.dy(),
98                ));
99                if begin.dx() < end.dx() {
100                    self.begin_angle = Some(sweep_angle(radius) * sign(begin.dy() - end.dy()));
101                    self.end_angle = Some(0.0);
102                } else {
103                    self.begin_angle = Some(PI + sweep_angle(radius) * sign(end.dy() - begin.dy()));
104                    self.end_angle = Some(PI);
105                }
106            } else {
107                let radius =
108                    distance_from_a_to_b * distance_from_a_to_b / (c - end).distance() / 2.0;
109                self.radius = Some(radius);
110                self.center = Some(Offset::new(
111                    begin.dx(),
112                    begin.dy() + sign(end.dy() - begin.dy()) * radius,
113                ));
114                if begin.dy() < end.dy() {
115                    let begin_angle = -PI / 2.0;
116                    self.begin_angle = Some(begin_angle);
117                    self.end_angle =
118                        Some(begin_angle + sweep_angle(radius) * sign(end.dx() - begin.dx()));
119                } else {
120                    let begin_angle = PI / 2.0;
121                    self.begin_angle = Some(begin_angle);
122                    self.end_angle =
123                        Some(begin_angle + sweep_angle(radius) * sign(begin.dx() - end.dx()));
124                }
125            }
126        } else {
127            self.begin_angle = None;
128            self.end_angle = None;
129        }
130    }
131
132    /// The value this variable has at the beginning of the animation.
133    pub fn begin(&self) -> Option<Offset> {
134        self.begin
135    }
136
137    /// Dart's `begin` setter.
138    pub fn set_begin(&mut self, value: Option<Offset>) {
139        if value != self.begin {
140            self.begin = value;
141            self.initialize();
142        }
143    }
144
145    /// The value this variable has at the end of the animation.
146    pub fn end(&self) -> Option<Offset> {
147        self.end
148    }
149
150    /// Dart's `end` setter.
151    pub fn set_end(&mut self, value: Option<Offset>) {
152        if value != self.end {
153            self.end = value;
154            self.initialize();
155        }
156    }
157
158    /// The center of the circular arc, null if [`begin`](Self::begin) and
159    /// [`end`](Self::end) are horizontally or vertically aligned, or if either is
160    /// null.
161    pub fn center(&self) -> Option<Offset> {
162        if self.begin.is_none() || self.end.is_none() {
163            return None;
164        }
165        self.center
166    }
167
168    /// The radius of the circular arc, null if [`begin`](Self::begin) and
169    /// [`end`](Self::end) are horizontally or vertically aligned, or if either is
170    /// null.
171    pub fn radius(&self) -> Option<f64> {
172        if self.begin.is_none() || self.end.is_none() {
173            return None;
174        }
175        self.radius
176    }
177
178    /// The beginning of the arc's sweep in radians, measured from the positive x
179    /// axis. Positive angles turn clockwise.
180    ///
181    /// This will be null if [`begin`](Self::begin) and [`end`](Self::end) are
182    /// horizontally or vertically aligned, or if either is null.
183    pub fn begin_angle(&self) -> Option<f64> {
184        if self.begin.is_none() || self.end.is_none() {
185            return None;
186        }
187        self.begin_angle
188    }
189
190    /// The end of the arc's sweep in radians, measured from the positive x axis.
191    /// Positive angles turn clockwise.
192    ///
193    /// This will be null if [`begin`](Self::begin) and [`end`](Self::end) are
194    /// horizontally or vertically aligned, or if either is null.
195    pub fn end_angle(&self) -> Option<f64> {
196        if self.begin.is_none() || self.end.is_none() {
197            return None;
198        }
199        self.begin_angle
200    }
201
202    /// Returns the value this variable has at the given animation clock value.
203    pub fn lerp(&self, t: f64) -> Offset {
204        let begin = self
205            .begin
206            .expect("MaterialPointArcTween.begin must be set before use");
207        let end = self
208            .end
209            .expect("MaterialPointArcTween.end must be set before use");
210        if t == 0.0 {
211            return begin;
212        }
213        if t == 1.0 {
214            return end;
215        }
216        let (Some(begin_angle), Some(end_angle)) = (self.begin_angle, self.end_angle) else {
217            return Offset::lerp(Some(begin), Some(end), t).unwrap();
218        };
219        let radius = self.radius.unwrap();
220        let angle = lerp_double(Some(begin_angle), Some(end_angle), t).unwrap();
221        let x = angle.cos() * radius;
222        let y = angle.sin() * radius;
223        self.center.unwrap() + Offset::new(x, y)
224    }
225}
226
227impl Animatable<Offset> for MaterialPointArcTween {
228    fn transform(&self, _app: &App, t: f64) -> Offset {
229        self.lerp(t)
230    }
231}
232
233#[derive(Clone, Copy)]
234enum CornerId {
235    TopLeft,
236    TopRight,
237    BottomLeft,
238    BottomRight,
239}
240
241#[derive(Clone, Copy)]
242struct Diagonal {
243    begin_id: CornerId,
244    end_id: CornerId,
245}
246
247const ALL_DIAGONALS: [Diagonal; 4] = [
248    Diagonal {
249        begin_id: CornerId::TopLeft,
250        end_id: CornerId::BottomRight,
251    },
252    Diagonal {
253        begin_id: CornerId::BottomRight,
254        end_id: CornerId::TopLeft,
255    },
256    Diagonal {
257        begin_id: CornerId::TopRight,
258        end_id: CornerId::BottomLeft,
259    },
260    Diagonal {
261        begin_id: CornerId::BottomLeft,
262        end_id: CornerId::TopRight,
263    },
264];
265
266// Select the element for which the key function returns the maximum value.
267fn max_by<T: Copy>(input: &[T], key_func: impl Fn(T) -> f64) -> T {
268    let mut max_value = None;
269    let mut max_key: Option<f64> = None;
270    for &value in input {
271        let key = key_func(value);
272        if max_key.is_none() || key > max_key.unwrap() {
273            max_value = Some(value);
274            max_key = Some(key);
275        }
276    }
277    max_value.expect("max_by needs at least one element")
278}
279
280fn corner_for(rect: Rect, id: CornerId) -> Offset {
281    match id {
282        CornerId::TopLeft => rect.top_left(),
283        CornerId::TopRight => rect.top_right(),
284        CornerId::BottomLeft => rect.bottom_left(),
285        CornerId::BottomRight => rect.bottom_right(),
286    }
287}
288
289/// A tween that interpolates a [`Rect`] by having its opposite corners follow
290/// circular arcs.
291///
292/// This class specializes the interpolation of `Tween<Rect>` so that instead of
293/// growing or shrinking linearly, opposite corners of the rectangle follow arcs
294/// in a manner consistent with Material Design principles.
295///
296/// Specifically, the rectangle corners whose diagonals are closest to the overall
297/// direction of the animation follow arcs defined with [`MaterialPointArcTween`].
298///
299/// The arcs are worked out when [`begin`](Self::begin) and [`end`](Self::end) are
300/// both set. A tween is a plain value, held as `Rc<dyn RectTweenObject>` where a
301/// hero wants one.
302///
303/// See also:
304///
305///  * [`MaterialRectCenterArcTween`], which interpolates a rect along a circular
306///    arc between the begin and end [`Rect`]'s centers.
307///  * `Tween`, for a discussion on how to use interpolation objects.
308///  * [`MaterialPointArcTween`], the analog for [`Offset`] interpolation.
309///  * `RectTween`, which does a linear rectangle interpolation.
310///  * `Hero::create_rect_tween`, which can be used to specify the tween that defines
311///    a hero's path.
312#[derive(Clone, Debug)]
313pub struct MaterialRectArcTween {
314    begin: Option<Rect>,
315    end: Option<Rect>,
316    begin_arc: Option<MaterialPointArcTween>,
317    end_arc: Option<MaterialPointArcTween>,
318}
319
320impl MaterialRectArcTween {
321    /// Creates a tween for animating [`Rect`]s along a circular arc.
322    ///
323    /// The [`begin`](Self::begin) and [`end`](Self::end) properties must be
324    /// non-null before the tween is first used, but the arguments can be null if
325    /// the values are going to be filled in later.
326    pub fn new(begin: Option<Rect>, end: Option<Rect>) -> MaterialRectArcTween {
327        let mut tween = MaterialRectArcTween {
328            begin,
329            end,
330            begin_arc: None,
331            end_arc: None,
332        };
333        tween.initialize();
334        tween
335    }
336
337    fn initialize(&mut self) {
338        let (Some(begin), Some(end)) = (self.begin, self.end) else {
339            self.begin_arc = None;
340            self.end_arc = None;
341            return;
342        };
343        let centers_vector = end.center() - begin.center();
344        let diagonal = max_by(&ALL_DIAGONALS, |d| {
345            Self::diagonal_support(begin, centers_vector, d)
346        });
347        self.begin_arc = Some(MaterialPointArcTween::new(
348            Some(corner_for(begin, diagonal.begin_id)),
349            Some(corner_for(end, diagonal.begin_id)),
350        ));
351        self.end_arc = Some(MaterialPointArcTween::new(
352            Some(corner_for(begin, diagonal.end_id)),
353            Some(corner_for(end, diagonal.end_id)),
354        ));
355    }
356
357    fn diagonal_support(begin: Rect, centers_vector: Offset, diagonal: Diagonal) -> f64 {
358        let delta = corner_for(begin, diagonal.end_id) - corner_for(begin, diagonal.begin_id);
359        let length = delta.distance();
360        centers_vector.dx() * delta.dx() / length + centers_vector.dy() * delta.dy() / length
361    }
362
363    /// The value this variable has at the beginning of the animation.
364    pub fn begin(&self) -> Option<Rect> {
365        self.begin
366    }
367
368    /// Dart's `begin` setter.
369    pub fn set_begin(&mut self, value: Option<Rect>) {
370        if value != self.begin {
371            self.begin = value;
372            self.initialize();
373        }
374    }
375
376    /// The value this variable has at the end of the animation.
377    pub fn end(&self) -> Option<Rect> {
378        self.end
379    }
380
381    /// Dart's `end` setter.
382    pub fn set_end(&mut self, value: Option<Rect>) {
383        if value != self.end {
384            self.end = value;
385            self.initialize();
386        }
387    }
388
389    /// The path of the corresponding [`begin`](Self::begin), [`end`](Self::end)
390    /// rectangle corners that lead the animation.
391    pub fn begin_arc(&self) -> Option<&MaterialPointArcTween> {
392        self.begin?;
393        Some(
394            self.begin_arc
395                .as_ref()
396                .expect("MaterialRectArcTween.end must be set before use"),
397        )
398    }
399
400    /// The path of the corresponding [`begin`](Self::begin), [`end`](Self::end)
401    /// rectangle corners that trail the animation.
402    pub fn end_arc(&self) -> Option<&MaterialPointArcTween> {
403        self.end?;
404        Some(
405            self.end_arc
406                .as_ref()
407                .expect("MaterialRectArcTween.begin must be set before use"),
408        )
409    }
410
411    /// Returns the value this variable has at the given animation clock value.
412    pub fn lerp(&self, t: f64) -> Rect {
413        let begin = self
414            .begin
415            .expect("MaterialRectArcTween.begin must be set before use");
416        let end = self
417            .end
418            .expect("MaterialRectArcTween.end must be set before use");
419        if t == 0.0 {
420            return begin;
421        }
422        if t == 1.0 {
423            return end;
424        }
425        Rect::from_points(
426            self.begin_arc.as_ref().unwrap().lerp(t),
427            self.end_arc.as_ref().unwrap().lerp(t),
428        )
429    }
430}
431
432impl RectTweenObject for MaterialRectArcTween {
433    fn begin(&self, _app: &App) -> Option<Rect> {
434        self.begin
435    }
436
437    fn end(&self, _app: &App) -> Option<Rect> {
438        self.end
439    }
440
441    fn lerp(&self, _app: &App, t: f64) -> Option<Rect> {
442        Some(MaterialRectArcTween::lerp(self, t))
443    }
444}
445
446impl Animatable<Option<Rect>> for MaterialRectArcTween {
447    fn transform(&self, app: &App, t: f64) -> Option<Rect> {
448        RectTweenObject::lerp(self, app, t)
449    }
450}
451
452/// A tween that interpolates a [`Rect`] by moving it along a circular arc from
453/// [`begin`](Self::begin)'s [`Rect::center`] to [`end`](Self::end)'s
454/// [`Rect::center`] while interpolating the rectangle's width and height.
455///
456/// The arc that defines that center of the interpolated rectangle as it morphs
457/// from [`begin`](Self::begin) to [`end`](Self::end) is a [`MaterialPointArcTween`].
458///
459/// See also:
460///
461///  * [`MaterialRectArcTween`], A tween that interpolates a [`Rect`] by having
462///    its opposite corners follow circular arcs.
463///  * `Tween`, for a discussion on how to use interpolation objects.
464///  * [`MaterialPointArcTween`], the analog for [`Offset`] interpolation.
465///  * `RectTween`, which does a linear rectangle interpolation.
466///  * `Hero::create_rect_tween`, which can be used to specify the tween that defines
467///    a hero's path.
468#[derive(Clone, Debug)]
469pub struct MaterialRectCenterArcTween {
470    begin: Option<Rect>,
471    end: Option<Rect>,
472    center_arc: Option<MaterialPointArcTween>,
473}
474
475impl MaterialRectCenterArcTween {
476    /// Creates a tween for animating [`Rect`]s along a circular arc.
477    ///
478    /// The [`begin`](Self::begin) and [`end`](Self::end) properties must be
479    /// non-null before the tween is first used, but the arguments can be null if
480    /// the values are going to be filled in later.
481    pub fn new(begin: Option<Rect>, end: Option<Rect>) -> MaterialRectCenterArcTween {
482        let mut tween = MaterialRectCenterArcTween {
483            begin,
484            end,
485            center_arc: None,
486        };
487        tween.initialize();
488        tween
489    }
490
491    fn initialize(&mut self) {
492        let (Some(begin), Some(end)) = (self.begin, self.end) else {
493            self.center_arc = None;
494            return;
495        };
496        self.center_arc = Some(MaterialPointArcTween::new(
497            Some(begin.center()),
498            Some(end.center()),
499        ));
500    }
501
502    /// The value this variable has at the beginning of the animation.
503    pub fn begin(&self) -> Option<Rect> {
504        self.begin
505    }
506
507    /// Dart's `begin` setter.
508    pub fn set_begin(&mut self, value: Option<Rect>) {
509        if value != self.begin {
510            self.begin = value;
511            self.initialize();
512        }
513    }
514
515    /// The value this variable has at the end of the animation.
516    pub fn end(&self) -> Option<Rect> {
517        self.end
518    }
519
520    /// Dart's `end` setter.
521    pub fn set_end(&mut self, value: Option<Rect>) {
522        if value != self.end {
523            self.end = value;
524            self.initialize();
525        }
526    }
527
528    /// If [`begin`](Self::begin) and [`end`](Self::end) are non-null, returns a
529    /// tween that interpolates along a circular arc between
530    /// [`begin`](Self::begin)'s [`Rect::center`] and [`end`](Self::end)'s
531    /// [`Rect::center`].
532    pub fn center_arc(&self) -> Option<&MaterialPointArcTween> {
533        if self.begin.is_none() || self.end.is_none() {
534            return None;
535        }
536        self.center_arc.as_ref()
537    }
538
539    /// Returns the value this variable has at the given animation clock value.
540    pub fn lerp(&self, t: f64) -> Rect {
541        let begin = self
542            .begin
543            .expect("MaterialRectCenterArcTween.begin must be set before use");
544        let end = self
545            .end
546            .expect("MaterialRectCenterArcTween.end must be set before use");
547        if t == 0.0 {
548            return begin;
549        }
550        if t == 1.0 {
551            return end;
552        }
553        let center = self.center_arc.as_ref().unwrap().lerp(t);
554        let width = lerp_double(Some(begin.width()), Some(end.width()), t).unwrap();
555        let height = lerp_double(Some(begin.height()), Some(end.height()), t).unwrap();
556        Rect::from_ltwh(
557            center.dx() - width / 2.0,
558            center.dy() - height / 2.0,
559            width,
560            height,
561        )
562    }
563}
564
565impl RectTweenObject for MaterialRectCenterArcTween {
566    fn begin(&self, _app: &App) -> Option<Rect> {
567        self.begin
568    }
569
570    fn end(&self, _app: &App) -> Option<Rect> {
571        self.end
572    }
573
574    fn lerp(&self, _app: &App, t: f64) -> Option<Rect> {
575        Some(MaterialRectCenterArcTween::lerp(self, t))
576    }
577}
578
579impl Animatable<Option<Rect>> for MaterialRectCenterArcTween {
580    fn transform(&self, app: &App, t: f64) -> Option<Rect> {
581        RectTweenObject::lerp(self, app, t)
582    }
583}
584
585#[cfg(test)]
586mod tests {
587    use super::*;
588
589    fn assert_offset_within(actual: Offset, from: Offset, distance: f64) {
590        assert!(
591            (actual - from).distance() <= distance,
592            "{actual:?} is not within {distance} of {from:?}"
593        );
594    }
595
596    fn same_rect(a: Rect, b: Rect) -> bool {
597        (a.left - b.left).abs() < 2.0
598            && (a.top - b.top).abs() < 2.0
599            && (a.right - b.right).abs() < 2.0
600            && (a.bottom - b.bottom).abs() < 2.0
601    }
602
603    #[test]
604    fn point_arc_tween_gives_equal_geometry_for_equal_ends() {
605        let a = MaterialPointArcTween::new(Some(Offset::ZERO), Some(Offset::new(0.0, 10.0)));
606        let b = MaterialPointArcTween::new(Some(Offset::ZERO), Some(Offset::new(0.0, 10.0)));
607        assert_eq!(format!("{a:?}"), format!("{b:?}"));
608    }
609
610    #[test]
611    fn rect_arc_tween_gives_equal_geometry_for_equal_ends() {
612        let make = || {
613            MaterialRectArcTween::new(
614                Some(Rect::from_ltwh(0.0, 0.0, 10.0, 10.0)),
615                Some(Rect::from_ltwh(0.0, 10.0, 10.0, 10.0)),
616            )
617        };
618        assert_eq!(format!("{:?}", make()), format!("{:?}", make()));
619    }
620
621    #[test]
622    fn on_axis_point_arc_tween_moves_in_a_straight_line() {
623        let tween = MaterialPointArcTween::new(Some(Offset::ZERO), Some(Offset::new(0.0, 10.0)));
624        assert_eq!(tween.lerp(0.5), Offset::new(0.0, 5.0));
625
626        let tween = MaterialPointArcTween::new(Some(Offset::ZERO), Some(Offset::new(10.0, 0.0)));
627        assert_eq!(tween.lerp(0.5), Offset::new(5.0, 0.0));
628    }
629
630    #[test]
631    fn on_axis_rect_arc_tween_moves_in_a_straight_line() {
632        let tween = MaterialRectArcTween::new(
633            Some(Rect::from_ltwh(0.0, 0.0, 10.0, 10.0)),
634            Some(Rect::from_ltwh(0.0, 10.0, 10.0, 10.0)),
635        );
636        assert_eq!(tween.lerp(0.5), Rect::from_ltwh(0.0, 5.0, 10.0, 10.0));
637
638        let tween = MaterialRectArcTween::new(
639            Some(Rect::from_ltwh(0.0, 0.0, 10.0, 10.0)),
640            Some(Rect::from_ltwh(10.0, 0.0, 10.0, 10.0)),
641        );
642        assert_eq!(tween.lerp(0.5), Rect::from_ltwh(5.0, 0.0, 10.0, 10.0));
643    }
644
645    #[test]
646    fn point_arc_tween_follows_a_circular_arc() {
647        let begin = Offset::new(180.0, 110.0);
648        let end = Offset::new(37.0, 250.0);
649
650        let tween = MaterialPointArcTween::new(Some(begin), Some(end));
651        assert_eq!(tween.lerp(0.0), begin);
652        assert_offset_within(tween.lerp(0.25), Offset::new(126.0, 120.0), 2.0);
653        assert_offset_within(tween.lerp(0.75), Offset::new(48.0, 196.0), 2.0);
654        assert_eq!(tween.lerp(1.0), end);
655
656        let tween = MaterialPointArcTween::new(Some(end), Some(begin));
657        assert_eq!(tween.lerp(0.0), end);
658        assert_offset_within(tween.lerp(0.25), Offset::new(91.0, 239.0), 2.0);
659        assert_offset_within(tween.lerp(0.75), Offset::new(168.3, 163.8), 2.0);
660        assert_eq!(tween.lerp(1.0), begin);
661    }
662
663    #[test]
664    fn rect_arc_tween_moves_opposite_corners_along_arcs() {
665        let begin = Rect::from_ltrb(180.0, 100.0, 330.0, 200.0);
666        let end = Rect::from_ltrb(32.0, 275.0, 132.0, 425.0);
667
668        let tween = MaterialRectArcTween::new(Some(begin), Some(end));
669        assert_eq!(tween.lerp(0.0), begin);
670        assert!(same_rect(
671            tween.lerp(0.25),
672            Rect::from_ltrb(120.0, 113.0, 259.0, 237.0)
673        ));
674        assert!(same_rect(
675            tween.lerp(0.75),
676            Rect::from_ltrb(42.3, 206.5, 153.5, 354.7)
677        ));
678        assert_eq!(tween.lerp(1.0), end);
679
680        let tween = MaterialRectArcTween::new(Some(end), Some(begin));
681        assert_eq!(tween.lerp(0.0), end);
682        assert!(same_rect(
683            tween.lerp(0.25),
684            Rect::from_ltrb(92.0, 262.0, 203.0, 388.0)
685        ));
686        assert!(same_rect(
687            tween.lerp(0.75),
688            Rect::from_ltrb(169.7, 168.5, 308.5, 270.3)
689        ));
690        assert_eq!(tween.lerp(1.0), begin);
691    }
692
693    #[test]
694    #[should_panic(expected = "MaterialRectArcTween.end must be set before use")]
695    fn rect_arc_tween_begin_arc_fails_once_its_end_is_cleared() {
696        let mut tween = MaterialRectArcTween::new(
697            Some(Rect::from_ltrb(180.0, 100.0, 330.0, 200.0)),
698            Some(Rect::from_ltrb(32.0, 275.0, 132.0, 425.0)),
699        );
700        assert!(tween.begin_arc().is_some());
701        tween.set_end(None);
702        tween.begin_arc();
703    }
704}