提交 4ee18488 编写于 作者: M Matt Perry

Add operator==, hashCode, and toString for Rect, Point, and Size dart classes.

R=abarth@chromium.org, sethladd@google.com

Review URL: https://codereview.chromium.org/1154213005
上级 3046acd9
......@@ -4,9 +4,22 @@
part of dart.sky;
/// Holds 2 floating-point coordinates.
class Point {
double x;
double y;
Point(this.x, this.y);
bool operator ==(other) {
if (!(other is Point)) return false;
return x == other.x && y == other.y;
}
int get hashCode {
int result = 373;
result = 37 * result + x.hashCode;
result = 37 * result + y.hashCode;
return result;
}
String toString() => "Point($x, $y)";
}
......@@ -4,29 +4,26 @@
part of dart.sky;
/// Holds 4 floating-point coordinates for a rectangle.
class Rect {
Float32List _value;
final Float32List _value = new Float32List(4);
double get left => _value[0];
double get top => _value[1];
double get right => _value[2];
double get bottom => _value[3];
Rect() : new Float32List(4);
Rect();
Rect.fromPointAndSize(Point point, Size size) {
_value = new Float32List(4)
..[0] = point.x
..[1] = point.y
..[2] = point.x + size.width
..[3] = point.y + size.height;
_value
..[0] = point.x
..[1] = point.y
..[2] = point.x + size.width
..[3] = point.y + size.height;
}
Rect.fromLTRB(double left, double top, double right, double bottom) {
_value = new Float32List(4)
..[0] = left
..[1] = top
..[2] = right
..[3] = bottom;
setLTRB(left, top, right, bottom);
}
Point get upperLeft => new Point(left, top);
......@@ -36,13 +33,26 @@ class Rect {
// Rects are inclusive of the top and left edges but exclusive of the bottom
// right edges.
bool contains(Point point) => point.x >= left && point.x < right
&& point.y >= top && point.y < bottom;
bool contains(Point point) =>
point.x >= left && point.x < right && point.y >= top && point.y < bottom;
void setLTRB(double left, double top, double right, double bottom) {
_value[0] = left
..[1] = top
..[2] = right
..[3] = bottom;
_value
..[0] = left
..[1] = top
..[2] = right
..[3] = bottom;
}
bool operator ==(other) {
if (!(other is Rect)) return false;
for (var i = 0; i < 4; ++i) {
if (_value[i] != other._value[i]) return false;
}
return true;
}
int get hashCode {
return _value.fold(373, (value, item) => (37 * value + item.hashCode));
}
String toString() => "Rect.LTRB($left, $top, $right, $bottom)";
}
......@@ -4,9 +4,22 @@
part of dart.sky;
/// Holds a 2D floating-point size.
class Size {
double width;
double height;
Point(this.width, this.height);
Size(this.width, this.height);
bool operator ==(other) {
if (!(other is Size)) return false;
return width == other.width && height == other.height;
}
int get hashCode {
int result = 373;
result = 37 * result + width.hashCode;
result = 37 * result + height.hashCode;
return result;
}
String toString() => "Size($width, $height)";
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册