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