Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of html; | 5 part of html; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A base class for representing two-dimensional rectangles. This will hopefully | 8 * A class for representing two-dimensional rectangles. |
| 9 * be moved merged with the dart:math Rect. | |
| 10 */ | 9 */ |
| 11 // TODO(efortuna): Merge with Math rect after finalizing with Florian. | 10 class Rect { |
| 12 abstract class RectBase { | 11 final num left; |
| 13 num get left; | 12 final num top; |
| 14 num get top; | 13 final num width; |
| 15 num get width; | 14 final num height; |
| 16 num get height; | 15 |
| 16 const Rect(this.left, this.top, this.width, this.height); | |
| 17 | |
| 18 factory Rect.fromPoints(Point a, Point b) { | |
| 19 var left; | |
| 20 var width; | |
| 21 if (a.x < b.x) { | |
| 22 left = a.x; | |
| 23 width = b.x - left; | |
| 24 } else { | |
| 25 left = b.x; | |
| 26 width = a.x - left; | |
| 27 } | |
| 28 var top; | |
| 29 var height; | |
| 30 if (a.y < b.y) { | |
| 31 top = a.y; | |
| 32 height = b.y - top; | |
| 33 } else { | |
| 34 top = b.y; | |
| 35 height = a.y - top; | |
| 36 } | |
| 37 | |
| 38 return new Rect(left, top, width, height); | |
| 39 } | |
| 17 | 40 |
| 18 num get right => left + width; | 41 num get right => left + width; |
| 19 num get bottom => top + height; | 42 num get bottom => top + height; |
| 20 | 43 |
| 21 // NOTE! All code below should be common with Rect. | 44 // NOTE! All code below should be common with Rect. |
| 45 // TODO: implement with mixins when available. | |
| 22 | 46 |
| 23 String toString() { | 47 String toString() { |
| 24 return '($left, $top, $width, $height)'; | 48 return '($left, $top, $width, $height)'; |
| 25 } | 49 } |
| 26 | 50 |
| 27 bool operator ==(other) { | 51 bool operator ==(other) { |
| 28 if (other is !Rect) return false; | 52 if (other is !Rect) return false; |
| 29 return left == other.left && top == other.top && width == other.width && | 53 return left == other.left && top == other.top && width == other.width && |
| 30 height == other.height; | 54 height == other.height; |
| 31 } | 55 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 * Truncates coordinates to integers and returns the result as a new | 129 * Truncates coordinates to integers and returns the result as a new |
| 106 * rectangle. | 130 * rectangle. |
| 107 */ | 131 */ |
| 108 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), | 132 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), |
| 109 height.toInt()); | 133 height.toInt()); |
| 110 | 134 |
| 111 Point get topLeft => new Point(this.left, this.top); | 135 Point get topLeft => new Point(this.left, this.top); |
| 112 Point get bottomRight => new Point(this.left + this.width, | 136 Point get bottomRight => new Point(this.left + this.width, |
| 113 this.top + this.height); | 137 this.top + this.height); |
| 114 } | 138 } |
| 115 | |
| 116 | |
| 117 | |
| 118 /** | |
| 119 * A class for representing two-dimensional rectangles. | |
| 120 * | |
| 121 * This class is distinctive from RectBase in that it enforces that its | |
| 122 * properties are immutable. | |
| 123 */ | |
| 124 class Rect extends RectBase { | |
|
Jennifer Messerly
2013/07/19 02:22:59
The fix might be to use a mixin: "extends Object w
| |
| 125 final num left; | |
| 126 final num top; | |
| 127 final num width; | |
| 128 final num height; | |
| 129 | |
| 130 const Rect(this.left, this.top, this.width, this.height); | |
| 131 | |
| 132 factory Rect.fromPoints(Point a, Point b) { | |
| 133 var left; | |
| 134 var width; | |
| 135 if (a.x < b.x) { | |
| 136 left = a.x; | |
| 137 width = b.x - left; | |
| 138 } else { | |
| 139 left = b.x; | |
| 140 width = a.x - left; | |
| 141 } | |
| 142 var top; | |
| 143 var height; | |
| 144 if (a.y < b.y) { | |
| 145 top = a.y; | |
| 146 height = b.y - top; | |
| 147 } else { | |
| 148 top = b.y; | |
| 149 height = a.y - top; | |
| 150 } | |
| 151 | |
| 152 return new Rect(left, top, width, height); | |
| 153 } | |
| 154 } | |
| OLD | NEW |