Chromium Code Reviews| Index: sdk/lib/math/rectangle.dart |
| diff --git a/sdk/lib/math/rectangle.dart b/sdk/lib/math/rectangle.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..96b916b108ee1d58cc546de0b1fd84a6716c06fe |
| --- /dev/null |
| +++ b/sdk/lib/math/rectangle.dart |
| @@ -0,0 +1,193 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| +part of dart.math; |
| + |
| +/** |
| + * A base class for representing two-dimensional axis-aligned rectangles. |
| + */ |
| +abstract class RectangleBase<T extends num> { |
|
Ivan Posva
2013/10/08 17:28:44
Why is this RectangleBase class exported? Do you e
|
| + // 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
|
| + // constructor and this one not. |
| + const RectangleBase(); |
| + |
| + /** |
| + * 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.
|
| + * corner can be found. |
| + */ |
| + T get left; |
| + /** |
| + * 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.
|
| + * left corner can be found. |
| + */ |
| + T get top; |
| + /** The `width` of the rectangle. */ |
| + T get width; |
| + /** The `height` of the rectangle. */ |
| + T get height; |
| + |
| + /** |
| + * The number of units `right` of the origin where this rectangle's bottom |
| + * right corner can be found. |
| + */ |
| + T get right => left + width; |
| + /** |
| + * The number of units below the origin where this rectangle's bottom |
| + * right corner can be found. |
| + */ |
| + T get bottom => top + height; |
| + |
| + String toString() { |
| + 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
|
| + } |
| + |
| + bool operator ==(other) { |
| + if (other is !Rectangle) return false; |
| + return left == other.left && top == other.top && width == other.width && |
| + height == other.height; |
| + } |
| + |
| + int get hashCode => _JenkinsSmiHash.hash4(left.hashCode, top.hashCode, |
| + width.hashCode, height.hashCode); |
| + |
| + /** |
| + * 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.
|
| + * 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.
|
| + */ |
| + Rectangle<T> intersection(Rectangle<T> rect) { |
| + var x0 = max(left, rect.left); |
| + var x1 = min(left + width, rect.left + rect.width); |
| + |
| + if (x0 <= x1) { |
| + var y0 = max(top, rect.top); |
| + var y1 = min(top + height, rect.top + rect.height); |
| + |
| + if (y0 <= y1) { |
| + return new Rectangle<T>(x0, y0, x1 - x0, y1 - y0); |
| + } |
| + } |
| + return null; |
| + } |
| + |
| + |
| + /** |
| + * 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.
|
| + */ |
| + bool intersects(Rectangle other) { |
| + return (left <= other.left + other.width && other.left <= left + width && |
| + top <= other.top + other.height && other.top <= top + height); |
| + } |
| + |
| + /** |
| + * 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.
|
| + * input rectangle. |
| + */ |
| + Rectangle<T> union(Rectangle<T> rect) { |
| + var right = max(this.left + this.width, rect.left + rect.width); |
| + var bottom = max(this.top + this.height, rect.top + rect.height); |
| + |
| + var left = min(this.left, rect.left); |
| + var top = min(this.top, rect.top); |
| + |
| + return new Rectangle<T>(left, top, right - left, bottom - top); |
| + } |
| + |
| + /** |
| + * Tests whether this rectangle entirely contains another rectangle. |
| + */ |
| + bool contains(Rectangle another) { |
| + return left <= another.left && |
| + left + width >= another.left + another.width && |
| + top <= another.top && |
| + top + height >= another.top + another.height; |
| + } |
| + |
| + /** |
| + * Tests whether this rectangle entirely contains a point. |
|
floitsch
2013/10/02 08:54:09
-entirely-
|
| + */ |
| + bool containsPoint(Point another) { |
| + return another.x >= left && |
| + another.x <= left + width && |
| + another.y >= top && |
| + another.y <= top + height; |
| + } |
| + |
| + 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.
|
| + height.ceil()); |
| + Rectangle<T> floor() => new Rectangle<T>(left.floor(), top.floor(), |
| + width.floor(), height.floor()); |
| + Rectangle<T> round() => new Rectangle<T>(left.round(), top.round(), |
| + width.round(), height.round()); |
| + |
| + /** |
| + * Truncates coordinates to integers and returns the result as a new |
| + * rectangle. |
| + */ |
| + Rectangle<int> truncate() => new Rectangle<int>(left.toInt(), top.toInt(), |
| + width.toInt(), height.toInt()); |
| + |
| + Point<T> get topLeft => new Point<T>(this.left, this.top); |
| + Point<T> get bottomRight => new Point<T>(this.left + this.width, |
| + this.top + this.height); |
| + |
| + static List<T> _calculateVerticesFromPoints(Point<T> a, Point<T> b) { |
| + var tempLeft; |
|
floitsch
2013/10/02 08:54:09
Remove "temp" prefixes in the function.
Emily Fortuna
2013/10/02 18:32:40
Done.
|
| + var tempWidth; |
| + if (a.x < b.x) { |
| + tempLeft = a.x; |
| + tempWidth = b.x - tempLeft; |
| + } else { |
| + tempLeft = b.x; |
| + tempWidth = a.x - tempLeft; |
| + } |
| + var tempTop; |
| + var tempHeight; |
| + if (a.y < b.y) { |
| + tempTop = a.y; |
| + tempHeight = b.y - tempTop; |
| + } else { |
| + tempTop = b.y; |
| + tempHeight = a.y - tempTop; |
| + } |
| + return [tempLeft, tempTop, tempWidth, tempHeight]; |
| + } |
| +} |
| + |
| + |
| +/** |
| + * A class for representing two-dimensional rectangles. |
| + * |
| + * 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.
|
| + * properties are immutable. |
| + */ |
| +class Rectangle<T> extends RectangleBase<T> { |
| + final T left; |
| + final T top; |
| + final T width; |
| + final T height; |
| + |
| + const Rectangle(this.left, this.top, this.width, this.height); |
| + |
| + factory Rectangle.fromPoints(Point<T> a, Point<T> b) { |
| + var list = RectangleBase._calculateVerticesFromPoints(a, b); |
| + return new Rectangle<T>(list[0], list[1], list[2], list[3]); |
| + } |
| +} |
| + |
| +/** |
| + * A class for representing two-dimensional axis-aligned rectangles with mutable |
| + * properties. |
| + */ |
| +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.
|
| + T left; |
| + T top; |
| + T width; |
| + T height; |
| + |
| + MutableRectangle(this.left, this.top, this.width, this.height); |
| + |
| + factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) { |
| + var list = RectangleBase._calculateVerticesFromPoints(a, b); |
| + return new MutableRectangle<T>(list[0], list[1], list[2], list[3]); |
| + } |
| +} |