Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: sdk/lib/math/rectangle.dart

Issue 135273009: Make Rectangle and MutableRectangle constructors handle negative lengths. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove unnecessary T's. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/html/dartium/html_dartium.dart ('k') | tests/lib/math/rectangle_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.math; 4 part of dart.math;
5 5
6 /** 6 /**
7 * A base class for representing two-dimensional axis-aligned rectangles. 7 * A base class for representing two-dimensional axis-aligned rectangles.
8 * 8 *
9 * This rectangle uses a left-handed Cartesian coordinate system, with x 9 * This rectangle uses a left-handed Cartesian coordinate system, with x
10 * directed to the right and y directed down, as per the convention in 2D 10 * directed to the right and y directed down, as per the convention in 2D
11 * computer graphics. 11 * computer graphics.
12 * 12 *
13 * See also: 13 * See also:
14 * [W3C Coordinate Systems Specification](http://www.w3.org/TR/SVG/coords.htm l#InitialCoordinateSystem). 14 * [W3C Coordinate Systems Specification](http://www.w3.org/TR/SVG/coords.htm l#InitialCoordinateSystem).
15 *
16 * The rectangle is the set of points with representable coordinates greater
17 * than or equal to left/top, and with distance to left/top no greater than
18 * width/height (to the limit of the precission of the coordinates).
15 */ 19 */
16 abstract class _RectangleBase<T extends num> { 20 abstract class _RectangleBase<T extends num> {
17 const _RectangleBase(); 21 const _RectangleBase();
18 22
19 /** The x-coordinate of the left edge. */ 23 /** The x-coordinate of the left edge. */
20 T get left; 24 T get left;
21 /** The y-coordinate of the top edge. */ 25 /** The y-coordinate of the top edge. */
22 T get top; 26 T get top;
23 /** The `width` of the rectangle. */ 27 /** The width of the rectangle. */
24 T get width; 28 T get width;
25 /** The `height` of the rectangle. */ 29 /** The height of the rectangle. */
26 T get height; 30 T get height;
27 31
28 /** The x-coordinate of the right edge. */ 32 /** The x-coordinate of the right edge. */
29 T get right => left + width; 33 T get right => left + width;
30 /** The y-coordinate of the bottom edge. */ 34 /** The y-coordinate of the bottom edge. */
31 T get bottom => top + height; 35 T get bottom => top + height;
32 36
33 String toString() { 37 String toString() {
34 return 'Rectangle ($left, $top) $width x $height'; 38 return 'Rectangle ($left, $top) $width x $height';
35 } 39 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 /** 127 /**
124 * A class for representing two-dimensional rectangles whose properties are 128 * A class for representing two-dimensional rectangles whose properties are
125 * immutable. 129 * immutable.
126 */ 130 */
127 class Rectangle<T extends num> extends _RectangleBase<T> { 131 class Rectangle<T extends num> extends _RectangleBase<T> {
128 final T left; 132 final T left;
129 final T top; 133 final T top;
130 final T width; 134 final T width;
131 final T height; 135 final T height;
132 136
133 const Rectangle(this.left, this.top, this.width, this.height); 137 /**
138 * Create a rectangle spanned by `(left, top)` and `(left+width, top+height)`.
139 *
140 * The rectangle contains the points
141 * with x-coordinate between `left` and `left + width`, and
142 * with y-coordiante between `top` and `top + height`, both inclusive.
143 *
144 * The `width` and `height` should be non-negative.
145 * If `width` is negative, it is truncated to zero.
srdjan 2014/01/30 15:28:45 I think clamped to zero is a better term than trun
Lasse Reichstein Nielsen 2014/01/31 08:51:53 True, and it matches the naming for Uint8ClampedLi
146 * Similar for `height`.
srdjan 2014/01/30 15:28:45 Please repeat the comment, instead of saying simil
Lasse Reichstein Nielsen 2014/01/31 08:51:53 Done.
147 *
148 * If `width` and `height` are zero, the "rectangle" comprises only the single
149 * point `(left, top)`.
150 */
151 const Rectangle(this.left, this.top, T width, T height)
152 : this.width = (width >= 0) ? width : -width * 0,
Emily Fortuna 2014/01/30 18:35:13 Why not: this.width = (width >= 0) ? width : 0, .
srdjan 2014/01/30 18:46:40 The problem is that width is of 'T extends num', a
Emily Fortuna 2014/01/30 18:56:23 Ah yes. Touché. (can we maybe at a little comment
Lasse Reichstein Nielsen 2014/01/31 08:51:53 I'll add a statuc "clampToZero" function and use t
153 this.height = (height >= 0) ? height : -height * 0;
134 154
155 /*
156 * Create a rectangle spanned by the points [a] and [b];
157 *
158 * The rectangle contains the points
159 * with x-coordinate between `a.x` and `b.x`, and
160 * with y-coordiante between `a.y` and `b.y`, both inclusive.
161 *
162 * If the distance between `a.x` and `b.x` is not representable
163 * (which can happen if one or both is a double),
164 * the actual right edge might be slightly off from `max(a.x, b.x)`.
165 * Similar for the y-coordinates and the bottom edge.
166 */
135 factory Rectangle.fromPoints(Point<T> a, Point<T> b) { 167 factory Rectangle.fromPoints(Point<T> a, Point<T> b) {
136 T left = min(a.x, b.x); 168 T left = min(a.x, b.x);
137 T width = max(a.x, b.x) - left; 169 T width = max(a.x, b.x) - left;
138 T top = min(a.y, b.y); 170 T top = min(a.y, b.y);
139 T height = max(a.y, b.y) - top; 171 T height = max(a.y, b.y) - top;
140 return new Rectangle<T>(left, top, width, height); 172 return new Rectangle<T>(left, top, width, height);
141 } 173 }
142 } 174 }
143 175
144 /** 176 /**
145 * A class for representing two-dimensional axis-aligned rectangles with mutable 177 * A class for representing two-dimensional axis-aligned rectangles with mutable
146 * properties. 178 * properties.
147 */ 179 */
148 class MutableRectangle<T extends num> extends _RectangleBase<T> 180 class MutableRectangle<T extends num> extends _RectangleBase<T>
149 implements Rectangle<T> { 181 implements Rectangle<T> {
182
183 /**
184 * The x-coordinate of the left edge.
185 *
186 * Setting the value will move the rectangle without changing its width.
187 */
150 T left; 188 T left;
189 /**
190 * The y-coordinate of the left edge.
191 *
192 * Setting the value will move the rectangle without changing its height.
193 */
151 T top; 194 T top;
152 T width; 195 T _width;
153 T height; 196 T _height;
154 197
155 MutableRectangle(this.left, this.top, this.width, this.height); 198 /**
199 * Create a mutable rectangle spanned by `(left, top)` and
200 * `(left+width, top+height)`.
201 *
202 * The rectangle contains the points
203 * with x-coordinate between `left` and `left + width`, and
204 * with y-coordiante between `top` and `top + height`, both inclusive.
205 *
206 * The `width` and `height` should be non-negative.
207 * If `width` is negative, it is truncated to zero.
208 * Similar for `height`.
srdjan 2014/01/30 15:28:45 dito
Lasse Reichstein Nielsen 2014/01/31 08:51:53 Done.
209 *
210 *
211 * If `width` and `height` are zero, the "rectangle" comprises only the single
212 * point `(left, top)`.
213 */
214 MutableRectangle(this.left, this.top, T width, T height)
215 : this._width = (width >= 0) ? width : -width * 0,
Lasse Reichstein Nielsen 2014/01/30 13:42:34 -width * 0 is integer zero if width is int and do
srdjan 2014/01/30 15:28:45 This is not as evident from code itself, please ad
Lasse Reichstein Nielsen 2014/01/31 08:51:53 I don't *need* to avoid -0.0, but I want to if I c
216 this._height = (height >= 0) ? height : -height * 0;
156 217
218 /*
219 * Create a mutable rectangle spanned by the points [a] and [b];
220 *
221 * The rectangle contains the points
222 * with x-coordinate between `a.x` and `b.x`, and
223 * with y-coordiante between `a.y` and `b.y`, both inclusive.
224 *
225 * If the distance between `a.x` and `b.x` is not representable
226 * (which can happen if one or both is a double),
227 * the actual right edge might be slightly off from `max(a.x, b.x)`.
228 * Similar for the y-coordinates and the bottom edge.
229 */
157 factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) { 230 factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) {
158 T left = min(a.x, b.x); 231 T left = min(a.x, b.x);
159 T width = max(a.x, b.x) - left; 232 T width = max(a.x, b.x) - left;
160 T top = min(a.y, b.y); 233 T top = min(a.y, b.y);
161 T height = max(a.y, b.y) - top; 234 T height = max(a.y, b.y) - top;
162 return new MutableRectangle<T>(left, top, width, height); 235 return new MutableRectangle<T>(left, top, width, height);
163 } 236 }
237
238 T get width => _width;
239
240 /**
241 * Sets the width of the rectangle.
242 *
243 * The width must be non-negative.
244 * If a negative width is supplied, it is truncated to zero.
245 *
246 * Setting the value will change the right edge of the rectangle,
247 * but will not change [left].
248 */
249 void set width(T width) {
250 if (width < 0) width = -width * 0; // Preserves int/double-ness of width.
251 _width = width;
252 }
253
254 T get height => _height;
255
256 /**
257 * Sets the height of the rectangle.
258 *
259 * The height must be non-negative.
260 * If a negative height is supplied, it is truncated to zero.
261 *
262 * Setting the value will change the bottom edge of the rectangle,
263 * but will not change [top].
264 */
265 void set height(T height) {
266 if (height < 0) height = -height * 0;
267 _height = height;
268 }
164 } 269 }
OLDNEW
« no previous file with comments | « sdk/lib/html/dartium/html_dartium.dart ('k') | tests/lib/math/rectangle_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698