forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.dart
1649 lines (1454 loc) · 59.7 KB
/
geometry.dart
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of dart.ui;
/// Base class for [Size] and [Offset], which are both ways to describe
/// a distance as a two-dimensional axis-aligned vector.
abstract class OffsetBase {
/// Abstract const constructor. This constructor enables subclasses to provide
/// const constructors so that they can be used in const expressions.
///
/// The first argument sets the horizontal component, and the second the
/// vertical component.
const OffsetBase(this._dx, this._dy);
final double _dx;
final double _dy;
/// Returns true if either component is [double.infinity], and false if both
/// are finite (or negative infinity, or NaN).
///
/// This is different than comparing for equality with an instance that has
/// _both_ components set to [double.infinity].
///
/// See also:
///
/// * [isFinite], which is true if both components are finite (and not NaN).
bool get isInfinite => _dx >= double.infinity || _dy >= double.infinity;
/// Whether both components are finite (neither infinite nor NaN).
///
/// See also:
///
/// * [isInfinite], which returns true if either component is equal to
/// positive infinity.
bool get isFinite => _dx.isFinite && _dy.isFinite;
/// Less-than operator. Compares an [Offset] or [Size] to another [Offset] or
/// [Size], and returns true if both the horizontal and vertical values of the
/// left-hand-side operand are smaller than the horizontal and vertical values
/// of the right-hand-side operand respectively. Returns false otherwise.
///
/// This is a partial ordering. It is possible for two values to be neither
/// less, nor greater than, nor equal to, another.
bool operator <(OffsetBase other) => _dx < other._dx && _dy < other._dy;
/// Less-than-or-equal-to operator. Compares an [Offset] or [Size] to another
/// [Offset] or [Size], and returns true if both the horizontal and vertical
/// values of the left-hand-side operand are smaller than or equal to the
/// horizontal and vertical values of the right-hand-side operand
/// respectively. Returns false otherwise.
///
/// This is a partial ordering. It is possible for two values to be neither
/// less, nor greater than, nor equal to, another.
bool operator <=(OffsetBase other) => _dx <= other._dx && _dy <= other._dy;
/// Greater-than operator. Compares an [Offset] or [Size] to another [Offset]
/// or [Size], and returns true if both the horizontal and vertical values of
/// the left-hand-side operand are bigger than the horizontal and vertical
/// values of the right-hand-side operand respectively. Returns false
/// otherwise.
///
/// This is a partial ordering. It is possible for two values to be neither
/// less, nor greater than, nor equal to, another.
bool operator >(OffsetBase other) => _dx > other._dx && _dy > other._dy;
/// Greater-than-or-equal-to operator. Compares an [Offset] or [Size] to
/// another [Offset] or [Size], and returns true if both the horizontal and
/// vertical values of the left-hand-side operand are bigger than or equal to
/// the horizontal and vertical values of the right-hand-side operand
/// respectively. Returns false otherwise.
///
/// This is a partial ordering. It is possible for two values to be neither
/// less, nor greater than, nor equal to, another.
bool operator >=(OffsetBase other) => _dx > other._dx && _dy >= other._dy;
/// Equality operator. Compares an [Offset] or [Size] to another [Offset] or
/// [Size], and returns true if the horizontal and vertical values of the
/// left-hand-side operand are equal to the horizontal and vertical values of
/// the right-hand-side operand respectively. Returns false otherwise.
@override
bool operator ==(dynamic other) {
if (other is! OffsetBase)
return false;
final OffsetBase typedOther = other;
return _dx == typedOther._dx &&
_dy == typedOther._dy;
}
@override
int get hashCode => hashValues(_dx, _dy);
@override
String toString() => '$runtimeType(${_dx?.toStringAsFixed(1)}, ${_dy?.toStringAsFixed(1)})';
}
/// An immutable 2D floating-point offset.
///
/// Generally speaking, Offsets can be interpreted in two ways:
///
/// 1. As representing a point in Cartesian space a specified distance from a
/// separately-maintained origin. For example, the top-left position of
/// children in the [RenderBox] protocol is typically represented as an
/// [Offset] from the top left of the parent box.
///
/// 2. As a vector that can be applied to coordinates. For example, when
/// painting a [RenderObject], the parent is passed an [Offset] from the
/// screen's origin which it can add to the offsets of its children to find
/// the [Offset] from the screen's origin to each of the children.
///
/// Because a particular [Offset] can be interpreted as one sense at one time
/// then as the other sense at a later time, the same class is used for both
/// senses.
///
/// See also:
///
/// * [Size], which represents a vector describing the size of a rectangle.
class Offset extends OffsetBase {
/// Creates an offset. The first argument sets [dx], the horizontal component,
/// and the second sets [dy], the vertical component.
const Offset(double dx, double dy) : super(dx, dy);
/// The x component of the offset.
///
/// The y component is given by [dy].
double get dx => _dx;
/// The y component of the offset.
///
/// The x component is given by [dx].
double get dy => _dy;
/// The magnitude of the offset.
///
/// If you need this value to compare it to another [Offset]'s distance,
/// consider using [distanceSquared] instead, since it is cheaper to compute.
double get distance => math.sqrt(_dx * _dx + _dy * _dy);
/// The square of the magnitude of the offset.
///
/// This is cheaper than computing the [distance] itself.
double get distanceSquared => _dx * _dx + _dy * _dy;
/// The angle of this offset as radians clockwise from the positive x-axis, in
/// the range -[pi] to [pi], assuming positive values of the x-axis go to the
/// left and positive values of the y-axis go down.
///
/// Zero means that [dy] is zero and [dx] is zero or positive.
///
/// Values from zero to [pi]/2 indicate positive values of [dx] and [dy], the
/// bottom-right quadrant.
///
/// Values from [pi]/2 to [pi] indicate negative values of [dx] and positive
/// values of [dy], the bottom-left quadrant.
///
/// Values from zero to -[pi]/2 indicate positive values of [dx] and negative
/// values of [dy], the top-right quadrant.
///
/// Values from -[pi]/2 to -[pi] indicate negative values of [dx] and [dy],
/// the top-left quadrant.
///
/// When [dy] is zero and [dx] is negative, the [direction] is [pi].
///
/// When [dx] is zero, [direction] is [pi]/2 if [dy] is positive and -[pi]/2
/// if [dy] is negative.
///
/// See also:
///
/// * [distance], to compute the magnitude of the vector.
/// * [Canvas.rotate], which uses the same convention for its angle.
double get direction => math.atan2(dy, dx);
/// An offset with zero magnitude.
///
/// This can be used to represent the origin of a coordinate space.
static const Offset zero = const Offset(0.0, 0.0);
/// An offset with infinite x and y components.
///
/// See also:
///
/// * [isInfinite], which checks whether either component is infinite.
/// * [isFinite], which checks whether both components are finite.
// This is included for completeness, because [Size.infinite] exists.
static const Offset infinite = const Offset(double.infinity, double.infinity);
/// Returns a new offset with the x component scaled by `scaleX` and the y
/// component scaled by `scaleY`.
///
/// If the two scale arguments are the same, consider using the `*` operator
/// instead:
///
/// ```dart
/// Offset a = const Offset(10.0, 10.0);
/// Offset b = a * 2.0; // same as: a.scale(2.0, 2.0)
/// ```
///
/// If the two arguments are -1, consider using the unary `-` operator
/// instead:
///
/// ```dart
/// Offset a = const Offset(10.0, 10.0);
/// Offset b = -a; // same as: a.scale(-1.0, -1.0)
/// ```
Offset scale(double scaleX, double scaleY) => new Offset(dx * scaleX, dy * scaleY);
/// Returns a new offset with translateX added to the x component and
/// translateY added to the y component.
///
/// If the arguments come from another [Offset], consider using the `+` or `-`
/// operators instead:
///
/// ```dart
/// Offset a = const Offset(10.0, 10.0);
/// Offset b = const Offset(10.0, 10.0);
/// Offset c = a + b; // same as: a.translate(b.dx, b.dy)
/// Offset d = a - b; // same as: a.translate(-b.dx, -b.dy)
/// ```
Offset translate(double translateX, double translateY) => new Offset(dx + translateX, dy + translateY);
/// Unary negation operator.
///
/// Returns an offset with the coordinates negated.
///
/// If the [Offset] represents an arrow on a plane, this operator returns the
/// same arrow but pointing in the reverse direction.
Offset operator -() => new Offset(-dx, -dy);
/// Binary subtraction operator.
///
/// Returns an offset whose [dx] value is the left-hand-side operand's [dx]
/// minus the right-hand-side operand's [dx] and whose [dy] value is the
/// left-hand-side operand's [dy] minus the right-hand-side operand's [dy].
///
/// See also [translate].
Offset operator -(Offset other) => new Offset(dx - other.dx, dy - other.dy);
/// Binary addition operator.
///
/// Returns an offset whose [dx] value is the sum of the [dx] values of the
/// two operands, and whose [dy] value is the sum of the [dy] values of the
/// two operands.
///
/// See also [translate].
Offset operator +(Offset other) => new Offset(dx + other.dx, dy + other.dy);
/// Multiplication operator.
///
/// Returns an offset whose coordinates are the coordinates of the
/// left-hand-side operand (an Offset) multiplied by the scalar
/// right-hand-side operand (a double).
///
/// See also [scale].
Offset operator *(double operand) => new Offset(dx * operand, dy * operand);
/// Division operator.
///
/// Returns an offset whose coordinates are the coordinates of the
/// left-hand-side operand (an Offset) divided by the scalar right-hand-side
/// operand (a double).
///
/// See also [scale].
Offset operator /(double operand) => new Offset(dx / operand, dy / operand);
/// Integer (truncating) division operator.
///
/// Returns an offset whose coordinates are the coordinates of the
/// left-hand-side operand (an Offset) divided by the scalar right-hand-side
/// operand (a double), rounded towards zero.
Offset operator ~/(double operand) => new Offset((dx ~/ operand).toDouble(), (dy ~/ operand).toDouble());
/// Modulo (remainder) operator.
///
/// Returns an offset whose coordinates are the remainder of dividing the
/// coordinates of the left-hand-side operand (an Offset) by the scalar
/// right-hand-side operand (a double).
Offset operator %(double operand) => new Offset(dx % operand, dy % operand);
/// Rectangle constructor operator.
///
/// Combines an [Offset] and a [Size] to form a [Rect] whose top-left
/// coordinate is the point given by adding this offset, the left-hand-side
/// operand, to the origin, and whose size is the right-hand-side operand.
///
/// ```dart
/// Rect myRect = Offset.zero & const Size(100.0, 100.0);
/// // same as: new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0)
/// ```
Rect operator &(Size other) => new Rect.fromLTWH(dx, dy, other.width, other.height);
/// Linearly interpolate between two offsets.
///
/// If either offset is null, this function interpolates from [Offset.zero].
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
/// that the interpolation has not started, returning `a` (or something
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
/// returning `b` (or something equivalent to `b`), and values in between
/// meaning that the interpolation is at the relevant point on the timeline
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
/// 1.0, so negative values and values greater than 1.0 are valid (and can
/// easily be generated by curves such as [Curves.elasticInOut]).
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static Offset lerp(Offset a, Offset b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return b * t;
if (b == null)
return a * (1.0 - t);
return new Offset(lerpDouble(a.dx, b.dx, t), lerpDouble(a.dy, b.dy, t));
}
/// Compares two Offsets for equality.
@override
bool operator ==(dynamic other) {
if (other is! Offset)
return false;
final Offset typedOther = other;
return _dx == typedOther._dx &&
_dy == typedOther._dy;
}
@override
int get hashCode => hashValues(_dx, _dy);
@override
String toString() => 'Offset(${dx?.toStringAsFixed(1)}, ${dy?.toStringAsFixed(1)})';
}
/// Holds a 2D floating-point size.
///
/// You can think of this as an [Offset] from the origin.
class Size extends OffsetBase {
/// Creates a [Size] with the given [width] and [height].
const Size(double width, double height) : super(width, height);
/// Creates an instance of [Size] that has the same values as another.
// Used by the rendering library's _DebugSize hack.
Size.copy(Size source) : super(source.width, source.height);
/// Creates a square [Size] whose [width] and [height] are the given dimension.
///
/// See also:
///
/// * [new Size.fromRadius], which is more convenient when the available size
/// is the radius of a circle.
const Size.square(double dimension) : super(dimension, dimension);
/// Creates a [Size] with the given [width] and an infinite [height].
const Size.fromWidth(double width) : super(width, double.infinity);
/// Creates a [Size] with the given [height] and an infinite [width].
const Size.fromHeight(double height) : super(double.infinity, height);
/// Creates a square [Size] whose [width] and [height] are twice the given
/// dimension.
///
/// This is a square that contains a circle with the given radius.
///
/// See also:
///
/// * [new Size.square], which creates a square with the given dimension.
const Size.fromRadius(double radius) : super(radius * 2.0, radius * 2.0);
/// The horizontal extent of this size.
double get width => _dx;
/// The vertical extent of this size.
double get height => _dy;
/// An empty size, one with a zero width and a zero height.
static const Size zero = const Size(0.0, 0.0);
/// A size whose [width] and [height] are infinite.
///
/// See also:
///
/// * [isInfinite], which checks whether either dimension is infinite.
/// * [isFinite], which checks whether both dimensions are finite.
static const Size infinite = const Size(double.infinity, double.infinity);
/// Whether this size encloses a non-zero area.
///
/// Negative areas are considered empty.
bool get isEmpty => width <= 0.0 || height <= 0.0;
/// Binary subtraction operator for [Size].
///
/// Subtracting a [Size] from a [Size] returns the [Offset] that describes how
/// much bigger the left-hand-side operand is than the right-hand-side
/// operand. Adding that resulting [Offset] to the [Size] that was the
/// right-hand-side operand would return a [Size] equal to the [Size] that was
/// the left-hand-side operand. (i.e. if `sizeA - sizeB -> offsetA`, then
/// `offsetA + sizeB -> sizeA`)
///
/// Subtracting an [Offset] from a [Size] returns the [Size] that is smaller than
/// the [Size] operand by the difference given by the [Offset] operand. In other
/// words, the returned [Size] has a [width] consisting of the [width] of the
/// left-hand-side operand minus the [Offset.dx] dimension of the
/// right-hand-side operand, and a [height] consisting of the [height] of the
/// left-hand-side operand minus the [Offset.dy] dimension of the
/// right-hand-side operand.
OffsetBase operator -(OffsetBase other) {
if (other is Size)
return new Offset(width - other.width, height - other.height);
if (other is Offset)
return new Size(width - other.dx, height - other.dy);
throw new ArgumentError(other);
}
/// Binary addition operator for adding an [Offset] to a [Size].
///
/// Returns a [Size] whose [width] is the sum of the [width] of the
/// left-hand-side operand, a [Size], and the [Offset.dx] dimension of the
/// right-hand-side operand, an [Offset], and whose [height] is the sum of the
/// [height] of the left-hand-side operand and the [Offset.dy] dimension of
/// the right-hand-side operand.
Size operator +(Offset other) => new Size(width + other.dx, height + other.dy);
/// Multiplication operator.
///
/// Returns a [Size] whose dimensions are the dimensions of the left-hand-side
/// operand (a [Size]) multiplied by the scalar right-hand-side operand (a
/// [double]).
Size operator *(double operand) => new Size(width * operand, height * operand);
/// Division operator.
///
/// Returns a [Size] whose dimensions are the dimensions of the left-hand-side
/// operand (a [Size]) divided by the scalar right-hand-side operand (a
/// [double]).
Size operator /(double operand) => new Size(width / operand, height / operand);
/// Integer (truncating) division operator.
///
/// Returns a [Size] whose dimensions are the dimensions of the left-hand-side
/// operand (a [Size]) divided by the scalar right-hand-side operand (a
/// [double]), rounded towards zero.
Size operator ~/(double operand) => new Size((width ~/ operand).toDouble(), (height ~/ operand).toDouble());
/// Modulo (remainder) operator.
///
/// Returns a [Size] whose dimensions are the remainder of dividing the
/// left-hand-side operand (a [Size]) by the scalar right-hand-side operand (a
/// [double]).
Size operator %(double operand) => new Size(width % operand, height % operand);
/// The lesser of the magnitudes of the [width] and the [height].
double get shortestSide => math.min(width.abs(), height.abs());
/// The greater of the magnitudes of the [width] and the [height].
double get longestSide => math.max(width.abs(), height.abs());
// Convenience methods that do the equivalent of calling the similarly named
// methods on a Rect constructed from the given origin and this size.
/// The offset to the intersection of the top and left edges of the rectangle
/// described by the given [Offset] (which is interpreted as the top-left corner)
/// and this [Size].
///
/// See also [Rect.topLeft].
Offset topLeft(Offset origin) => origin;
/// The offset to the center of the top edge of the rectangle described by the
/// given offset (which is interpreted as the top-left corner) and this size.
///
/// See also [Rect.topCenter].
Offset topCenter(Offset origin) => new Offset(origin.dx + width / 2.0, origin.dy);
/// The offset to the intersection of the top and right edges of the rectangle
/// described by the given offset (which is interpreted as the top-left corner)
/// and this size.
///
/// See also [Rect.topRight].
Offset topRight(Offset origin) => new Offset(origin.dx + width, origin.dy);
/// The offset to the center of the left edge of the rectangle described by the
/// given offset (which is interpreted as the top-left corner) and this size.
///
/// See also [Rect.centerLeft].
Offset centerLeft(Offset origin) => new Offset(origin.dx, origin.dy + height / 2.0);
/// The offset to the point halfway between the left and right and the top and
/// bottom edges of the rectangle described by the given offset (which is
/// interpreted as the top-left corner) and this size.
///
/// See also [Rect.center].
Offset center(Offset origin) => new Offset(origin.dx + width / 2.0, origin.dy + height / 2.0);
/// The offset to the center of the right edge of the rectangle described by the
/// given offset (which is interpreted as the top-left corner) and this size.
///
/// See also [Rect.centerLeft].
Offset centerRight(Offset origin) => new Offset(origin.dx + width, origin.dy + height / 2.0);
/// The offset to the intersection of the bottom and left edges of the
/// rectangle described by the given offset (which is interpreted as the
/// top-left corner) and this size.
///
/// See also [Rect.bottomLeft].
Offset bottomLeft(Offset origin) => new Offset(origin.dx, origin.dy + height);
/// The offset to the center of the bottom edge of the rectangle described by
/// the given offset (which is interpreted as the top-left corner) and this
/// size.
///
/// See also [Rect.bottomLeft].
Offset bottomCenter(Offset origin) => new Offset(origin.dx + width / 2.0, origin.dy + height);
/// The offset to the intersection of the bottom and right edges of the
/// rectangle described by the given offset (which is interpreted as the
/// top-left corner) and this size.
///
/// See also [Rect.bottomRight].
Offset bottomRight(Offset origin) => new Offset(origin.dx + width, origin.dy + height);
/// Whether the point specified by the given offset (which is assumed to be
/// relative to the top left of the size) lies between the left and right and
/// the top and bottom edges of a rectangle of this size.
///
/// Rectangles include their top and left edges but exclude their bottom and
/// right edges.
bool contains(Offset offset) {
return offset.dx >= 0.0 && offset.dx < width && offset.dy >= 0.0 && offset.dy < height;
}
/// A [Size] with the [width] and [height] swapped.
Size get flipped => new Size(height, width);
/// Linearly interpolate between two sizes
///
/// If either size is null, this function interpolates from [Size.zero].
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
/// that the interpolation has not started, returning `a` (or something
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
/// returning `b` (or something equivalent to `b`), and values in between
/// meaning that the interpolation is at the relevant point on the timeline
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
/// 1.0, so negative values and values greater than 1.0 are valid (and can
/// easily be generated by curves such as [Curves.elasticInOut]).
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static Size lerp(Size a, Size b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return b * t;
if (b == null)
return a * (1.0 - t);
return new Size(lerpDouble(a.width, b.width, t), lerpDouble(a.height, b.height, t));
}
/// Compares two Sizes for equality.
// We don't compare the runtimeType because of _DebugSize in the framework.
@override
bool operator ==(dynamic other) {
if (other is! Size)
return false;
final Size typedOther = other;
return _dx == typedOther._dx &&
_dy == typedOther._dy;
}
@override
int get hashCode => hashValues(_dx, _dy);
@override
String toString() => 'Size(${width?.toStringAsFixed(1)}, ${height?.toStringAsFixed(1)})';
}
/// An immutable, 2D, axis-aligned, floating-point rectangle whose coordinates
/// are relative to a given origin.
///
/// A Rect can be created with one its constructors or from an [Offset] and a
/// [Size] using the `&` operator:
///
/// ```dart
/// Rect myRect = const Offset(1.0, 2.0) & const Size(3.0, 4.0);
/// ```
class Rect {
Rect._();
/// Construct a rectangle from its left, top, right, and bottom edges.
@pragma('vm:entry-point')
Rect.fromLTRB(double left, double top, double right, double bottom) {
_value
..[0] = left
..[1] = top
..[2] = right
..[3] = bottom;
}
/// Construct a rectangle from its left and top edges, its width, and its
/// height.
///
/// To construct a [Rect] from an [Offset] and a [Size], you can use the
/// rectangle constructor operator `&`. See [Offset.&].
Rect.fromLTWH(double left, double top, double width, double height) {
_value
..[0] = left
..[1] = top
..[2] = left + width
..[3] = top + height;
}
/// Construct a rectangle that bounds the given circle.
///
/// The `center` argument is assumed to be an offset from the origin.
Rect.fromCircle({ Offset center, double radius }) {
_value
..[0] = center.dx - radius
..[1] = center.dy - radius
..[2] = center.dx + radius
..[3] = center.dy + radius;
}
/// Construct the smallest rectangle that encloses the given offsets, treating
/// them as vectors from the origin.
Rect.fromPoints(Offset a, Offset b) {
_value
..[0] = math.min(a.dx, b.dx)
..[1] = math.min(a.dy, b.dy)
..[2] = math.max(a.dx, b.dx)
..[3] = math.max(a.dy, b.dy);
}
static const int _kDataSize = 4;
final Float32List _value = new Float32List(_kDataSize);
/// The offset of the left edge of this rectangle from the x axis.
double get left => _value[0];
/// The offset of the top edge of this rectangle from the y axis.
double get top => _value[1];
/// The offset of the right edge of this rectangle from the x axis.
double get right => _value[2];
/// The offset of the bottom edge of this rectangle from the y axis.
double get bottom => _value[3];
/// The distance between the left and right edges of this rectangle.
double get width => right - left;
/// The distance between the top and bottom edges of this rectangle.
double get height => bottom - top;
/// The distance between the upper-left corner and the lower-right corner of
/// this rectangle.
Size get size => new Size(width, height);
/// A rectangle with left, top, right, and bottom edges all at zero.
static final Rect zero = new Rect._();
static const double _giantScalar = 1.0E+9; // matches kGiantRect from default_layer_builder.cc
/// A rectangle that covers the entire coordinate space.
///
/// This covers the space from -1e9,-1e9 to 1e9,1e9.
/// This is the space over which graphics operations are valid.
static final Rect largest = new Rect.fromLTRB(-_giantScalar, -_giantScalar, _giantScalar, _giantScalar);
/// Whether any of the coordinates of this rectangle are equal to positive infinity.
// included for consistency with Offset and Size
bool get isInfinite {
return left >= double.infinity
|| top >= double.infinity
|| right >= double.infinity
|| bottom >= double.infinity;
}
/// Whether all coordinates of this rectangle are finite.
bool get isFinite => left.isFinite && top.isFinite && right.isFinite && bottom.isFinite;
/// Whether this rectangle encloses a non-zero area. Negative areas are
/// considered empty.
bool get isEmpty => left >= right || top >= bottom;
/// Returns a new rectangle translated by the given offset.
///
/// To translate a rectangle by separate x and y components rather than by an
/// [Offset], consider [translate].
Rect shift(Offset offset) {
return new Rect.fromLTRB(left + offset.dx, top + offset.dy, right + offset.dx, bottom + offset.dy);
}
/// Returns a new rectangle with translateX added to the x components and
/// translateY added to the y components.
///
/// To translate a rectangle by an [Offset] rather than by separate x and y
/// components, consider [shift].
Rect translate(double translateX, double translateY) {
return new Rect.fromLTRB(left + translateX, top + translateY, right + translateX, bottom + translateY);
}
/// Returns a new rectangle with edges moved outwards by the given delta.
Rect inflate(double delta) {
return new Rect.fromLTRB(left - delta, top - delta, right + delta, bottom + delta);
}
/// Returns a new rectangle with edges moved inwards by the given delta.
Rect deflate(double delta) => inflate(-delta);
/// Returns a new rectangle that is the intersection of the given
/// rectangle and this rectangle. The two rectangles must overlap
/// for this to be meaningful. If the two rectangles do not overlap,
/// then the resulting Rect will have a negative width or height.
Rect intersect(Rect other) {
return new Rect.fromLTRB(
math.max(left, other.left),
math.max(top, other.top),
math.min(right, other.right),
math.min(bottom, other.bottom)
);
}
/// Returns a new rectangle which is the bounding box containing this
/// rectangle and the given rectangle.
Rect expandToInclude(Rect other) {
return new Rect.fromLTRB(
math.min(left, other.left),
math.min(top, other.top),
math.max(right, other.right),
math.max(bottom, other.bottom),
);
}
/// Whether `other` has a nonzero area of overlap with this rectangle.
bool overlaps(Rect other) {
if (right <= other.left || other.right <= left)
return false;
if (bottom <= other.top || other.bottom <= top)
return false;
return true;
}
/// The lesser of the magnitudes of the [width] and the [height] of this
/// rectangle.
double get shortestSide => math.min(width.abs(), height.abs());
/// The greater of the magnitudes of the [width] and the [height] of this
/// rectangle.
double get longestSide => math.max(width.abs(), height.abs());
/// The offset to the intersection of the top and left edges of this rectangle.
///
/// See also [Size.topLeft].
Offset get topLeft => new Offset(left, top);
/// The offset to the center of the top edge of this rectangle.
///
/// See also [Size.topCenter].
Offset get topCenter => new Offset(left + width / 2.0, top);
/// The offset to the intersection of the top and right edges of this rectangle.
///
/// See also [Size.topRight].
Offset get topRight => new Offset(right, top);
/// The offset to the center of the left edge of this rectangle.
///
/// See also [Size.centerLeft].
Offset get centerLeft => new Offset(left, top + height / 2.0);
/// The offset to the point halfway between the left and right and the top and
/// bottom edges of this rectangle.
///
/// See also [Size.center].
Offset get center => new Offset(left + width / 2.0, top + height / 2.0);
/// The offset to the center of the right edge of this rectangle.
///
/// See also [Size.centerLeft].
Offset get centerRight => new Offset(right, top + height / 2.0);
/// The offset to the intersection of the bottom and left edges of this rectangle.
///
/// See also [Size.bottomLeft].
Offset get bottomLeft => new Offset(left, bottom);
/// The offset to the center of the bottom edge of this rectangle.
///
/// See also [Size.bottomLeft].
Offset get bottomCenter => new Offset(left + width / 2.0, bottom);
/// The offset to the intersection of the bottom and right edges of this rectangle.
///
/// See also [Size.bottomRight].
Offset get bottomRight => new Offset(right, bottom);
/// Whether the point specified by the given offset (which is assumed to be
/// relative to the origin) lies between the left and right and the top and
/// bottom edges of this rectangle.
///
/// Rectangles include their top and left edges but exclude their bottom and
/// right edges.
bool contains(Offset offset) {
return offset.dx >= left && offset.dx < right && offset.dy >= top && offset.dy < bottom;
}
/// Linearly interpolate between two rectangles.
///
/// If either rect is null, [Rect.zero] is used as a substitute.
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
/// that the interpolation has not started, returning `a` (or something
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
/// returning `b` (or something equivalent to `b`), and values in between
/// meaning that the interpolation is at the relevant point on the timeline
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
/// 1.0, so negative values and values greater than 1.0 are valid (and can
/// easily be generated by curves such as [Curves.elasticInOut]).
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static Rect lerp(Rect a, Rect b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return new Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
if (b == null) {
final double k = 1.0 - t;
return new Rect.fromLTRB(a.left * k, a.top * k, a.right * k, a.bottom * k);
}
return new Rect.fromLTRB(
lerpDouble(a.left, b.left, t),
lerpDouble(a.top, b.top, t),
lerpDouble(a.right, b.right, t),
lerpDouble(a.bottom, b.bottom, t),
);
}
@override
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
if (runtimeType != other.runtimeType)
return false;
final Rect typedOther = other;
for (int i = 0; i < _kDataSize; i += 1) {
if (_value[i] != typedOther._value[i])
return false;
}
return true;
}
@override
int get hashCode => hashList(_value);
@override
String toString() => 'Rect.fromLTRB(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})';
}
/// A radius for either circular or elliptical shapes.
class Radius {
/// Constructs a circular radius. [x] and [y] will have the same radius value.
const Radius.circular(double radius) : this.elliptical(radius, radius);
/// Constructs an elliptical radius with the given radii.
const Radius.elliptical(this.x, this.y);
/// The radius value on the horizontal axis.
final double x;
/// The radius value on the vertical axis.
final double y;
/// A radius with [x] and [y] values set to zero.
///
/// You can use [Radius.zero] with [RRect] to have right-angle corners.
static const Radius zero = const Radius.circular(0.0);
/// Unary negation operator.
///
/// Returns a Radius with the distances negated.
///
/// Radiuses with negative values aren't geometrically meaningful, but could
/// occur as part of expressions. For example, negating a radius of one pixel
/// and then adding the result to another radius is equivalent to subtracting
/// a radius of one pixel from the other.
Radius operator -() => new Radius.elliptical(-x, -y);
/// Binary subtraction operator.
///
/// Returns a radius whose [x] value is the left-hand-side operand's [x]
/// minus the right-hand-side operand's [x] and whose [y] value is the
/// left-hand-side operand's [y] minus the right-hand-side operand's [y].
Radius operator -(Radius other) => new Radius.elliptical(x - other.x, y - other.y);
/// Binary addition operator.
///
/// Returns a radius whose [x] value is the sum of the [x] values of the
/// two operands, and whose [y] value is the sum of the [y] values of the
/// two operands.
Radius operator +(Radius other) => new Radius.elliptical(x + other.x, y + other.y);
/// Multiplication operator.
///
/// Returns a radius whose coordinates are the coordinates of the
/// left-hand-side operand (a radius) multiplied by the scalar
/// right-hand-side operand (a double).
Radius operator *(double operand) => new Radius.elliptical(x * operand, y * operand);
/// Division operator.
///
/// Returns a radius whose coordinates are the coordinates of the
/// left-hand-side operand (a radius) divided by the scalar right-hand-side
/// operand (a double).
Radius operator /(double operand) => new Radius.elliptical(x / operand, y / operand);
/// Integer (truncating) division operator.
///
/// Returns a radius whose coordinates are the coordinates of the
/// left-hand-side operand (a radius) divided by the scalar right-hand-side
/// operand (a double), rounded towards zero.
Radius operator ~/(double operand) => new Radius.elliptical((x ~/ operand).toDouble(), (y ~/ operand).toDouble());
/// Modulo (remainder) operator.
///
/// Returns a radius whose coordinates are the remainder of dividing the
/// coordinates of the left-hand-side operand (a radius) by the scalar
/// right-hand-side operand (a double).
Radius operator %(double operand) => new Radius.elliptical(x % operand, y % operand);
/// Linearly interpolate between two radii.
///
/// If either is null, this function substitutes [Radius.zero] instead.
///
/// The `t` argument represents position on the timeline, with 0.0 meaning
/// that the interpolation has not started, returning `a` (or something
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
/// returning `b` (or something equivalent to `b`), and values in between
/// meaning that the interpolation is at the relevant point on the timeline
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
/// 1.0, so negative values and values greater than 1.0 are valid (and can
/// easily be generated by curves such as [Curves.elasticInOut]).
///
/// Values for `t` are usually obtained from an [Animation<double>], such as
/// an [AnimationController].
static Radius lerp(Radius a, Radius b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return new Radius.elliptical(b.x * t, b.y * t);
if (b == null) {
final double k = 1.0 - t;
return new Radius.elliptical(a.x * k, a.y * k);
}
return new Radius.elliptical(
lerpDouble(a.x, b.x, t),
lerpDouble(a.y, b.y, t),
);
}
@override
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
if (runtimeType != other.runtimeType)
return false;
final Radius typedOther = other;
return typedOther.x == x && typedOther.y == y;
}
@override
int get hashCode => hashValues(x, y);
@override
String toString() {
return x == y ? 'Radius.circular(${x.toStringAsFixed(1)})' :
'Radius.elliptical(${x.toStringAsFixed(1)}, '
'${y.toStringAsFixed(1)})';
}
}
/// An immutable rounded rectangle with the custom radii for all four corners.
class RRect {
RRect._();
/// Construct a rounded rectangle from its left, top, right, and bottom edges,
/// and the same radii along its horizontal axis and its vertical axis.
RRect.fromLTRBXY(double left, double top, double right, double bottom,
double radiusX, double radiusY) {
_value
..[0] = left
..[1] = top
..[2] = right
..[3] = bottom
..[4] = radiusX
..[5] = radiusY