OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** | |
6 * A utility class for representing two-dimensional sizes. | |
7 */ | |
8 class Size { | |
9 num width; | |
10 num height; | |
11 | |
12 Size(num this.width, num this.height) { | |
13 } | |
14 | |
15 bool operator ==(Size other) { | |
16 return other !== null && width == other.width && height == other.height; | |
17 } | |
18 | |
19 /** | |
20 * Returns the area of the size (width * height). | |
21 */ | |
22 num area() { | |
23 return width * height; | |
24 } | |
25 | |
26 /** | |
27 * Returns the ratio of the size's width to its height. | |
28 */ | |
29 num aspectRatio() { | |
30 return width / height; | |
31 } | |
32 | |
33 /** | |
34 * Clamps the width and height parameters upward to integer values. | |
35 * Returns this size with ceil'd components. | |
36 */ | |
37 Size ceil() { | |
38 width = width.ceil(); | |
39 height = height.ceil(); | |
40 return this; | |
41 } | |
42 | |
43 /** | |
44 * Returns a new copy of the Size. | |
45 */ | |
46 Size clone() { | |
47 return new Size(width, height); | |
48 } | |
49 | |
50 /** | |
51 * Returns true if this Size is the same size or smaller than the | |
52 * [target] size in both dimensions. | |
53 */ | |
54 bool fitsInside(Size target) { | |
55 return width <= target.width && height <= target.height; | |
56 } | |
57 | |
58 /** | |
59 * Clamps the width and height parameters downward to integer values. | |
60 * Returns this size with floored components. | |
61 */ | |
62 Size floor() { | |
63 width = width.floor(); | |
64 height = height.floor(); | |
65 return this; | |
66 } | |
67 | |
68 /** | |
69 * Returns the longer of the two dimensions in the size. | |
70 */ | |
71 num getLongest() { | |
72 return Math.max(width, height); | |
73 } | |
74 | |
75 /** | |
76 * Returns the shorter of the two dimensions in the size. | |
77 */ | |
78 num getShortest() { | |
79 return Math.min(width, height); | |
80 } | |
81 | |
82 /** | |
83 * Returns true if the size has zero area, false if both dimensions | |
84 * are non-zero numbers. | |
85 */ | |
86 bool isEmpty() { | |
87 return area() == 0; | |
88 } | |
89 | |
90 /** | |
91 * Returns the perimeter of the size (width + height) * 2. | |
92 */ | |
93 num perimeter() { | |
94 return (width + height) * 2; | |
95 } | |
96 | |
97 /** | |
98 * Rounds the width and height parameters to integer values. | |
99 * Returns this size with rounded components. | |
100 */ | |
101 Size round() { | |
102 width = width.round(); | |
103 height = height.round(); | |
104 return this; | |
105 } | |
106 | |
107 /** | |
108 * Scales the size uniformly by a factor. | |
109 * [s] The scale factor. | |
110 * Returns this Size object after scaling. | |
111 */ | |
112 Size scale(num s) { | |
113 width *= s; | |
114 height *= s; | |
115 return this; | |
116 } | |
117 | |
118 /** | |
119 * Uniformly scales the size to fit inside the dimensions of a given size. The | |
120 * original aspect ratio will be preserved. | |
121 * | |
122 * This function assumes that both Sizes contain strictly positive dimensions. | |
123 * Returns this Size object, after optional scaling. | |
124 */ | |
125 Size scaleToFit(Size target) { | |
126 num s = aspectRatio() > target.aspectRatio() ? | |
127 target.width / width : target.height / height; | |
128 return scale(s); | |
129 } | |
130 | |
131 /** | |
132 * Returns a nice string representing size. | |
133 * Returns in the form (50 x 73). | |
134 */ | |
135 String toString() { | |
136 return "(${width} x ${height})"; | |
137 } | |
138 } | |
OLD | NEW |