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 |
5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
6 | 6 |
7 @DocsEditable() | 7 @DocsEditable() |
8 $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS implements Rect$IMPLEME
NTS$NATIVESPEC { | 8 $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS implements Rectangle$IM
PLEMENTS$NATIVESPEC { |
9 | 9 |
10 // NOTE! All code below should be common with Rect. | 10 // NOTE! All code below should be common with RectangleBase. |
11 // TODO(blois): implement with mixins when available. | 11 String toString() { |
12 | 12 return 'Rectangle ($left, $top) $width x $height'; |
13 String toString() { | |
14 return '($left, $top, $width, $height)'; | |
15 } | 13 } |
16 | 14 |
17 bool operator ==(other) { | 15 bool operator ==(other) { |
18 if (other is !Rect) return false; | 16 if (other is !Rectangle) return false; |
19 return left == other.left && top == other.top && width == other.width && | 17 return left == other.left && top == other.top && width == other.width && |
20 height == other.height; | 18 height == other.height; |
21 } | 19 } |
22 | 20 |
23 int get hashCode => JenkinsSmiHash.hash4(left.hashCode, top.hashCode, | 21 int get hashCode => _JenkinsSmiHash.hash4(left.hashCode, top.hashCode, |
24 width.hashCode, height.hashCode); | 22 width.hashCode, height.hashCode); |
25 | 23 |
26 /** | 24 /** |
27 * Computes the intersection of this rectangle and the rectangle parameter. | 25 * Computes the intersection of `this` and [other]. |
28 * Returns null if there is no intersection. | 26 * |
| 27 * The intersection of two axis-aligned rectangles, if any, is always another |
| 28 * axis-aligned rectangle. |
| 29 * |
| 30 * Returns the intersection of this and `other`, or `null` if they don't |
| 31 * intersect. |
29 */ | 32 */ |
30 Rect intersection(Rect rect) { | 33 Rectangle intersection(Rectangle other) { |
31 var x0 = max(left, rect.left); | 34 var x0 = max(left, other.left); |
32 var x1 = min(left + width, rect.left + rect.width); | 35 var x1 = min(left + width, other.left + other.width); |
33 | 36 |
34 if (x0 <= x1) { | 37 if (x0 <= x1) { |
35 var y0 = max(top, rect.top); | 38 var y0 = max(top, other.top); |
36 var y1 = min(top + height, rect.top + rect.height); | 39 var y1 = min(top + height, other.top + other.height); |
37 | 40 |
38 if (y0 <= y1) { | 41 if (y0 <= y1) { |
39 return new Rect(x0, y0, x1 - x0, y1 - y0); | 42 return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
40 } | 43 } |
41 } | 44 } |
42 return null; | 45 return null; |
43 } | 46 } |
44 | 47 |
45 | 48 |
46 /** | 49 /** |
47 * Returns whether a rectangle intersects this rectangle. | 50 * Returns true if `this` intersects [other]. |
48 */ | 51 */ |
49 bool intersects(Rect other) { | 52 bool intersects(Rectangle<num> other) { |
50 return (left <= other.left + other.width && other.left <= left + width && | 53 return (left <= other.left + other.width && |
51 top <= other.top + other.height && other.top <= top + height); | 54 other.left <= left + width && |
| 55 top <= other.top + other.height && |
| 56 other.top <= top + height); |
52 } | 57 } |
53 | 58 |
54 /** | 59 /** |
55 * Returns a new rectangle which completely contains this rectangle and the | 60 * Returns a new rectangle which completely contains `this` and [other]. |
56 * input rectangle. | |
57 */ | 61 */ |
58 Rect union(Rect rect) { | 62 Rectangle boundingBox(Rectangle other) { |
59 var right = max(this.left + this.width, rect.left + rect.width); | 63 var right = max(this.left + this.width, other.left + other.width); |
60 var bottom = max(this.top + this.height, rect.top + rect.height); | 64 var bottom = max(this.top + this.height, other.top + other.height); |
61 | 65 |
62 var left = min(this.left, rect.left); | 66 var left = min(this.left, other.left); |
63 var top = min(this.top, rect.top); | 67 var top = min(this.top, other.top); |
64 | 68 |
65 return new Rect(left, top, right - left, bottom - top); | 69 return new Rectangle(left, top, right - left, bottom - top); |
66 } | 70 } |
67 | 71 |
68 /** | 72 /** |
69 * Tests whether this rectangle entirely contains another rectangle. | 73 * Tests whether `this` entirely contains [another]. |
70 */ | 74 */ |
71 bool containsRect(Rect another) { | 75 bool contains(Rectangle<num> another) { |
72 return left <= another.left && | 76 return left <= another.left && |
73 left + width >= another.left + another.width && | 77 left + width >= another.left + another.width && |
74 top <= another.top && | 78 top <= another.top && |
75 top + height >= another.top + another.height; | 79 top + height >= another.top + another.height; |
76 } | 80 } |
77 | 81 |
78 /** | 82 /** |
79 * Tests whether this rectangle entirely contains a point. | 83 * Tests whether [another] is inside or along the edges of `this`. |
80 */ | 84 */ |
81 bool containsPoint(Point another) { | 85 bool containsPoint(Point<num> another) { |
82 return another.x >= left && | 86 return another.x >= left && |
83 another.x <= left + width && | 87 another.x <= left + width && |
84 another.y >= top && | 88 another.y >= top && |
85 another.y <= top + height; | 89 another.y <= top + height; |
86 } | 90 } |
87 | 91 |
88 Rect ceil() => new Rect(left.ceil(), top.ceil(), width.ceil(), height.ceil()); | |
89 Rect floor() => new Rect(left.floor(), top.floor(), width.floor(), | |
90 height.floor()); | |
91 Rect round() => new Rect(left.round(), top.round(), width.round(), | |
92 height.round()); | |
93 | |
94 /** | |
95 * Truncates coordinates to integers and returns the result as a new | |
96 * rectangle. | |
97 */ | |
98 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), | |
99 height.toInt()); | |
100 | |
101 Point get topLeft => new Point(this.left, this.top); | 92 Point get topLeft => new Point(this.left, this.top); |
| 93 Point get topRight => new Point(this.left + this.width, this.top); |
102 Point get bottomRight => new Point(this.left + this.width, | 94 Point get bottomRight => new Point(this.left + this.width, |
103 this.top + this.height); | 95 this.top + this.height); |
104 $!MEMBERS} | 96 Point get bottomLeft => new Point(this.left, |
| 97 this.top + this.height); |
| 98 |
| 99 $!MEMBERS} |
| 100 |
| 101 /** |
| 102 * This is the [Jenkins hash function][1] but using masking to keep |
| 103 * values in SMI range. |
| 104 * |
| 105 * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function |
| 106 * |
| 107 * Use: |
| 108 * Hash each value with the hash of the previous value, then get the final |
| 109 * hash by calling finish. |
| 110 * |
| 111 * var hash = 0; |
| 112 * for (var value in values) { |
| 113 * hash = JenkinsSmiHash.combine(hash, value.hashCode); |
| 114 * } |
| 115 * hash = JenkinsSmiHash.finish(hash); |
| 116 */ |
| 117 class _JenkinsSmiHash { |
| 118 // TODO(11617): This class should be optimized and standardized elsewhere. |
| 119 |
| 120 static int combine(int hash, int value) { |
| 121 hash = 0x1fffffff & (hash + value); |
| 122 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 123 return hash ^ (hash >> 6); |
| 124 } |
| 125 |
| 126 static int finish(int hash) { |
| 127 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 128 hash = hash ^ (hash >> 11); |
| 129 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 130 } |
| 131 |
| 132 static int hash2(a, b) => finish(combine(combine(0, a), b)); |
| 133 |
| 134 static int hash4(a, b, c, d) => |
| 135 finish(combine(combine(combine(combine(0, a), b), c), d)); |
| 136 } |
OLD | NEW |