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

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

Issue 25808002: Move Rectangle and Point into dart:math. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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/math/point.dart ('k') | tests/html/canvasrenderingcontext2d_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
(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 part of dart.math;
5
6 /**
7 * A base class for representing two-dimensional axis-aligned rectangles.
8 */
9 abstract class _RectangleBase<T extends num> {
10 const _RectangleBase();
11
12 /** The x-coordinate of the left edge. */
13 T get left;
14 /** The y-coordinate of the top edge. */
15 T get top;
16 /** The `width` of the rectangle. */
17 T get width;
18 /** The `height` of the rectangle. */
19 T get height;
20
21 /** The x-coordinate of the right edge. */
22 T get right => left + width;
23 /** The y-coordinate of the bottom edge. */
24 T get bottom => top + height;
25
26 String toString() {
27 return 'Rectangle ($left, $top) $width x $height';
28 }
29
30 bool operator ==(other) {
31 if (other is !Rectangle) return false;
32 return left == other.left && top == other.top && width == other.width &&
33 height == other.height;
34 }
35
36 int get hashCode => _JenkinsSmiHash.hash4(left.hashCode, top.hashCode,
37 width.hashCode, height.hashCode);
38
39 /**
40 * Computes the intersection of `this` and [other].
41 *
42 * The intersection of two axis-aligned rectangles, if any, is always another
43 * axis-aligned rectangle.
44 *
45 * Returns the intersection of this and `other`, or `null` if they don't
46 * intersect.
47 */
48 Rectangle<T> intersection(Rectangle<T> other) {
49 var x0 = max(left, other.left);
50 var x1 = min(left + width, other.left + other.width);
51
52 if (x0 <= x1) {
53 var y0 = max(top, other.top);
54 var y1 = min(top + height, other.top + other.height);
55
56 if (y0 <= y1) {
57 return new Rectangle<T>(x0, y0, x1 - x0, y1 - y0);
58 }
59 }
60 return null;
61 }
62
63
64 /**
65 * Returns true if `this` intersects [other].
66 */
67 bool intersects(Rectangle<num> other) {
68 return (left <= other.left + other.width &&
69 other.left <= left + width &&
70 top <= other.top + other.height &&
71 other.top <= top + height);
72 }
73
74 /**
75 * Returns a new rectangle which completely contains `this` and [other].
76 */
77 Rectangle<T> boundingBox(Rectangle<T> other) {
78 var right = max(this.left + this.width, other.left + other.width);
79 var bottom = max(this.top + this.height, other.top + other.height);
80
81 var left = min(this.left, other.left);
82 var top = min(this.top, other.top);
83
84 return new Rectangle<T>(left, top, right - left, bottom - top);
85 }
86
87 /**
88 * Tests whether `this` entirely contains [another].
89 */
90 bool contains(Rectangle<num> another) {
91 return left <= another.left &&
92 left + width >= another.left + another.width &&
93 top <= another.top &&
94 top + height >= another.top + another.height;
95 }
96
97 /**
98 * Tests whether [another] is inside or along the edges of `this`.
99 */
100 bool containsPoint(Point<num> another) {
101 return another.x >= left &&
102 another.x <= left + width &&
103 another.y >= top &&
104 another.y <= top + height;
105 }
106
107 Point<T> get topLeft => new Point<T>(this.left, this.top);
108 Point<T> get topRight => new Point<T>(this.left + this.width, this.top);
109 Point<T> get bottomRight => new Point<T>(this.left + this.width,
110 this.top + this.height);
111 Point<T> get bottomLeft => new Point<T>(this.left,
112 this.top + this.height);
113 }
114
115
116 /**
117 * A class for representing two-dimensional rectangles whose properties are
118 * immutable.
119 */
120 class Rectangle<T extends num> extends _RectangleBase<T> {
121 final T left;
122 final T top;
123 final T width;
124 final T height;
125
126 const Rectangle(this.left, this.top, this.width, this.height);
127
128 factory Rectangle.fromPoints(Point<T> a, Point<T> b) {
129 T left = min(a.x, b.x);
130 T width = max(a.x, b.x) - left;
131 T top = min(a.y, b.y);
132 T height = max(a.y, b.y) - top;
133 return new Rectangle<T>(left, top, width, height);
134 }
135 }
136
137 /**
138 * A class for representing two-dimensional axis-aligned rectangles with mutable
139 * properties.
140 */
141 class MutableRectangle<T extends num> extends _RectangleBase<T>
142 implements Rectangle<T> {
143 T left;
144 T top;
145 T width;
146 T height;
147
148 MutableRectangle(this.left, this.top, this.width, this.height);
149
150 factory MutableRectangle.fromPoints(Point<T> a, Point<T> b) {
151 T left = min(a.x, b.x);
152 T width = max(a.x, b.x) - left;
153 T top = min(a.y, b.y);
154 T height = max(a.y, b.y) - top;
155 return new MutableRectangle<T>(left, top, width, height);
156 }
157 }
siva 2013/10/08 22:05:30 Wouldn't it make more sense to call the classes :
Emily Fortuna 2013/10/08 22:20:27 We convey immutability that in the documentation.
OLDNEW
« no previous file with comments | « sdk/lib/math/point.dart ('k') | tests/html/canvasrenderingcontext2d_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698