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 | |
| 5 part of html; | |
| 6 | |
| 7 /// A utility class for representing two-dimensional positions. | |
|
Jennifer Messerly
2013/03/05 02:35:21
just curious, why /// here but /** */ below?
(i do
blois
2013/03/05 21:43:33
No reason, changed.
| |
| 8 class Point { | |
| 9 final num x; | |
| 10 final num y; | |
| 11 | |
| 12 Point([num x = 0, num y = 0]): x = x, y = y; | |
|
Jennifer Messerly
2013/03/05 02:35:21
this could be "const" to allow construction of con
blois
2013/03/05 21:43:33
Done.
| |
| 13 | |
| 14 String toString() { | |
|
Jennifer Messerly
2013/03/05 02:35:21
both are valid, but sometimes I like using inline
blois
2013/03/05 21:43:33
Done.
| |
| 15 return '($x, $y)'; | |
| 16 } | |
| 17 | |
| 18 bool operator ==(other) { | |
| 19 if (other is !Point) return false; | |
| 20 return x == other.x && y == other.y; | |
| 21 } | |
| 22 | |
| 23 Point operator +(Point other) { | |
| 24 return new Point(x + other.x, y + other.y); | |
| 25 } | |
| 26 | |
| 27 Point operator -(Point other) { | |
| 28 return new Point(x - other.x, y - other.y); | |
| 29 } | |
| 30 | |
| 31 Point operator *(num factor) { | |
| 32 return new Point(x * factor, y * factor); | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Returns the distance between two points. | |
| 37 */ | |
| 38 double distanceTo(Point other) { | |
| 39 var dx = x - other.x; | |
| 40 var dy = y - other.y; | |
| 41 return sqrt(dx * dx + dy * dy); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Returns the squared distance between two points. | |
| 46 * | |
| 47 * Squared distances can be used for comparisons when the actual value is not | |
| 48 * required. | |
| 49 */ | |
| 50 num squaredDistanceTo(Point other) { | |
| 51 var dx = x - other.x; | |
| 52 var dy = y - other.y; | |
| 53 return dx * dx + dy * dy; | |
| 54 } | |
| 55 | |
| 56 Point ceil() => new Point(x.ceil(), y.ceil()); | |
| 57 Point floor() => new Point(x.floor(), y.floor()); | |
| 58 Point round() => new Point(x.round(), y.round()); | |
| 59 | |
| 60 /** | |
| 61 * Truncates x and y to integers and returns the result as a new point. | |
| 62 */ | |
| 63 Point toInt() => new Point(x.toInt(), y.toInt()); | |
| 64 } | |
| OLD | NEW |