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 final Float32List _value = new Float32List(4); | 9 Rect(); |
10 double get left => _value[0]; | |
11 double get top => _value[1]; | |
12 double get right => _value[2]; | |
13 double get bottom => _value[3]; | |
14 | 10 |
15 Rect(); | 11 Rect.fromLTRB(double left, double top, double right, double bottom) { |
| 12 _value |
| 13 ..[0] = left |
| 14 ..[1] = top |
| 15 ..[2] = right |
| 16 ..[3] = bottom; |
| 17 } |
16 | 18 |
17 Rect.fromPointAndSize(Point point, Size size) { | 19 Rect.fromPointAndSize(Point point, Size size) { |
18 _value | 20 _value |
19 ..[0] = point.x | 21 ..[0] = point.x |
20 ..[1] = point.y | 22 ..[1] = point.y |
21 ..[2] = point.x + size.width | 23 ..[2] = point.x + size.width |
22 ..[3] = point.y + size.height; | 24 ..[3] = point.y + size.height; |
23 } | 25 } |
24 | 26 |
25 Rect.fromSize(Size size) { | 27 Rect.fromSize(Size size) { |
26 _value | 28 _value |
27 ..[2] = size.width | 29 ..[2] = size.width |
28 ..[3] = size.height; | 30 ..[3] = size.height; |
29 } | 31 } |
30 | 32 |
31 Rect.fromLTRB(double left, double top, double right, double bottom) { | 33 final Float32List _value = new Float32List(4); |
32 _value | 34 double get left => _value[0]; |
33 ..[0] = left | 35 double get top => _value[1]; |
34 ..[1] = top | 36 double get right => _value[2]; |
35 ..[2] = right | 37 double get bottom => _value[3]; |
36 ..[3] = bottom; | |
37 } | |
38 | 38 |
39 Point get upperLeft => new Point(left, top); | 39 Point get upperLeft => new Point(left, top); |
40 Point get lowerRight => new Point(right, bottom); | 40 Point get lowerRight => new Point(right, bottom); |
41 Point get center => new Point(left + right / 2.0, top + bottom / 2.0); | 41 Point get center => new Point(left + right / 2.0, top + bottom / 2.0); |
42 | 42 |
43 Size get size => new Size(right - left, bottom - top); | 43 Size get size => new Size(right - left, bottom - top); |
44 | 44 |
45 // Rects are inclusive of the top and left edges but exclusive of the bottom | 45 // Rects are inclusive of the top and left edges but exclusive of the bottom |
46 // right edges. | 46 // right edges. |
47 bool contains(Point point) { | 47 bool contains(Point point) { |
48 return point.x >= left && point.x < right && point.y >= top && point.y < bot
tom; | 48 return point.x >= left && point.x < right && point.y >= top && point.y < bot
tom; |
49 } | 49 } |
50 | 50 |
51 bool operator ==(other) { | 51 bool operator ==(other) { |
52 if (other is! Rect) | 52 if (other is! Rect) |
53 return false; | 53 return false; |
54 for (var i = 0; i < 4; ++i) { | 54 for (var i = 0; i < 4; ++i) { |
55 if (_value[i] != other._value[i]) | 55 if (_value[i] != other._value[i]) |
56 return false; | 56 return false; |
57 } | 57 } |
58 return true; | 58 return true; |
59 } | 59 } |
60 | 60 |
61 int get hashCode { | 61 int get hashCode =>_value.fold(373, (value, item) => (37 * value + item.hashCo
de)); |
62 return _value.fold(373, (value, item) => (37 * value + item.hashCode)); | |
63 } | |
64 String toString() => "Rect.fromLTRB($left, $top, $right, $bottom)"; | 62 String toString() => "Rect.fromLTRB($left, $top, $right, $bottom)"; |
65 } | 63 } |
OLD | NEW |