From 39ba4de88b8c8fcccdfacecdef3fe02083778553 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 9 Jan 2018 22:17:57 -0800 Subject: [PATCH] Offset.direction (flutter/engine#4530) Since we've got a vector class, and it has a getter for the magnitude, why not also a getter for the angle. --- engine/src/flutter/lib/ui/geometry.dart | 29 +++++++++++++++++++ .../flutter/testing/dart/geometry_test.dart | 22 ++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 engine/src/flutter/testing/dart/geometry_test.dart diff --git a/engine/src/flutter/lib/ui/geometry.dart b/engine/src/flutter/lib/ui/geometry.dart index 2f7b03d67cd..5249f1c03c1 100644 --- a/engine/src/flutter/lib/ui/geometry.dart +++ b/engine/src/flutter/lib/ui/geometry.dart @@ -142,6 +142,35 @@ class Offset extends OffsetBase { /// 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. diff --git a/engine/src/flutter/testing/dart/geometry_test.dart b/engine/src/flutter/testing/dart/geometry_test.dart new file mode 100644 index 00000000000..fc3ce174342 --- /dev/null +++ b/engine/src/flutter/testing/dart/geometry_test.dart @@ -0,0 +1,22 @@ +// Copyright 2018 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. + +import 'dart:ui'; +import 'dart:math' show pi; + +import 'package:test/test.dart'; + +void main() { + test('Offset.direction', () { + expect(const Offset(0.0, 0.0).direction, 0.0); + expect(const Offset(0.0, 1.0).direction, pi / 2.0); + expect(const Offset(0.0, -1.0).direction, -pi / 2.0); + expect(const Offset(1.0, 0.0).direction, 0.0); + expect(const Offset(1.0, 1.0).direction, pi / 4.0); + expect(const Offset(1.0, -1.0).direction, -pi / 4.0); + expect(const Offset(-1.0, 0.0).direction, pi); + 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); + }); +}