OLD | NEW |
(Empty) | |
| 1 import 'dart:math' hide Rectangle; |
| 2 import 'dart:math' as math show Point, Rectangle, MutableRectangle; |
| 3 import 'package:expect/expect.dart' show Expect; |
| 4 |
| 5 void main() { |
| 6 verifyRectable(new Rectangle(1, 2, 3, 4)); |
| 7 } |
| 8 |
| 9 void verifyRectable(math.Rectangle rect) { |
| 10 Expect.equals(1.0, rect.left.toDouble()); |
| 11 Expect.equals(2.0, rect.top.toDouble()); |
| 12 Expect.equals(4.0, rect.right.toDouble()); |
| 13 Expect.equals(6.0, rect.bottom.toDouble()); |
| 14 } |
| 15 |
| 16 class Rectangle<T extends num> implements math.MutableRectangle<T> { |
| 17 T left; |
| 18 T top; |
| 19 T width; |
| 20 T height; |
| 21 |
| 22 Rectangle(this.left, this.top, this.width, this.height); |
| 23 |
| 24 T get right => left + width; |
| 25 |
| 26 T get bottom => top + height; |
| 27 |
| 28 Point<T> get topLeft => new Point<T>(left, top); |
| 29 |
| 30 Point<T> get topRight => new Point<T>(right, top); |
| 31 |
| 32 Point<T> get bottomLeft => new Point<T>(left, bottom); |
| 33 |
| 34 Point<T> get bottomRight => new Point<T>(right, bottom); |
| 35 |
| 36 //--------------------------------------------------------------------------- |
| 37 |
| 38 bool contains(num px, num py) { |
| 39 return left <= px && top <= py && right > px && bottom > py; |
| 40 } |
| 41 |
| 42 bool containsPoint(math.Point<num> p) { |
| 43 return contains(p.x, p.y); |
| 44 } |
| 45 |
| 46 bool intersects(math.Rectangle<num> r) { |
| 47 return left < r.right && right > r.left && top < r.bottom && bottom > r.top; |
| 48 } |
| 49 |
| 50 /// Returns a new rectangle which completely contains `this` and [other]. |
| 51 |
| 52 Rectangle<T> boundingBox(math.Rectangle<T> other) { |
| 53 T rLeft = min(left, other.left); |
| 54 T rTop = min(top, other.top); |
| 55 T rRight = max(right, other.right); |
| 56 T rBottom = max(bottom, other.bottom); |
| 57 return new Rectangle<T>(rLeft, rTop, rRight - rLeft, rBottom - rTop); |
| 58 } |
| 59 |
| 60 /// Tests whether `this` entirely contains [another]. |
| 61 |
| 62 bool containsRectangle(math.Rectangle<num> r) { |
| 63 return left <= r.left && |
| 64 top <= r.top && |
| 65 right >= r.right && |
| 66 bottom >= r.bottom; |
| 67 } |
| 68 |
| 69 Rectangle<T> intersection(math.Rectangle<T> rect) { |
| 70 T rLeft = max(left, rect.left); |
| 71 T rTop = max(top, rect.top); |
| 72 T rRight = min(right, rect.right); |
| 73 T rBottom = min(bottom, rect.bottom); |
| 74 return new Rectangle<T>(rLeft, rTop, rRight - rLeft, rBottom - rTop); |
| 75 } |
| 76 } |
OLD | NEW |