Chromium Code Reviews| 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 | 4 part of dart.math; |
| 5 part of html; | |
| 6 | 5 |
| 7 /** | 6 /** |
| 8 * A utility class for representing two-dimensional positions. | 7 * A utility class for representing two-dimensional positions. |
| 9 */ | 8 */ |
| 10 class Point { | 9 class Point<T extends num> { |
| 11 final num x; | 10 final T x; |
| 12 final num y; | 11 final T y; |
| 13 | 12 |
| 14 const Point([num x = 0, num y = 0]): x = x, y = y; | 13 const Point([T x = 0, T y = 0]): x = x, y = y; |
|
floitsch
2013/10/02 08:54:09
*minor* nit: this.x = x, this.y = y;
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 15 | 14 |
| 16 String toString() => '($x, $y)'; | 15 String toString() => '($x, $y)'; |
| 17 | 16 |
| 18 bool operator ==(other) { | 17 bool operator ==(other) { |
| 19 if (other is !Point) return false; | 18 if (other is !Point) return false; |
| 20 return x == other.x && y == other.y; | 19 return x == other.x && y == other.y; |
| 21 } | 20 } |
| 22 | 21 |
| 23 int get hashCode => JenkinsSmiHash.hash2(x.hashCode, y.hashCode); | 22 int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode); |
| 24 | 23 |
| 25 Point operator +(Point other) { | 24 Point<T> operator +(Point<T> other) { |
| 26 return new Point(x + other.x, y + other.y); | 25 return new Point(x + other.x, y + other.y); |
| 27 } | 26 } |
| 28 | 27 |
| 29 Point operator -(Point other) { | 28 Point<T> operator -(Point<T> other) { |
| 30 return new Point(x - other.x, y - other.y); | 29 return new Point(x - other.x, y - other.y); |
| 31 } | 30 } |
| 32 | 31 |
| 33 Point operator *(num factor) { | 32 Point<T> operator *(T factor) { |
|
floitsch
2013/10/02 08:54:09
that one is hard.
There is no reason not to write
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 34 return new Point(x * factor, y * factor); | 33 return new Point(x * factor, y * factor); |
| 35 } | 34 } |
| 36 | 35 |
| 37 /** | 36 /** |
| 38 * Get the straight line (Euclidean) distance between the origin (0, 0) and | 37 * Get the straight line (Euclidean) distance between the origin (0, 0) and |
| 39 * this point. | 38 * this point. |
| 40 */ | 39 */ |
| 41 num get magnitude => sqrt(x * x + y * y); | 40 T get magnitude => sqrt(x * x + y * y); |
| 42 | 41 |
| 43 /** | 42 /** |
| 44 * Returns the distance between two points. | 43 * Returns the distance between two points. |
|
floitsch
2013/10/02 08:54:09
`this` and [other].
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 45 */ | 44 */ |
| 46 double distanceTo(Point other) { | 45 double distanceTo(Point<T> other) { |
| 47 var dx = x - other.x; | 46 var dx = x - other.x; |
| 48 var dy = y - other.y; | 47 var dy = y - other.y; |
| 49 return sqrt(dx * dx + dy * dy); | 48 return sqrt(dx * dx + dy * dy); |
| 50 } | 49 } |
| 51 | 50 |
| 52 /** | 51 /** |
| 53 * Returns the squared distance between two points. | 52 * Returns the squared distance between two points. |
|
floitsch
2013/10/02 08:54:09
ditto.
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 54 * | 53 * |
| 55 * Squared distances can be used for comparisons when the actual value is not | 54 * Squared distances can be used for comparisons when the actual value is not |
| 56 * required. | 55 * required. |
| 57 */ | 56 */ |
| 58 num squaredDistanceTo(Point other) { | 57 T squaredDistanceTo(Point<T> other) { |
| 59 var dx = x - other.x; | 58 var dx = x - other.x; |
| 60 var dy = y - other.y; | 59 var dy = y - other.y; |
| 61 return dx * dx + dy * dy; | 60 return dx * dx + dy * dy; |
| 62 } | 61 } |
| 63 | 62 |
| 64 Point ceil() => new Point(x.ceil(), y.ceil()); | 63 Point<T> ceil() => new Point<T>(x.ceil(), y.ceil()); |
|
Emily Fortuna
2013/10/01 23:12:21
One could argue that these should always return Po
floitsch
2013/10/02 08:54:09
Are these functions really needed?
I think you ne
Emily Fortuna
2013/10/02 18:32:40
Done (converted them to int).
These functions wer
| |
| 65 Point floor() => new Point(x.floor(), y.floor()); | 64 Point<T> floor() => new Point<T>(x.floor(), y.floor()); |
| 66 Point round() => new Point(x.round(), y.round()); | 65 Point<T> round() => new Point<T>(x.round(), y.round()); |
| 67 | 66 |
| 68 /** | 67 /** |
| 69 * Truncates x and y to integers and returns the result as a new point. | 68 * Truncates x and y to integers and returns the result as a new point. |
| 70 */ | 69 */ |
| 71 Point toInt() => new Point(x.toInt(), y.toInt()); | 70 Point<int> truncate() => new Point<int>(x.toInt(), y.toInt()); |
| 72 } | 71 } |
| OLD | NEW |