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

Unified Diff: sdk/lib/math/point.dart

Issue 25808002: Move Rectangle and Point into dart:math. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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: sdk/lib/math/point.dart
diff --git a/sdk/lib/math/point.dart b/sdk/lib/math/point.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d7757f3771050046c8096c26499c27c95a5c8504
--- /dev/null
+++ b/sdk/lib/math/point.dart
@@ -0,0 +1,80 @@
+// 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 dart.math;
+
+/**
+ * A utility class for representing two-dimensional positions.
+ */
+class Point<T extends num> {
+ final T x;
+ final T y;
+
+ const Point([T x = 0, T y = 0]): this.x = x, this.y = y;
+
+ String toString() => '($x, $y)';
Lasse Reichstein Nielsen 2013/10/03 06:24:03 I wouldn't mind it saying "Point($x, $y)". Just th
Emily Fortuna 2013/10/03 21:29:12 Done.
+
+ bool operator ==(other) {
+ if (other is !Point) return false;
+ return x == other.x && y == other.y;
+ }
+
+ int get hashCode => _JenkinsSmiHash.hash2(x.hashCode, y.hashCode);
+
+ Point<T> operator +(Point<T> other) {
Lasse Reichstein Nielsen 2013/10/03 06:24:03 These are really Vector operations, not Point oper
Emily Fortuna 2013/10/03 21:29:12 Done.
+ return new Point<T>(x + other.x, y + other.y);
+ }
+
+ Point<T> operator -(Point<T> other) {
+ return new Point<T>(x - other.x, y - other.y);
+ }
+
+ /**
+ * Scale this point by [factor] as if it were a vector.
floitsch 2013/10/03 09:33:50 if it was a vector
Emily Fortuna 2013/10/03 21:29:12 Done.
+ *
+ * *Important* *Note*: This function accepts a `num` as its argument only so
+ * that you can scale Point<double> objects by an `int` factor. Because the
Lasse Reichstein Nielsen 2013/10/03 06:24:03 I don't particularly like this, but I guess it is
Emily Fortuna 2013/10/03 21:29:12 No Florian and I don't weren't thrilled with it ei
+ * star operator always returns the same type of Point that originally called
+ * it, passing in a double [factor] on a Point<int> _will_ _cause_ _a_
Lasse Reichstein Nielsen 2013/10/03 06:24:03 `Point<int>`
floitsch 2013/10/03 09:33:50 No use for future: _causes a runtime error_ in che
Emily Fortuna 2013/10/03 21:29:12 Done.
Emily Fortuna 2013/10/03 21:29:12 Done.
+ * _runtime_ _error_ in checked mode.
+ */
+ Point<T> operator *(num factor) {
+ return new Point<T>(x * factor, y * factor);
+ }
+
+ /**
+ * Get the straight line (Euclidean) distance between the origin (0, 0) and
+ * this point.
+ */
+ double get magnitude => sqrt(x * x + y * y);
+
+ /**
+ * Returns the distance between `this` and [other].
+ */
+ double distanceTo(Point<T> other) {
+ var dx = x - other.x;
+ var dy = y - other.y;
+ return sqrt(dx * dx + dy * dy);
+ }
+
+ /**
+ * Returns the squared distance between `this` and [other].
+ *
+ * Squared distances can be used for comparisons when the actual value is not
+ * required.
+ */
+ T squaredDistanceTo(Point<T> other) {
+ var dx = x - other.x;
+ var dy = y - other.y;
+ return dx * dx + dy * dy;
+ }
+
Lasse Reichstein Nielsen 2013/10/03 06:24:03 Needs documentation.
Emily Fortuna 2013/10/03 21:29:12 Done.
+ Point<int> ceil() => new Point<int>(x.ceil(), y.ceil());
+ Point<int> floor() => new Point<int>(x.floor(), y.floor());
+ Point<int> round() => new Point<int>(x.round(), y.round());
+
+ /**
+ * Truncates x and y to integers and returns the result as a new point.
+ */
+ Point<int> truncate() => new Point<int>(x.toInt(), y.toInt());
+}

Powered by Google App Engine
This is Rietveld 408576698