Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Represents a point in 2 dimensional space. | 6 * Represents a point in 2 dimensional space. |
| 7 */ | 7 */ |
| 8 class Coordinate { | 8 class Coordinate { |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 return dx * dx + dy * dy; | 46 return dx * dx + dy * dy; |
| 47 } | 47 } |
| 48 | 48 |
| 49 static Coordinate sum(Coordinate a, Coordinate b) { | 49 static Coordinate sum(Coordinate a, Coordinate b) { |
| 50 return new Coordinate(a.x + b.x, a.y + b.y); | 50 return new Coordinate(a.x + b.x, a.y + b.y); |
| 51 } | 51 } |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Returns a new copy of the coordinate. | 54 * Returns a new copy of the coordinate. |
| 55 */ | 55 */ |
| 56 Coordinate clone() { | 56 Coordinate clone() => new Coordinate(x, y); |
| 57 return new Coordinate(x, y); | |
| 58 } | |
| 59 | 57 |
| 60 /** | 58 String toString() => "($x, $y)"; |
|
nweiz
2011/10/28 03:58:38
Class name?
Jacob
2011/10/31 22:09:50
We intentionally avoid the class name.
If you wro
| |
| 61 * Returns a nice string representing the coordinate. | |
| 62 * @return {string} In the form (50, 73). | |
| 63 */ | |
| 64 String toString() { | |
| 65 return "(${x}, ${y})"; | |
| 66 } | |
| 67 } | 59 } |
| 68 | 60 |
| 69 /** | 61 /** |
| 70 * A utility class for representing two-dimensional sizes. | |
| 71 */ | |
| 72 class Size { | |
| 73 num width; | |
| 74 num height; | |
| 75 | |
| 76 Size(num this.width, num this.height) { | |
| 77 } | |
| 78 | |
| 79 bool operator ==(Size other) { | |
| 80 return other !== null && width == other.width && height == other.height; | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Returns the area of the size (width * height). | |
| 85 */ | |
| 86 num area() { | |
| 87 return width * height; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Returns the ratio of the size's width to its height. | |
| 92 */ | |
| 93 num aspectRatio() { | |
| 94 return width / height; | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Clamps the width and height parameters upward to integer values. | |
| 99 * Returns this size with ceil'd components. | |
| 100 */ | |
| 101 Size ceil() { | |
| 102 width = width.ceil(); | |
| 103 height = height.ceil(); | |
| 104 return this; | |
| 105 } | |
| 106 | |
| 107 /** | |
| 108 * Returns a new copy of the Size. | |
| 109 */ | |
| 110 Size clone() { | |
| 111 return new Size(width, height); | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Returns true if this Size is the same size or smaller than the | |
| 116 * [target] size in both dimensions. | |
| 117 */ | |
| 118 bool fitsInside(Size target) { | |
| 119 return width <= target.width && height <= target.height; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Clamps the width and height parameters downward to integer values. | |
| 124 * Returns this size with floored components. | |
| 125 */ | |
| 126 Size floor() { | |
| 127 width = width.floor(); | |
| 128 height = height.floor(); | |
| 129 return this; | |
| 130 } | |
| 131 | |
| 132 /** | |
| 133 * Returns the longer of the two dimensions in the size. | |
| 134 */ | |
| 135 num getLongest() { | |
| 136 return Math.max(width, height); | |
| 137 } | |
| 138 | |
| 139 /** | |
| 140 * Returns the shorter of the two dimensions in the size. | |
| 141 */ | |
| 142 num getShortest() { | |
| 143 return Math.min(width, height); | |
| 144 } | |
| 145 | |
| 146 /** | |
| 147 * Returns true if the size has zero area, false if both dimensions | |
| 148 * are non-zero numbers. | |
| 149 */ | |
| 150 bool isEmpty() { | |
| 151 return area() == 0; | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Returns the perimeter of the size (width + height) * 2. | |
| 156 */ | |
| 157 num perimeter() { | |
| 158 return (width + height) * 2; | |
| 159 } | |
| 160 | |
| 161 /** | |
| 162 * Rounds the width and height parameters to integer values. | |
| 163 * Returns this size with rounded components. | |
| 164 */ | |
| 165 Size round() { | |
| 166 width = width.round(); | |
| 167 height = height.round(); | |
| 168 return this; | |
| 169 } | |
| 170 | |
| 171 /** | |
| 172 * Scales the size uniformly by a factor. | |
| 173 * [s] The scale factor. | |
| 174 * Returns this Size object after scaling. | |
| 175 */ | |
| 176 Size scale(num s) { | |
| 177 width *= s; | |
| 178 height *= s; | |
| 179 return this; | |
| 180 } | |
| 181 | |
| 182 /** | |
| 183 * Uniformly scales the size to fit inside the dimensions of a given size. The | |
| 184 * original aspect ratio will be preserved. | |
| 185 * | |
| 186 * This function assumes that both Sizes contain strictly positive dimensions. | |
| 187 * Returns this Size object, after optional scaling. | |
| 188 */ | |
| 189 Size scaleToFit(Size target) { | |
| 190 num s = aspectRatio() > target.aspectRatio() ? | |
| 191 target.width / width : target.height / height; | |
| 192 return scale(s); | |
| 193 } | |
| 194 | |
| 195 /** | |
| 196 * Returns a nice string representing size. | |
| 197 * Returns in the form (50 x 73). | |
| 198 */ | |
| 199 String toString() { | |
| 200 return "(${width} x ${height})"; | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 /** | |
| 205 * Represents the interval { x | start <= x < end }. | 62 * Represents the interval { x | start <= x < end }. |
| 206 */ | 63 */ |
| 207 class Interval { | 64 class Interval { |
| 208 | 65 |
| 209 final num start; | 66 final num start; |
| 210 final num end; | 67 final num end; |
| 211 | 68 |
| 212 Interval(num this.start, num this.end) {} | 69 Interval(num this.start, num this.end) {} |
| 213 | 70 |
| 214 num get length() { | 71 num get length() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 225 } | 82 } |
| 226 | 83 |
| 227 bool contains(num value) { | 84 bool contains(num value) { |
| 228 return value >= start && value < end; | 85 return value >= start && value < end; |
| 229 } | 86 } |
| 230 | 87 |
| 231 String toString() { | 88 String toString() { |
| 232 return '(${start}, ${end})'; | 89 return '(${start}, ${end})'; |
| 233 } | 90 } |
| 234 } | 91 } |
| OLD | NEW |