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

Side by Side Diff: tools/dom/src/Rectangle.dart

Issue 12395036: Adding Point & Rect primitives (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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 | « tools/dom/src/Point.dart ('k') | tools/dom/templates/html/dart2js/html_dart2js.darttemplate » ('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
5 part of html;
6
7 /**
8 * A class for representing two-dimensional rectangles.
9 */
10 class Rect {
11 final num left;
12 final num top;
13 final num width;
14 final num height;
15
16 const Rect(this.left, this.top, this.width, this.height);
blois 2013/03/05 23:18:54 Made this constructor const as well, which necessi
Jennifer Messerly 2013/03/05 23:43:08 Hmmm. Interesting. Yeah, maybe that isn't the righ
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
41 num get right => left + width;
42 num get bottom => top + height;
43
44 // NOTE! All code below should be common with Rect.
45 // TODO: implement with mixins when available.
Jennifer Messerly 2013/03/05 23:43:08 TODO(blois) -- just so we know who has the context
46
47 String toString() {
48 return '($left, $top, $width, $height)';
49 }
50
51 bool operator ==(other) {
52 if (other is !Rect) return false;
53 return left == other.left && top == other.top && width == other.width &&
54 height == other.height;
55 }
56
57 /**
58 * Computes the intersection of this rectangle and the rectangle parameter.
59 * Returns null if there is no intersection.
60 */
61 Rect intersection(Rect rect) {
62 var x0 = max(left, rect.left);
63 var x1 = min(left + width, rect.left + rect.width);
64
65 if (x0 <= x1) {
66 var y0 = max(top, rect.top);
67 var y1 = min(top + height, rect.top + rect.height);
68
69 if (y0 <= y1) {
70 return new Rect(x0, y0, x1 - x0, y1 - y0);
71 }
72 }
73 return null;
74 }
75
76
77 /**
78 * Returns whether a rectangle intersects this rectangle.
79 */
80 bool intersects(Rect other) {
81 return (left <= other.left + other.width && other.left <= left + width &&
82 top <= other.top + other.height && other.top <= top + height);
83 }
84
85 /**
86 * Returns a new rectangle which completely contains this rectangle and the
87 * input rectangle.
88 */
89 Rect union(Rect rect) {
90 var right = max(this.left + this.width, rect.left + rect.width);
91 var bottom = max(this.top + this.height, rect.top + rect.height);
92
93 var left = min(this.left, rect.left);
94 var top = min(this.top, rect.top);
95
96 return new Rect(left, top, right - left, bottom - top);
97 }
98
99 /**
100 * Tests whether this rectangle entirely contains another rectangle.
101 */
102 bool containsRect(Rect another) {
103 return left <= another.left &&
104 left + width >= another.left + another.width &&
105 top <= another.top &&
106 top + height >= another.top + another.height;
107 }
108
109 /**
110 * Tests whether this rectangle entirely contains a point.
111 */
112 bool containsPoint(Point another) {
113 return another.x >= left &&
114 another.x <= left + width &&
115 another.y >= top &&
116 another.y <= top + height;
117 }
118
119 Rect ceil() => new Rect(left.ceil(), top.ceil(), width.ceil(), height.ceil());
120 Rect floor() => new Rect(left.floor(), top.floor(), width.floor(),
121 height.floor());
122 Rect round() => new Rect(left.round(), top.round(), width.round(),
123 height.round());
124
125 /**
126 * Truncates coordinates to integers and returns the result as a new
127 * rectangle.
128 */
129 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(),
130 height.toInt());
131
132 Point get topLeft => new Point(this.left, this.top);
133 Point get bottomRight => new Point(this.left + this.width,
134 this.top + this.height);
135 }
OLDNEW
« no previous file with comments | « tools/dom/src/Point.dart ('k') | tools/dom/templates/html/dart2js/html_dart2js.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698