mirror of
https://github.com/encounter/engine.git
synced 2026-03-30 11:09:55 -07:00
Add Rect.fromCenter() constructor (#8716)
This commit is contained in:
@@ -636,11 +636,20 @@ class Rect {
|
||||
/// 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 }) : this.fromLTRB(
|
||||
center.dx - radius,
|
||||
center.dy - radius,
|
||||
center.dx + radius,
|
||||
center.dy + radius,
|
||||
Rect.fromCircle({ Offset center, double radius }) : this.fromCenter(
|
||||
center: center,
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
);
|
||||
|
||||
/// Constructs a rectangle from its center point, width, and height.
|
||||
///
|
||||
/// The `center` argument is assumed to be an offset from the origin.
|
||||
Rect.fromCenter({ Offset center, double width, double height }) : this.fromLTRB(
|
||||
center.dx - width / 2,
|
||||
center.dy - height / 2,
|
||||
center.dx + width / 2,
|
||||
center.dy + height / 2,
|
||||
);
|
||||
|
||||
/// Construct the smallest rectangle that encloses the given offsets, treating
|
||||
|
||||
+14
-5
@@ -637,11 +637,20 @@ class Rect {
|
||||
/// 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 }) : this.fromLTRB(
|
||||
center.dx - radius,
|
||||
center.dy - radius,
|
||||
center.dx + radius,
|
||||
center.dy + radius,
|
||||
Rect.fromCircle({ Offset center, double radius }) : this.fromCenter(
|
||||
center: center,
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
);
|
||||
|
||||
/// Constructs a rectangle from its center point, width, and height.
|
||||
///
|
||||
/// The `center` argument is assumed to be an offset from the origin.
|
||||
Rect.fromCenter({ Offset center, double width, double height }) : this.fromLTRB(
|
||||
center.dx - width / 2,
|
||||
center.dy - height / 2,
|
||||
center.dx + width / 2,
|
||||
center.dy + height / 2,
|
||||
);
|
||||
|
||||
/// Construct the smallest rectangle that encloses the given offsets, treating
|
||||
|
||||
@@ -90,4 +90,26 @@ void main() {
|
||||
expect(const Size(-1.0, -1.0).aspectRatio, 1.0);
|
||||
expect(const Size(3.0, 4.0).aspectRatio, 3.0 / 4.0);
|
||||
});
|
||||
test('Rect.fromCenter', () {
|
||||
Rect rect = Rect.fromCenter(center: const Offset(1.0, 3.0), width: 5.0, height: 7.0);
|
||||
expect(rect.left, -1.5);
|
||||
expect(rect.top, -0.5);
|
||||
expect(rect.right, 3.5);
|
||||
expect(rect.bottom, 6.5);
|
||||
rect = Rect.fromCenter(center: const Offset(0.0, 0.0), width: 0.0, height: 0.0);
|
||||
expect(rect.left, 0.0);
|
||||
expect(rect.top, 0.0);
|
||||
expect(rect.right, 0.0);
|
||||
expect(rect.bottom, 0.0);
|
||||
rect = Rect.fromCenter(center: const Offset(double.nan, 0.0), width: 0.0, height: 0.0);
|
||||
expect(rect.left, isNaN);
|
||||
expect(rect.top, 0.0);
|
||||
expect(rect.right, isNaN);
|
||||
expect(rect.bottom, 0.0);
|
||||
rect = Rect.fromCenter(center: const Offset(0.0, double.nan), width: 0.0, height: 0.0);
|
||||
expect(rect.left, 0.0);
|
||||
expect(rect.top, isNaN);
|
||||
expect(rect.right, 0.0);
|
||||
expect(rect.bottom, isNaN);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user