OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of dart.sky; | 5 part of dart.sky; |
6 | 6 |
7 /// Holds 4 floating-point coordinates for a rectangle. | 7 /// Holds 4 floating-point coordinates for a rectangle. |
8 class Rect { | 8 class Rect { |
9 Rect(); | 9 Rect(); |
10 | 10 |
11 Rect.fromLTRB(double left, double top, double right, double bottom) { | 11 Rect.fromLTRB(double left, double top, double right, double bottom) { |
12 _value | 12 _value |
13 ..[0] = left | 13 ..[0] = left |
14 ..[1] = top | 14 ..[1] = top |
15 ..[2] = right | 15 ..[2] = right |
16 ..[3] = bottom; | 16 ..[3] = bottom; |
17 } | 17 } |
18 | 18 |
19 Rect.fromPointAndSize(Point point, Size size) { | 19 Rect.fromPointAndSize(Point point, Size size) { |
20 _value | 20 _value |
21 ..[0] = point.x | 21 ..[0] = point.x |
22 ..[1] = point.y | 22 ..[1] = point.y |
23 ..[2] = point.x + size.width | 23 ..[2] = point.x + size.width |
24 ..[3] = point.y + size.height; | 24 ..[3] = point.y + size.height; |
25 } | 25 } |
26 | 26 |
| 27 // assumes top-left corner is at the origin (0,0) |
27 Rect.fromSize(Size size) { | 28 Rect.fromSize(Size size) { |
28 _value | 29 _value |
29 ..[2] = size.width | 30 ..[2] = size.width |
30 ..[3] = size.height; | 31 ..[3] = size.height; |
31 } | 32 } |
32 | 33 |
33 final Float32List _value = new Float32List(4); | 34 final Float32List _value = new Float32List(4); |
34 double get left => _value[0]; | 35 double get left => _value[0]; |
35 double get top => _value[1]; | 36 double get top => _value[1]; |
36 double get right => _value[2]; | 37 double get right => _value[2]; |
37 double get bottom => _value[3]; | 38 double get bottom => _value[3]; |
38 | 39 |
39 Point get upperLeft => new Point(left, top); | |
40 Point get lowerRight => new Point(right, bottom); | |
41 Point get center => new Point(left + right / 2.0, top + bottom / 2.0); | 40 Point get center => new Point(left + right / 2.0, top + bottom / 2.0); |
| 41 Point get topLeft => new Point(left, top); |
| 42 Point get topRight => new Point(right, top); |
| 43 Point get bottomLeft => new Point(left, bottom); |
| 44 Point get bottomRight => new Point(right, bottom); |
42 | 45 |
43 Size get size => new Size(right - left, bottom - top); | 46 Size get size => new Size(right - left, bottom - top); |
44 | 47 |
45 // Rects are inclusive of the top and left edges but exclusive of the bottom | 48 // Rects are inclusive of the top and left edges but exclusive of the bottom |
46 // right edges. | 49 // right edges. |
47 bool contains(Point point) { | 50 bool contains(Point point) { |
48 return point.x >= left && point.x < right && point.y >= top && point.y < bot
tom; | 51 return point.x >= left && point.x < right && point.y >= top && point.y < bot
tom; |
49 } | 52 } |
50 | 53 |
51 bool operator ==(other) { | 54 bool operator ==(other) { |
52 if (other is! Rect) | 55 if (other is! Rect) |
53 return false; | 56 return false; |
54 for (var i = 0; i < 4; ++i) { | 57 for (var i = 0; i < 4; ++i) { |
55 if (_value[i] != other._value[i]) | 58 if (_value[i] != other._value[i]) |
56 return false; | 59 return false; |
57 } | 60 } |
58 return true; | 61 return true; |
59 } | 62 } |
60 | |
61 int get hashCode =>_value.fold(373, (value, item) => (37 * value + item.hashCo
de)); | 63 int get hashCode =>_value.fold(373, (value, item) => (37 * value + item.hashCo
de)); |
62 String toString() => "Rect.fromLTRB($left, $top, $right, $bottom)"; | 64 String toString() => "Rect.fromLTRB($left, $top, $right, $bottom)"; |
63 } | 65 } |
OLD | NEW |