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