OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** | |
6 * Represents a point in 2 dimensional space. | |
7 */ | |
8 class Coordinate { | |
9 | |
10 /** | |
11 * X-value | |
12 */ | |
13 num x; | |
14 | |
15 /** | |
16 * Y-value | |
17 */ | |
18 num y; | |
19 | |
20 Coordinate([num this.x = 0, num this.y = 0]) { | |
21 } | |
22 | |
23 /** | |
24 * Gets the coordinates of a touch's location relative to the window's | |
25 * viewport. [input] is either a touch object or an event object. | |
26 */ | |
27 Coordinate.fromClient(var input) : this(input.clientX, input.clientY); | |
28 | |
29 static Coordinate difference(Coordinate a, Coordinate b) { | |
30 return new Coordinate(a.x - b.x, a.y - b.y); | |
31 } | |
32 | |
33 static num distance(Coordinate a, Coordinate b) { | |
34 final dx = a.x - b.x; | |
35 final dy = a.y - b.y; | |
36 return Math.sqrt(dx * dx + dy * dy); | |
37 } | |
38 | |
39 bool operator ==(Coordinate other) { | |
40 return other !== null && x == other.x && y == other.y; | |
41 } | |
42 | |
43 static num squaredDistance(Coordinate a, Coordinate b) { | |
44 final dx = a.x - b.x; | |
45 final dy = a.y - b.y; | |
46 return dx * dx + dy * dy; | |
47 } | |
48 | |
49 static Coordinate sum(Coordinate a, Coordinate b) { | |
50 return new Coordinate(a.x + b.x, a.y + b.y); | |
51 } | |
52 | |
53 /** | |
54 * Returns a new copy of the coordinate. | |
55 */ | |
56 Coordinate clone() => new Coordinate(x, y); | |
57 | |
58 String toString() => "($x, $y)"; | |
59 } | |
60 | |
61 /** | |
62 * Represents the interval { x | start <= x < end }. | |
63 */ | |
64 class Interval { | |
65 | |
66 final num start; | |
67 final num end; | |
68 | |
69 Interval(num this.start, num this.end) {} | |
70 | |
71 num get length() { | |
72 return end - start; | |
73 } | |
74 | |
75 bool operator ==(Interval other) { | |
76 return other !== null && other.start == start && other.end == end; | |
77 } | |
78 | |
79 Interval union(Interval other) { | |
80 return new Interval(Math.min(start, other.start), | |
81 Math.max(end, other.end)); | |
82 } | |
83 | |
84 bool contains(num value) { | |
85 return value >= start && value < end; | |
86 } | |
87 | |
88 String toString() { | |
89 return '(${start}, ${end})'; | |
90 } | |
91 } | |
OLD | NEW |