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