| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 part of dart.math; | |
| 5 | |
| 6 /** | |
| 7 * A utility class for representing two-dimensional positions. | |
| 8 */ | |
| 9 class Point<T extends num> { | |
| 10 final T x; | |
| 11 final T y; | |
| 12 | |
| 13 const Point(T x, T y): this.x = x, this.y = y; | |
| 14 | |
| 15 String toString() => 'Point($x, $y)'; | |
| 16 | |
| 17 /** | |
| 18 * A `Point` is only equal to another `Point` with the same coordinates. | |
| 19 * | |
| 20 * This point is equal to `other` if, and only if, | |
| 21 * `other` is a `Point` with | |
| 22 * [x] equal to `other.x` and [y] equal to `other.y`. | |
| 23 */ | |
| 24 bool operator ==(other) { | |
| 25 if (other is !Point) return false; | |
| 26 return x == other.x && y == other.y; | |
| 27 } | |
| 28 | |
| 29 int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode); | |
| 30 | |
| 31 /** | |
| 32 * Add [other] to `this`, as if both points were vectors. | |
| 33 * | |
| 34 * Returns the resulting "vector" as a Point. | |
| 35 */ | |
| 36 Point<T> operator +(Point<T> other) { | |
| 37 return new Point<T>(x + other.x, y + other.y); | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Subtract [other] from `this`, as if both points were vectors. | |
| 42 * | |
| 43 * Returns the resulting "vector" as a Point. | |
| 44 */ | |
| 45 Point<T> operator -(Point<T> other) { | |
| 46 return new Point<T>(x - other.x, y - other.y); | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Scale this point by [factor] as if it were a vector. | |
| 51 * | |
| 52 * *Important* *Note*: This function accepts a `num` as its argument only so | |
| 53 * that you can scale Point<double> objects by an `int` factor. Because the | |
| 54 * star operator always returns the same type of Point that originally called | |
| 55 * it, passing in a double [factor] on a `Point<int>` _causes_ _a_ | |
| 56 * _runtime_ _error_ in checked mode. | |
| 57 */ | |
| 58 Point<T> operator *(num/*T|int*/ factor) { | |
| 59 return new Point<T>( | |
| 60 (x * factor) as dynamic/*=T*/, (y * factor) as dynamic/*=T*/); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Get the straight line (Euclidean) distance between the origin (0, 0) and | |
| 65 * this point. | |
| 66 */ | |
| 67 double get magnitude => sqrt(x * x + y * y); | |
| 68 | |
| 69 /** | |
| 70 * Returns the distance between `this` and [other]. | |
| 71 */ | |
| 72 double distanceTo(Point<T> other) { | |
| 73 var dx = x - other.x; | |
| 74 var dy = y - other.y; | |
| 75 return sqrt(dx * dx + dy * dy); | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Returns the squared distance between `this` and [other]. | |
| 80 * | |
| 81 * Squared distances can be used for comparisons when the actual value is not | |
| 82 * required. | |
| 83 */ | |
| 84 T squaredDistanceTo(Point<T> other) { | |
| 85 var dx = x - other.x; | |
| 86 var dy = y - other.y; | |
| 87 return dx * dx + dy * dy; | |
| 88 } | |
| 89 } | |
| OLD | NEW |