Chromium Code Reviews| 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 = 0, T y = 0]): this.x = x, this.y = y; | |
| 14 | |
| 15 String toString() => '($x, $y)'; | |
|
Lasse Reichstein Nielsen
2013/10/03 06:24:03
I wouldn't mind it saying "Point($x, $y)".
Just th
Emily Fortuna
2013/10/03 21:29:12
Done.
| |
| 16 | |
| 17 bool operator ==(other) { | |
| 18 if (other is !Point) return false; | |
| 19 return x == other.x && y == other.y; | |
| 20 } | |
| 21 | |
| 22 int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode); | |
| 23 | |
| 24 Point<T> operator +(Point<T> other) { | |
|
Lasse Reichstein Nielsen
2013/10/03 06:24:03
These are really Vector operations, not Point oper
Emily Fortuna
2013/10/03 21:29:12
Done.
| |
| 25 return new Point<T>(x + other.x, y + other.y); | |
| 26 } | |
| 27 | |
| 28 Point<T> operator -(Point<T> other) { | |
| 29 return new Point<T>(x - other.x, y - other.y); | |
| 30 } | |
| 31 | |
| 32 /** | |
| 33 * Scale this point by [factor] as if it were a vector. | |
|
floitsch
2013/10/03 09:33:50
if it was a vector
Emily Fortuna
2013/10/03 21:29:12
Done.
| |
| 34 * | |
| 35 * *Important* *Note*: This function accepts a `num` as its argument only so | |
| 36 * that you can scale Point<double> objects by an `int` factor. Because the | |
|
Lasse Reichstein Nielsen
2013/10/03 06:24:03
I don't particularly like this, but I guess it is
Emily Fortuna
2013/10/03 21:29:12
No Florian and I don't weren't thrilled with it ei
| |
| 37 * star operator always returns the same type of Point that originally called | |
| 38 * it, passing in a double [factor] on a Point<int> _will_ _cause_ _a_ | |
|
Lasse Reichstein Nielsen
2013/10/03 06:24:03
`Point<int>`
floitsch
2013/10/03 09:33:50
No use for future: _causes a runtime error_ in che
Emily Fortuna
2013/10/03 21:29:12
Done.
Emily Fortuna
2013/10/03 21:29:12
Done.
| |
| 39 * _runtime_ _error_ in checked mode. | |
| 40 */ | |
| 41 Point<T> operator *(num factor) { | |
| 42 return new Point<T>(x * factor, y * factor); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Get the straight line (Euclidean) distance between the origin (0, 0) and | |
| 47 * this point. | |
| 48 */ | |
| 49 double get magnitude => sqrt(x * x + y * y); | |
| 50 | |
| 51 /** | |
| 52 * Returns the distance between `this` and [other]. | |
| 53 */ | |
| 54 double distanceTo(Point<T> other) { | |
| 55 var dx = x - other.x; | |
| 56 var dy = y - other.y; | |
| 57 return sqrt(dx * dx + dy * dy); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Returns the squared distance between `this` and [other]. | |
| 62 * | |
| 63 * Squared distances can be used for comparisons when the actual value is not | |
| 64 * required. | |
| 65 */ | |
| 66 T squaredDistanceTo(Point<T> other) { | |
| 67 var dx = x - other.x; | |
| 68 var dy = y - other.y; | |
| 69 return dx * dx + dy * dy; | |
| 70 } | |
| 71 | |
|
Lasse Reichstein Nielsen
2013/10/03 06:24:03
Needs documentation.
Emily Fortuna
2013/10/03 21:29:12
Done.
| |
| 72 Point<int> ceil() => new Point<int>(x.ceil(), y.ceil()); | |
| 73 Point<int> floor() => new Point<int>(x.floor(), y.floor()); | |
| 74 Point<int> round() => new Point<int>(x.round(), y.round()); | |
| 75 | |
| 76 /** | |
| 77 * Truncates x and y to integers and returns the result as a new point. | |
| 78 */ | |
| 79 Point<int> truncate() => new Point<int>(x.toInt(), y.toInt()); | |
| 80 } | |
| OLD | NEW |