Chromium Code Reviews| 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> { | |
|
Ivan Posva
2013/10/08 17:28:44
Why is this RectangleBase class exported? Do you e
| |
| 10 // Not used, but keeps the VM from complaining about Rectangle having a const | |
|
floitsch
2013/10/02 08:54:09
Remove comment. As a base class the const construc
Emily Fortuna
2013/10/02 18:32:40
Oops, that was left over from when I was using thi
| |
| 11 // constructor and this one not. | |
| 12 const RectangleBase(); | |
| 13 | |
| 14 /** | |
| 15 * The number of units `left` of the origin where this rectangle's top left | |
|
floitsch
2013/10/02 08:54:09
The x-coordinate of the upper-left corner.
or
The
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 16 * corner can be found. | |
| 17 */ | |
| 18 T get left; | |
| 19 /** | |
| 20 * The number of units from the `top` origin point where this rectangle's top | |
|
floitsch
2013/10/02 08:54:09
The y-coordinate of the upper left corner.
or
The
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 21 * left corner can be found. | |
| 22 */ | |
| 23 T get top; | |
| 24 /** The `width` of the rectangle. */ | |
| 25 T get width; | |
| 26 /** The `height` of the rectangle. */ | |
| 27 T get height; | |
| 28 | |
| 29 /** | |
| 30 * The number of units `right` of the origin where this rectangle's bottom | |
| 31 * right corner can be found. | |
| 32 */ | |
| 33 T get right => left + width; | |
| 34 /** | |
| 35 * The number of units below the origin where this rectangle's bottom | |
| 36 * right corner can be found. | |
| 37 */ | |
| 38 T get bottom => top + height; | |
| 39 | |
| 40 String toString() { | |
| 41 return '($left, $top, $width, $height)'; | |
|
floitsch
2013/10/02 08:54:09
We are using "( ... )" to write iterables now.
I'm
Emily Fortuna
2013/10/02 18:32:40
Adjusted. Let me know if you'd like further change
| |
| 42 } | |
| 43 | |
| 44 bool operator ==(other) { | |
| 45 if (other is !Rectangle) return false; | |
| 46 return left == other.left && top == other.top && width == other.width && | |
| 47 height == other.height; | |
| 48 } | |
| 49 | |
| 50 int get hashCode => _JenkinsSmiHash.hash4(left.hashCode, top.hashCode, | |
| 51 width.hashCode, height.hashCode); | |
| 52 | |
| 53 /** | |
| 54 * Computes the intersection of this rectangle and the rectangle parameter. | |
|
floitsch
2013/10/02 08:54:09
`this` and [other].
I tend to avoid repeating the
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 55 * Returns null if there is no intersection. | |
|
floitsch
2013/10/02 08:54:09
New line before.
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 56 */ | |
| 57 Rectangle<T> intersection(Rectangle<T> rect) { | |
| 58 var x0 = max(left, rect.left); | |
| 59 var x1 = min(left + width, rect.left + rect.width); | |
| 60 | |
| 61 if (x0 <= x1) { | |
| 62 var y0 = max(top, rect.top); | |
| 63 var y1 = min(top + height, rect.top + rect.height); | |
| 64 | |
| 65 if (y0 <= y1) { | |
| 66 return new Rectangle<T>(x0, y0, x1 - x0, y1 - y0); | |
| 67 } | |
| 68 } | |
| 69 return null; | |
| 70 } | |
| 71 | |
| 72 | |
| 73 /** | |
| 74 * Returns whether a rectangle intersects this rectangle. | |
|
floitsch
2013/10/02 08:54:09
Returns `true` if `this` intersects (with?) [other
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 75 */ | |
| 76 bool intersects(Rectangle other) { | |
| 77 return (left <= other.left + other.width && other.left <= left + width && | |
| 78 top <= other.top + other.height && other.top <= top + height); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Returns a new rectangle which completely contains this rectangle and the | |
|
floitsch
2013/10/02 08:54:09
ditto (`this` [other])
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 83 * input rectangle. | |
| 84 */ | |
| 85 Rectangle<T> union(Rectangle<T> rect) { | |
| 86 var right = max(this.left + this.width, rect.left + rect.width); | |
| 87 var bottom = max(this.top + this.height, rect.top + rect.height); | |
| 88 | |
| 89 var left = min(this.left, rect.left); | |
| 90 var top = min(this.top, rect.top); | |
| 91 | |
| 92 return new Rectangle<T>(left, top, right - left, bottom - top); | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * Tests whether this rectangle entirely contains another rectangle. | |
| 97 */ | |
| 98 bool contains(Rectangle another) { | |
| 99 return left <= another.left && | |
| 100 left + width >= another.left + another.width && | |
| 101 top <= another.top && | |
| 102 top + height >= another.top + another.height; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Tests whether this rectangle entirely contains a point. | |
|
floitsch
2013/10/02 08:54:09
-entirely-
| |
| 107 */ | |
| 108 bool containsPoint(Point another) { | |
| 109 return another.x >= left && | |
| 110 another.x <= left + width && | |
| 111 another.y >= top && | |
| 112 another.y <= top + height; | |
| 113 } | |
| 114 | |
| 115 Rectangle<T> ceil() => new Rectangle<T>(left.ceil(), top.ceil(), width.ceil(), | |
|
floitsch
2013/10/02 08:54:09
same as for Point: This must return a Rectangle<in
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 116 height.ceil()); | |
| 117 Rectangle<T> floor() => new Rectangle<T>(left.floor(), top.floor(), | |
| 118 width.floor(), height.floor()); | |
| 119 Rectangle<T> round() => new Rectangle<T>(left.round(), top.round(), | |
| 120 width.round(), height.round()); | |
| 121 | |
| 122 /** | |
| 123 * Truncates coordinates to integers and returns the result as a new | |
| 124 * rectangle. | |
| 125 */ | |
| 126 Rectangle<int> truncate() => new Rectangle<int>(left.toInt(), top.toInt(), | |
| 127 width.toInt(), height.toInt()); | |
| 128 | |
| 129 Point<T> get topLeft => new Point<T>(this.left, this.top); | |
| 130 Point<T> get bottomRight => new Point<T>(this.left + this.width, | |
| 131 this.top + this.height); | |
| 132 | |
| 133 static List<T> _calculateVerticesFromPoints(Point<T> a, Point<T> b) { | |
| 134 var tempLeft; | |
|
floitsch
2013/10/02 08:54:09
Remove "temp" prefixes in the function.
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 135 var tempWidth; | |
| 136 if (a.x < b.x) { | |
| 137 tempLeft = a.x; | |
| 138 tempWidth = b.x - tempLeft; | |
| 139 } else { | |
| 140 tempLeft = b.x; | |
| 141 tempWidth = a.x - tempLeft; | |
| 142 } | |
| 143 var tempTop; | |
| 144 var tempHeight; | |
| 145 if (a.y < b.y) { | |
| 146 tempTop = a.y; | |
| 147 tempHeight = b.y - tempTop; | |
| 148 } else { | |
| 149 tempTop = b.y; | |
| 150 tempHeight = a.y - tempTop; | |
| 151 } | |
| 152 return [tempLeft, tempTop, tempWidth, tempHeight]; | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 | |
| 157 /** | |
| 158 * A class for representing two-dimensional rectangles. | |
| 159 * | |
| 160 * This class is distinctive from RectangleBase in that it enforces that its | |
|
floitsch
2013/10/02 08:54:09
This sentence isn't true: The RectangleBase only e
Emily Fortuna
2013/10/02 18:32:40
Done.
| |
| 161 * properties are immutable. | |
| 162 */ | |
| 163 class Rectangle<T> extends RectangleBase<T> { | |
| 164 final T left; | |
| 165 final T top; | |
| 166 final T width; | |
| 167 final T height; | |
| 168 | |
| 169 const Rectangle(this.left, this.top, this.width, this.height); | |
| 170 | |
| 171 factory Rectangle.fromPoints(Point<T> a, Point<T> b) { | |
| 172 var list = RectangleBase._calculateVerticesFromPoints(a, b); | |
| 173 return new Rectangle<T>(list[0], list[1], list[2], list[3]); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 /** | |
| 178 * A class for representing two-dimensional axis-aligned rectangles with mutable | |
| 179 * properties. | |
| 180 */ | |
| 181 class MutableRectangle<T> extends RectangleBase<T> implements Rectangle<T> { | |
|
floitsch
2013/10/02 08:54:09
Unless used and needed, I would prefer not to have
Emily Fortuna
2013/10/02 18:32:40
Removed.
| |
| 182 T left; | |
| 183 T top; | |
| 184 T width; | |
| 185 T height; | |
| 186 | |
| 187 MutableRectangle(this.left, this.top, this.width, this.height); | |
| 188 | |
| 189 factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) { | |
| 190 var list = RectangleBase._calculateVerticesFromPoints(a, b); | |
| 191 return new MutableRectangle<T>(list[0], list[1], list[2], list[3]); | |
| 192 } | |
| 193 } | |
| OLD | NEW |