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 | |
| 5 part of html; | |
| 6 | |
| 7 /** | |
| 8 * A utility class for representing rectangles. | |
| 9 */ | |
|
Jacob
2013/03/05 02:20:21
Will you use this class any time the DOM currently
blois
2013/03/05 21:43:33
Yes, we should.
| |
| 10 class Rect { | |
| 11 final num left; | |
| 12 final num top; | |
| 13 final num width; | |
| 14 final num height; | |
|
Jennifer Messerly
2013/03/05 02:35:21
right, bottom getters?
blois
2013/03/05 21:43:33
Yeah, remembered those shortly after I sent this.
| |
| 15 | |
| 16 Rect(this.left, this.top, this.width, this.height) { | |
| 17 if (width < 0) { | |
| 18 throw ArgumentError(width); | |
| 19 } | |
| 20 if (height < 0) { | |
| 21 throw ArgumentError(height); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 factory Rect.fromCorners(Point a, Point b) { | |
|
blois
2013/03/05 02:08:51
Seems useful but not sure about naming.
Jennifer Messerly
2013/03/05 02:35:21
new Rect.fromPoints?
blois
2013/03/05 21:43:33
Done.
| |
| 26 var left; | |
| 27 var width; | |
| 28 if (a.x < b.x) { | |
| 29 left = a.x; | |
| 30 width = b.x - left; | |
| 31 } else { | |
| 32 left = b.x; | |
| 33 width = a.x - left; | |
| 34 } | |
| 35 var top; | |
| 36 var height; | |
| 37 if (a.y < b.y) { | |
| 38 top = a.y; | |
| 39 height = b.y - top; | |
| 40 } else { | |
| 41 top = b.y; | |
| 42 height = a.y - top; | |
| 43 } | |
| 44 | |
| 45 return new Rect(left, top, width, height); | |
| 46 } | |
| 47 | |
| 48 String toString() { | |
| 49 return '($left, $top, $width, $height)'; | |
| 50 } | |
| 51 | |
| 52 bool operator ==(other) { | |
| 53 if (other is !Rect) return false; | |
| 54 return left == other.left && top == other.top && width == other.width && | |
| 55 height == other.height; | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Computes the intersection of this rectangle and the rectangle parameter. | |
| 60 * Returns null if there is no intersection | |
|
Jennifer Messerly
2013/03/05 02:35:21
period at the end
blois
2013/03/05 21:43:33
Done.
| |
| 61 */ | |
| 62 Rect intersection(Rect rect) { | |
| 63 var x0 = max(left, rect.left); | |
| 64 var x1 = min(left + width, rect.left + rect.width); | |
| 65 | |
| 66 if (x0 <= x1) { | |
| 67 var y0 = max(top, rect.top); | |
| 68 var y1 = min(top + height, rect.top + rect.height); | |
| 69 | |
| 70 if (y0 <= y1) { | |
| 71 return new Rect(x0, y0, x1 - x0, y1 - y0); | |
| 72 } | |
| 73 } | |
| 74 return null; | |
| 75 } | |
| 76 | |
| 77 | |
| 78 /** | |
| 79 * Returns whether a rectangle intersects this rectangle. | |
| 80 */ | |
| 81 bool intersects(Rect other) { | |
| 82 return (left <= other.left + other.width && other.left <= left + width && | |
| 83 top <= other.top + other.height && other.top <= top + height); | |
|
Jennifer Messerly
2013/03/05 02:35:21
+2 indent
blois
2013/03/05 21:43:33
Done.
| |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * Returns a new rectangle which completely contains this rectangle and the | |
| 88 * input rectangle. | |
| 89 */ | |
| 90 Rect union(Rect rect) { | |
| 91 var right = max(this.left + this.width, rect.left + rect.width); | |
| 92 var bottom = max(this.top + this.height, rect.top + rect.height); | |
| 93 | |
| 94 var left = min(this.left, rect.left); | |
| 95 var top = min(this.top, rect.top); | |
| 96 | |
| 97 return new Rect(left, top, right - left, bottom - top); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * Tests whether this rectangle entirely contains another rectangle. | |
| 102 */ | |
| 103 bool containsRect(Rect another) { | |
| 104 return left <= another.left && | |
| 105 left + width >= another.left + another.width && | |
| 106 top <= another.top && | |
| 107 top + height >= another.top + another.height; | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Tests whether this rectangle entirely contains a point. | |
| 112 */ | |
| 113 bool containsPoint(Point another) { | |
| 114 return another.x >= left && | |
| 115 another.x <= left + width && | |
| 116 another.y >= top && | |
| 117 another.y <= top + height; | |
| 118 } | |
| 119 | |
| 120 Rect ceil() => new Rect(left.ceil(), top.ceil(), width.ceil(), height.ceil()); | |
| 121 Rect floor() => new Rect(left.floor(), top.floor(), width.floor(), | |
| 122 height.floor()); | |
| 123 Rect round() => new Rect(left.round(), top.round(), width.round(), | |
| 124 height.round()); | |
| 125 | |
| 126 /** | |
| 127 * Truncates coordinates to integers and returns the result as a new | |
| 128 * rectangle. | |
| 129 */ | |
| 130 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), | |
| 131 height.toInt()); | |
| 132 | |
| 133 Point get topLeft => new Point(this.left, this.top); | |
| 134 Point get bottomRight => new Point(this.left + this.width, | |
| 135 this.top + this.height); | |
| 136 } | |
| OLD | NEW |