| 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 part of dart.math; |
| 5 |
| 6 /** |
| 7 * A base class for representing two-dimensional axis-aligned rectangles. |
| 8 */ |
| 9 abstract class RectangleBase<T extends num> { |
| 10 const RectangleBase(); |
| 11 |
| 12 /** |
| 13 * The x-coordinate of the left edge. |
| 14 */ |
| 15 T get left; |
| 16 /** |
| 17 * The y-coordinate of the top edge. |
| 18 */ |
| 19 T get top; |
| 20 /** The `width` of the rectangle. */ |
| 21 T get width; |
| 22 /** The `height` of the rectangle. */ |
| 23 T get height; |
| 24 |
| 25 /** |
| 26 * The number of units `right` of the origin where this rectangle's bottom |
| 27 * right corner can be found. |
| 28 */ |
| 29 T get right => left + width; |
| 30 /** |
| 31 * The number of units below the origin where this rectangle's bottom |
| 32 * right corner can be found. |
| 33 */ |
| 34 T get bottom => top + height; |
| 35 |
| 36 String toString() { |
| 37 return 'Rectangle ($left, $top) $width x $height'; |
| 38 } |
| 39 |
| 40 bool operator ==(other) { |
| 41 if (other is !Rectangle) return false; |
| 42 return left == other.left && top == other.top && width == other.width && |
| 43 height == other.height; |
| 44 } |
| 45 |
| 46 int get hashCode => _JenkinsSmiHash.hash4(left.hashCode, top.hashCode, |
| 47 width.hashCode, height.hashCode); |
| 48 |
| 49 /** |
| 50 * Computes the intersection of `this` and [other]. |
| 51 * |
| 52 * Returns null if there is no intersection. |
| 53 */ |
| 54 Rectangle<T> intersection(Rectangle<T> other) { |
| 55 var x0 = max(left, other.left); |
| 56 var x1 = min(left + width, other.left + other.width); |
| 57 |
| 58 if (x0 <= x1) { |
| 59 var y0 = max(top, other.top); |
| 60 var y1 = min(top + height, other.top + other.height); |
| 61 |
| 62 if (y0 <= y1) { |
| 63 return new Rectangle<T>(x0, y0, x1 - x0, y1 - y0); |
| 64 } |
| 65 } |
| 66 return null; |
| 67 } |
| 68 |
| 69 |
| 70 /** |
| 71 * Returns true if `this` intersects [other]. |
| 72 */ |
| 73 bool intersects(Rectangle other) { |
| 74 return (left <= other.left + other.width && other.left <= left + width && |
| 75 top <= other.top + other.height && other.top <= top + height); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Returns a new rectangle which completely contains `this` and [other]. |
| 80 */ |
| 81 Rectangle<T> union(Rectangle<T> other) { |
| 82 var right = max(this.left + this.width, other.left + other.width); |
| 83 var bottom = max(this.top + this.height, other.top + other.height); |
| 84 |
| 85 var left = min(this.left, other.left); |
| 86 var top = min(this.top, other.top); |
| 87 |
| 88 return new Rectangle<T>(left, top, right - left, bottom - top); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Tests whether `this` entirely contains [another]. |
| 93 */ |
| 94 bool contains(Rectangle another) { |
| 95 return left <= another.left && |
| 96 left + width >= another.left + another.width && |
| 97 top <= another.top && |
| 98 top + height >= another.top + another.height; |
| 99 } |
| 100 |
| 101 /** |
| 102 * Tests whether `this` entirely contains a point. |
| 103 */ |
| 104 bool containsPoint(Point another) { |
| 105 return another.x >= left && |
| 106 another.x <= left + width && |
| 107 another.y >= top && |
| 108 another.y <= top + height; |
| 109 } |
| 110 |
| 111 Rectangle<int> ceil() => new Rectangle<int>(left.ceil(), top.ceil(), |
| 112 width.ceil(), height.ceil()); |
| 113 Rectangle<int> floor() => new Rectangle<int>(left.floor(), top.floor(), |
| 114 width.floor(), height.floor()); |
| 115 Rectangle<int> round() => new Rectangle<int>(left.round(), top.round(), |
| 116 width.round(), height.round()); |
| 117 |
| 118 /** |
| 119 * Truncates coordinates to integers and returns the result as a new |
| 120 * rectangle. |
| 121 */ |
| 122 Rectangle<int> truncate() => new Rectangle<int>(left.toInt(), top.toInt(), |
| 123 width.toInt(), height.toInt()); |
| 124 |
| 125 Point<T> get topLeft => new Point<T>(this.left, this.top); |
| 126 Point<T> get bottomRight => new Point<T>(this.left + this.width, |
| 127 this.top + this.height); |
| 128 |
| 129 static List<T> _calculateVerticesFromPoints(Point<T> a, Point<T> b) { |
| 130 var left; |
| 131 var width; |
| 132 if (a.x < b.x) { |
| 133 left = a.x; |
| 134 width = b.x - left; |
| 135 } else { |
| 136 left = b.x; |
| 137 width = a.x - left; |
| 138 } |
| 139 var top; |
| 140 var height; |
| 141 if (a.y < b.y) { |
| 142 top = a.y; |
| 143 height = b.y - top; |
| 144 } else { |
| 145 top = b.y; |
| 146 height = a.y - top; |
| 147 } |
| 148 return [left, top, width, height]; |
| 149 } |
| 150 } |
| 151 |
| 152 |
| 153 /** |
| 154 * A class for representing two-dimensional rectangles whose properties are |
| 155 * immutable. |
| 156 */ |
| 157 class Rectangle<T> extends RectangleBase<T> { |
| 158 final T left; |
| 159 final T top; |
| 160 final T width; |
| 161 final T height; |
| 162 |
| 163 const Rectangle(this.left, this.top, this.width, this.height); |
| 164 |
| 165 factory Rectangle.fromPoints(Point<T> a, Point<T> b) { |
| 166 var list = RectangleBase._calculateVerticesFromPoints(a, b); |
| 167 return new Rectangle<T>(list[0], list[1], list[2], list[3]); |
| 168 } |
| 169 } |
| OLD | NEW |