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 /** | |
8 * A utility class for representing two-dimensional positions. | |
9 */ | |
10 class Point { | |
11 final num x; | |
12 final num y; | |
13 | |
14 const Point([num x = 0, num y = 0]): x = x, y = y; | |
15 | |
16 String toString() => '($x, $y)'; | |
17 | |
18 bool operator ==(other) { | |
19 if (other is !Point) return false; | |
20 return x == other.x && y == other.y; | |
21 } | |
22 | |
23 int get hashCode => JenkinsSmiHash.hash2(x.hashCode, y.hashCode); | |
24 | |
25 Point operator +(Point other) { | |
26 return new Point(x + other.x, y + other.y); | |
27 } | |
28 | |
29 Point operator -(Point other) { | |
30 return new Point(x - other.x, y - other.y); | |
31 } | |
32 | |
33 Point operator *(num factor) { | |
34 return new Point(x * factor, y * factor); | |
35 } | |
36 | |
37 /** | |
38 * Get the straight line (Euclidean) distance between the origin (0, 0) and | |
39 * this point. | |
40 */ | |
41 num get magnitude => sqrt(x * x + y * y); | |
42 | |
43 /** | |
44 * Returns the distance between two points. | |
45 */ | |
46 double distanceTo(Point other) { | |
47 var dx = x - other.x; | |
48 var dy = y - other.y; | |
49 return sqrt(dx * dx + dy * dy); | |
50 } | |
51 | |
52 /** | |
53 * Returns the squared distance between two points. | |
54 * | |
55 * Squared distances can be used for comparisons when the actual value is not | |
56 * required. | |
57 */ | |
58 num squaredDistanceTo(Point other) { | |
59 var dx = x - other.x; | |
60 var dy = y - other.y; | |
61 return dx * dx + dy * dy; | |
62 } | |
63 | |
64 Point ceil() => new Point(x.ceil(), y.ceil()); | |
65 Point floor() => new Point(x.floor(), y.floor()); | |
66 Point round() => new Point(x.round(), y.round()); | |
67 | |
68 /** | |
69 * Truncates x and y to integers and returns the result as a new point. | |
70 */ | |
71 Point toInt() => new Point(x.toInt(), y.toInt()); | |
72 } | |
OLD | NEW |