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 class for representing two-dimensional rectangles. | 8 * A base class for representing two-dimensional rectangles. This will hopefully |
| 9 * be moved merged with the dart:math Rect. | |
| 9 */ | 10 */ |
| 10 class Rect { | 11 // TODO(efortuna): Merge with Math rect after finalizing with Florian. |
|
Jacob
2013/05/22 02:09:51
Why can't we just get the full class in Math.rect?
Emily Fortuna
2013/07/10 21:40:16
You're right. And that's why the TODO is here. I'm
| |
| 11 final num left; | 12 abstract class RectBase { |
| 12 final num top; | 13 num get left; |
| 13 final num width; | 14 num get top; |
| 14 final num height; | 15 num get width; |
| 15 | 16 num get height; |
| 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 } | |
| 40 | 17 |
| 41 num get right => left + width; | 18 num get right => left + width; |
| 42 num get bottom => top + height; | 19 num get bottom => top + height; |
| 43 | 20 |
| 44 // NOTE! All code below should be common with Rect. | 21 // NOTE! All code below should be common with Rect. |
| 45 // TODO: implement with mixins when available. | |
| 46 | 22 |
| 47 String toString() { | 23 String toString() { |
| 48 return '($left, $top, $width, $height)'; | 24 return '($left, $top, $width, $height)'; |
| 49 } | 25 } |
| 50 | 26 |
| 51 bool operator ==(other) { | 27 bool operator ==(other) { |
| 52 if (other is !Rect) return false; | 28 if (other is !Rect) return false; |
| 53 return left == other.left && top == other.top && width == other.width && | 29 return left == other.left && top == other.top && width == other.width && |
| 54 height == other.height; | 30 height == other.height; |
| 55 } | 31 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 * Truncates coordinates to integers and returns the result as a new | 102 * Truncates coordinates to integers and returns the result as a new |
| 127 * rectangle. | 103 * rectangle. |
| 128 */ | 104 */ |
| 129 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), | 105 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), |
| 130 height.toInt()); | 106 height.toInt()); |
| 131 | 107 |
| 132 Point get topLeft => new Point(this.left, this.top); | 108 Point get topLeft => new Point(this.left, this.top); |
| 133 Point get bottomRight => new Point(this.left + this.width, | 109 Point get bottomRight => new Point(this.left + this.width, |
| 134 this.top + this.height); | 110 this.top + this.height); |
| 135 } | 111 } |
| 112 | |
| 113 | |
| 114 | |
| 115 /** | |
| 116 * A class for representing two-dimensional rectangles. | |
| 117 */ | |
| 118 class Rect extends RectBase { | |
| 119 final num left; | |
| 120 final num top; | |
| 121 final num width; | |
| 122 final num height; | |
| 123 | |
| 124 const Rect(this.left, this.top, this.width, this.height); | |
| 125 | |
| 126 factory Rect.fromPoints(Point a, Point b) { | |
| 127 var left; | |
| 128 var width; | |
| 129 if (a.x < b.x) { | |
| 130 left = a.x; | |
| 131 width = b.x - left; | |
| 132 } else { | |
| 133 left = b.x; | |
| 134 width = a.x - left; | |
| 135 } | |
| 136 var top; | |
| 137 var height; | |
| 138 if (a.y < b.y) { | |
| 139 top = a.y; | |
| 140 height = b.y - top; | |
| 141 } else { | |
| 142 top = b.y; | |
| 143 height = a.y - top; | |
| 144 } | |
| 145 | |
| 146 return new Rect(left, top, width, height); | |
| 147 } | |
| 148 } | |
| OLD | NEW |