OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 part of dart.math; | 4 part of dart.math; |
5 | 5 |
6 /** | 6 /** |
7 * A utility class for representing two-dimensional positions. | 7 * A utility class for representing two-dimensional positions. |
8 */ | 8 */ |
9 class Point<T extends num> { | 9 class Point<T extends num> { |
10 final T x; | 10 final T x; |
11 final T y; | 11 final T y; |
12 | 12 |
13 const Point(T x, T y): this.x = x, this.y = y; | 13 const Point(T x, T y) |
| 14 : this.x = x, |
| 15 this.y = y; |
14 | 16 |
15 String toString() => 'Point($x, $y)'; | 17 String toString() => 'Point($x, $y)'; |
16 | 18 |
17 /** | 19 /** |
18 * A `Point` is only equal to another `Point` with the same coordinates. | 20 * A `Point` is only equal to another `Point` with the same coordinates. |
19 * | 21 * |
20 * This point is equal to `other` if, and only if, | 22 * This point is equal to `other` if, and only if, |
21 * `other` is a `Point` with | 23 * `other` is a `Point` with |
22 * [x] equal to `other.x` and [y] equal to `other.y`. | 24 * [x] equal to `other.x` and [y] equal to `other.y`. |
23 */ | 25 */ |
24 bool operator ==(other) { | 26 bool operator ==(other) { |
25 if (other is !Point) return false; | 27 if (other is! Point) return false; |
26 return x == other.x && y == other.y; | 28 return x == other.x && y == other.y; |
27 } | 29 } |
28 | 30 |
29 int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode); | 31 int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode); |
30 | 32 |
31 /** | 33 /** |
32 * Add [other] to `this`, as if both points were vectors. | 34 * Add [other] to `this`, as if both points were vectors. |
33 * | 35 * |
34 * Returns the resulting "vector" as a Point. | 36 * Returns the resulting "vector" as a Point. |
35 */ | 37 */ |
(...skipping 12 matching lines...) Expand all Loading... |
48 | 50 |
49 /** | 51 /** |
50 * Scale this point by [factor] as if it were a vector. | 52 * Scale this point by [factor] as if it were a vector. |
51 * | 53 * |
52 * *Important* *Note*: This function accepts a `num` as its argument only so | 54 * *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 | 55 * 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 | 56 * 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_ | 57 * it, passing in a double [factor] on a `Point<int>` _causes_ _a_ |
56 * _runtime_ _error_ in checked mode. | 58 * _runtime_ _error_ in checked mode. |
57 */ | 59 */ |
58 Point<T> operator *(num/*T|int*/ factor) { | 60 Point<T> operator *(num /*T|int*/ factor) { |
59 return new Point<T>( | 61 return new Point<T>( |
60 (x * factor) as dynamic/*=T*/, (y * factor) as dynamic/*=T*/); | 62 (x * factor) as dynamic/*=T*/, (y * factor) as dynamic/*=T*/); |
61 } | 63 } |
62 | 64 |
63 /** | 65 /** |
64 * Get the straight line (Euclidean) distance between the origin (0, 0) and | 66 * Get the straight line (Euclidean) distance between the origin (0, 0) and |
65 * this point. | 67 * this point. |
66 */ | 68 */ |
67 double get magnitude => sqrt(x * x + y * y); | 69 double get magnitude => sqrt(x * x + y * y); |
68 | 70 |
(...skipping 11 matching lines...) Expand all Loading... |
80 * | 82 * |
81 * Squared distances can be used for comparisons when the actual value is not | 83 * Squared distances can be used for comparisons when the actual value is not |
82 * required. | 84 * required. |
83 */ | 85 */ |
84 T squaredDistanceTo(Point<T> other) { | 86 T squaredDistanceTo(Point<T> other) { |
85 var dx = x - other.x; | 87 var dx = x - other.x; |
86 var dy = y - other.y; | 88 var dy = y - other.y; |
87 return dx * dx + dy * dy; | 89 return dx * dx + dy * dy; |
88 } | 90 } |
89 } | 91 } |
OLD | NEW |