Adding Rect.expandToInclude and Rect.longestSide (flutter/engine#4309)

Adding Rect.expandToInclude and Rect.longestSide to make the Rect API more symmetric.
This commit is contained in:
Greg Spencer
2017-11-09 16:37:21 -08:00
committed by GitHub
parent 98d001e88f
commit 6f2797a88d
2 changed files with 90 additions and 34 deletions

View File

@@ -92,7 +92,7 @@ abstract class OffsetBase {
int get hashCode => hashValues(_dx, _dy);
@override
String toString() => "$runtimeType(${_dx?.toStringAsFixed(1)}, ${_dy?.toStringAsFixed(1)})";
String toString() => '$runtimeType(${_dx?.toStringAsFixed(1)}, ${_dy?.toStringAsFixed(1)})';
}
/// An immutable 2D floating-point offset.
@@ -284,7 +284,7 @@ class Offset extends OffsetBase {
}
@override
String toString() => "Offset(${dx?.toStringAsFixed(1)}, ${dy?.toStringAsFixed(1)})";
String toString() => 'Offset(${dx?.toStringAsFixed(1)}, ${dy?.toStringAsFixed(1)})';
}
/// Holds a 2D floating-point size.
@@ -405,12 +405,11 @@ class Size extends OffsetBase {
/// [double]).
Size operator %(double operand) => new Size(width % operand, height % operand);
/// The lesser of the [width] and the [height].
double get shortestSide {
double w = width.abs();
double h = height.abs();
return w < h ? w : h;
}
/// 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.
@@ -513,7 +512,7 @@ class Size extends OffsetBase {
}
@override
String toString() => "Size(${width?.toStringAsFixed(1)}, ${height?.toStringAsFixed(1)})";
String toString() => 'Size(${width?.toStringAsFixed(1)}, ${height?.toStringAsFixed(1)})';
}
/// An immutable, 2D, axis-aligned, floating-point rectangle whose coordinates
@@ -653,6 +652,17 @@ class Rect {
);
}
/// 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)
@@ -662,13 +672,13 @@ class Rect {
return true;
}
/// The lesser of the magnitudes of the width and the height of this
/// The lesser of the magnitudes of the [width] and the [height] of this
/// rectangle.
double get shortestSide {
double w = width.abs();
double h = height.abs();
return w < h ? w : h;
}
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.
///
@@ -735,7 +745,7 @@ class Rect {
if (a == null)
return new Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
if (b == null) {
double k = 1.0 - t;
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(
@@ -764,7 +774,7 @@ class Rect {
int get hashCode => hashList(_value);
@override
String toString() => "Rect.fromLTRB(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})";
String toString() => 'Rect.fromLTRB(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})';
}
/// A radius for either circular or elliptical shapes.
@@ -847,7 +857,7 @@ class Radius {
if (a == null)
return new Radius.elliptical(b.x * t, b.y * t);
if (b == null) {
double k = 1.0 - t;
final double k = 1.0 - t;
return new Radius.elliptical(a.x * k, a.y * k);
}
return new Radius.elliptical(
@@ -1241,13 +1251,14 @@ class RRect {
/// Whether this rounded rectangle would draw as a circle.
bool get isCircle => width == height && isEllipse;
/// The lesser of the magnitudes of the width and the height of this rounded
/// rectangle.
double get shortestSide {
double w = width.abs();
double h = height.abs();
return w < h ? w : h;
}
/// The lesser of the magnitudes of the [width] and the [height] of this
/// rounded rectangle.
double get shortestSide => math.min(width.abs(), height.abs());
/// The greater of the magnitudes of the [width] and the [height] of this
/// rounded rectangle.
double get longestSide => math.max(width.abs(), height.abs());
/// The offset to the point halfway between the left and right and the top and
/// bottom edges of this rectangle.
@@ -1256,7 +1267,7 @@ class RRect {
// Returns the minimum between min and scale to which radius1 and radius2
// should be scaled with in order not to exceed the limit.
double _getMin(double min, double radius1, double radius2, double limit) {
double sum = radius1 + radius2;
final double sum = radius1 + radius2;
if (sum > limit && sum != 0.0)
return math.min(min, limit / sum);
return min;
@@ -1270,7 +1281,7 @@ class RRect {
void _scaleRadii() {
if (_scaled == null) {
double scale = 1.0;
final List<double> scaled = new List.from(_value);
final List<double> scaled = new List<double>.from(_value);
scale = _getMin(scale, scaled[11], scaled[5], height);
scale = _getMin(scale, scaled[4], scaled[6], width);
@@ -1364,7 +1375,7 @@ class RRect {
]);
}
if (b == null) {
double k = 1.0 - t;
final double k = 1.0 - t;
return new RRect._fromList(<double>[
a.left * k,
a.top * k,

View File

@@ -8,7 +8,7 @@ import 'package:test/test.dart';
void main() {
test("rect accessors", () {
Rect r = new Rect.fromLTRB(1.0, 3.0, 5.0, 7.0);
final Rect r = new Rect.fromLTRB(1.0, 3.0, 5.0, 7.0);
expect(r.left, equals(1.0));
expect(r.top, equals(3.0));
expect(r.right, equals(5.0));
@@ -16,22 +16,67 @@ void main() {
});
test("rect created by width and height", () {
Rect r = new Rect.fromLTWH(1.0, 3.0, 5.0, 7.0);
final Rect r = new Rect.fromLTWH(1.0, 3.0, 5.0, 7.0);
expect(r.left, equals(1.0));
expect(r.top, equals(3.0));
expect(r.right, equals(6.0));
expect(r.bottom, equals(10.0));
expect(r.shortestSide, equals(5.0));
expect(r.longestSide, equals(7.0));
});
test("rect intersection", () {
Rect r1 = new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
Rect r2 = new Rect.fromLTRB(50.0, 50.0, 200.0, 200.0);
Rect r3 = r1.intersect(r2);
final Rect r1 = new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final Rect r2 = new Rect.fromLTRB(50.0, 50.0, 200.0, 200.0);
final Rect r3 = r1.intersect(r2);
expect(r3.left, equals(50.0));
expect(r3.top, equals(50.0));
expect(r3.right, equals(100.0));
expect(r3.bottom, equals(100.0));
Rect r4 = r2.intersect(r1);
final Rect r4 = r2.intersect(r1);
expect(r4, equals(r3));
});
test("rect expandToInclude overlapping rects", () {
final Rect r1 = new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final Rect r2 = new Rect.fromLTRB(50.0, 50.0, 200.0, 200.0);
final Rect r3 = r1.expandToInclude(r2);
expect(r3.left, equals(0.0));
expect(r3.top, equals(0.0));
expect(r3.right, equals(200.0));
expect(r3.bottom, equals(200.0));
final Rect r4 = r2.expandToInclude(r1);
expect(r4, equals(r3));
});
test("rect expandToInclude crossing rects", () {
final Rect r1 = new Rect.fromLTRB(50.0, 0.0, 50.0, 200.0);
final Rect r2 = new Rect.fromLTRB(0.0, 50.0, 200.0, 50.0);
final Rect r3 = r1.expandToInclude(r2);
expect(r3.left, equals(0.0));
expect(r3.top, equals(0.0));
expect(r3.right, equals(200.0));
expect(r3.bottom, equals(200.0));
final Rect r4 = r2.expandToInclude(r1);
expect(r4, equals(r3));
});
test("size created from doubles", () {
final Size size = new Size(5.0, 7.0);
expect(size.width, equals(5.0));
expect(size.height, equals(7.0));
expect(size.shortestSide, equals(5.0));
expect(size.longestSide, equals(7.0));
});
test("rounded rect created from rect and radii", () {
final Rect baseRect = new Rect.fromLTWH(1.0, 3.0, 5.0, 7.0);
final RRect r = new RRect.fromRectXY(baseRect, 1.0, 1.0);
expect(r.left, equals(1.0));
expect(r.top, equals(3.0));
expect(r.right, equals(6.0));
expect(r.bottom, equals(10.0));
expect(r.shortestSide, equals(5.0));
expect(r.longestSide, equals(7.0));
});
}