Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: tools/dom/src/Point.dart

Issue 12395036: Adding Point & Rect primitives (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/dom/src/Point.dart
diff --git a/tools/dom/src/Point.dart b/tools/dom/src/Point.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bce5b8c754a6086f7dbf0fd82ee71472b0a2806b
--- /dev/null
+++ b/tools/dom/src/Point.dart
@@ -0,0 +1,64 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of html;
+
+/// A utility class for representing two-dimensional positions.
Jennifer Messerly 2013/03/05 02:35:21 just curious, why /// here but /** */ below? (i do
blois 2013/03/05 21:43:33 No reason, changed.
+class Point {
+ final num x;
+ final num y;
+
+ Point([num x = 0, num y = 0]): x = x, y = y;
Jennifer Messerly 2013/03/05 02:35:21 this could be "const" to allow construction of con
blois 2013/03/05 21:43:33 Done.
+
+ String toString() {
Jennifer Messerly 2013/03/05 02:35:21 both are valid, but sometimes I like using inline
blois 2013/03/05 21:43:33 Done.
+ return '($x, $y)';
+ }
+
+ bool operator ==(other) {
+ if (other is !Point) return false;
+ return x == other.x && y == other.y;
+ }
+
+ Point operator +(Point other) {
+ return new Point(x + other.x, y + other.y);
+ }
+
+ Point operator -(Point other) {
+ return new Point(x - other.x, y - other.y);
+ }
+
+ Point operator *(num factor) {
+ return new Point(x * factor, y * factor);
+ }
+
+ /**
+ * Returns the distance between two points.
+ */
+ double distanceTo(Point other) {
+ var dx = x - other.x;
+ var dy = y - other.y;
+ return sqrt(dx * dx + dy * dy);
+ }
+
+ /**
+ * Returns the squared distance between two points.
+ *
+ * Squared distances can be used for comparisons when the actual value is not
+ * required.
+ */
+ num squaredDistanceTo(Point other) {
+ var dx = x - other.x;
+ var dy = y - other.y;
+ return dx * dx + dy * dy;
+ }
+
+ Point ceil() => new Point(x.ceil(), y.ceil());
+ Point floor() => new Point(x.floor(), y.floor());
+ Point round() => new Point(x.round(), y.round());
+
+ /**
+ * Truncates x and y to integers and returns the result as a new point.
+ */
+ Point toInt() => new Point(x.toInt(), y.toInt());
+}

Powered by Google App Engine
This is Rietveld 408576698