From ce62ca408f778e16142f9e3fd83119b9172ef566 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Sat, 15 Dec 2018 08:31:59 -0800 Subject: [PATCH] Offset.fromDirection and Size.aspectRatio (flutter/engine#6805) --- engine/src/flutter/lib/ui/geometry.dart | 43 ++++++++++++++++--- .../flutter/testing/dart/geometry_test.dart | 39 ++++++++++++++++- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/lib/ui/geometry.dart b/engine/src/flutter/lib/ui/geometry.dart index e46cf63109..4c37708656 100644 --- a/engine/src/flutter/lib/ui/geometry.dart +++ b/engine/src/flutter/lib/ui/geometry.dart @@ -121,6 +121,15 @@ class Offset extends OffsetBase { /// and the second sets [dy], the vertical component. const Offset(double dx, double dy) : super(dx, dy); + /// Creates an offset from its [direction] and [distance]. + /// + /// The direction is in radians clockwise from the positive x-axis. + /// + /// The distance can be omitted, to create a unit vector (distance = 1.0). + factory Offset.fromDirection(double direction, [ double distance = 1.0 ]) { + return new Offset(distance * math.cos(direction), distance * math.sin(direction)); + } + /// The x component of the offset. /// /// The y component is given by [dy]. @@ -135,12 +144,12 @@ class Offset extends OffsetBase { /// /// 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); + 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; + 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 @@ -321,12 +330,12 @@ class Offset extends OffsetBase { if (other is! Offset) return false; final Offset typedOther = other; - return _dx == typedOther._dx && - _dy == typedOther._dy; + return dx == typedOther.dx && + dy == typedOther.dy; } @override - int get hashCode => hashValues(_dx, _dy); + int get hashCode => hashValues(dx, dy); @override String toString() => 'Offset(${dx?.toStringAsFixed(1)}, ${dy?.toStringAsFixed(1)})'; @@ -373,6 +382,30 @@ class Size extends OffsetBase { /// The vertical extent of this size. double get height => _dy; + /// The aspect ratio of this size. + /// + /// This returns the [width] divided by the [height]. + /// + /// If the [width] is zero, the result will be zero. If the [height] is zero + /// (and the [width] is not), the result will be [double.infinity] or + /// [double.negativeInfinity] as determined by the sign of [width]. + /// + /// See also: + /// + /// * [AspectRatio], a widget for giving a child widget a specific aspect + /// ratio. + /// * [FittedBox], a widget that (in most modes) attempts to maintain a + /// child widget's aspect ratio while changing its size. + double get aspectRatio { + if (height != 0.0) + return width / height; + if (width > 0.0) + return double.infinity; + if (width < 0.0) + return double.negativeInfinity; + return 0.0; + } + /// An empty size, one with a zero width and a zero height. static const Size zero = const Size(0.0, 0.0); diff --git a/engine/src/flutter/testing/dart/geometry_test.dart b/engine/src/flutter/testing/dart/geometry_test.dart index 4da3e6e825..9d14a5c25a 100644 --- a/engine/src/flutter/testing/dart/geometry_test.dart +++ b/engine/src/flutter/testing/dart/geometry_test.dart @@ -2,8 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:ui'; +import 'dart:math' as math show sqrt; import 'dart:math' show pi; +import 'dart:ui'; import 'package:test/test.dart'; @@ -19,4 +20,40 @@ void main() { expect(const Offset(-1.0, 1.0).direction, pi * 3.0 / 4.0); expect(const Offset(-1.0, -1.0).direction, -pi * 3.0 / 4.0); }); + test('Offset.fromDirection', () { + expect(Offset.fromDirection(0.0, 0.0), const Offset(0.0, 0.0)); + expect(Offset.fromDirection(pi / 2.0).dx, closeTo(0.0, 1e-12)); // aah, floating point math. i love you so. + expect(Offset.fromDirection(pi / 2.0).dy, 1.0); + expect(Offset.fromDirection(-pi / 2.0).dx, closeTo(0.0, 1e-12)); + expect(Offset.fromDirection(-pi / 2.0).dy, -1.0); + expect(Offset.fromDirection(0.0), const Offset(1.0, 0.0)); + expect(Offset.fromDirection(pi / 4.0).dx, closeTo(1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(pi / 4.0).dy, closeTo(1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(-pi / 4.0).dx, closeTo(1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(-pi / 4.0).dy, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(pi).dx, -1.0); + expect(Offset.fromDirection(pi).dy, closeTo(0.0, 1e-12)); + expect(Offset.fromDirection(pi * 3.0 / 4.0).dx, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(pi * 3.0 / 4.0).dy, closeTo(1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(-pi * 3.0 / 4.0).dx, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(-pi * 3.0 / 4.0).dy, closeTo(-1.0 / math.sqrt(2.0), 1e-12)); + expect(Offset.fromDirection(0.0, 2.0), const Offset(2.0, 0.0)); + expect(Offset.fromDirection(pi / 6, 2.0).dx, closeTo(math.sqrt(3.0), 1e-12)); + expect(Offset.fromDirection(pi / 6, 2.0).dy, closeTo(1.0, 1e-12)); + }); + test('Size.aspectRatio', () { + expect(const Size(0.0, 0.0).aspectRatio, 0.0); + expect(const Size(-0.0, 0.0).aspectRatio, 0.0); + expect(const Size(0.0, -0.0).aspectRatio, 0.0); + expect(const Size(-0.0, -0.0).aspectRatio, 0.0); + expect(const Size(0.0, 1.0).aspectRatio, 0.0); + expect(const Size(0.0, -1.0).aspectRatio, -0.0); + expect(const Size(1.0, 0.0).aspectRatio, double.infinity); + expect(const Size(1.0, 1.0).aspectRatio, 1.0); + expect(const Size(1.0, -1.0).aspectRatio, -1.0); + expect(const Size(-1.0, 0.0).aspectRatio, -double.infinity); + expect(const Size(-1.0, 1.0).aspectRatio, -1.0); + expect(const Size(-1.0, -1.0).aspectRatio, 1.0); + expect(const Size(3.0, 4.0).aspectRatio, 3.0 / 4.0); + }); }