Appearance
Point<T extends num>
class Point<T extends num>A utility class for representing two-dimensional positions.
Example:
dart
var leftTop = const Point(0, 0);
var rightBottom = const Point(200, 400);Legacy: New usages of Point are discouraged.
- If you are using the
Pointclass withdart:html, we recommend migrating topackage:web. To learn how and why to migrate, check out the migration guide. - If you want to combine an
xandycoordinate, consider using a record. Depending on how you will use it, this could look likevar point = (x, y)orvar point = (x: x, y: y). - If you want to perform vector operations, like vector addition or scalar multiplication, consider using a dedicated vector math library, such as
package:vector_math. - If you are developing a Flutter application or package, consider using the
Offsettype fromdart:ui.
Constructors
Point() const
const Point(T x, T y)Creates a point with the provided x and y coordinates.
Legacy: New usages of Point are discouraged. To learn more, check out the Point class API docs.
Implementation
dart
const Point(T x, T y) : this.x = x, this.y = y;Properties
hashCode no setter override
int get hashCodeThe hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Implementation
dart
int get hashCode => SystemHash.hash2(x.hashCode, y.hashCode, 0);magnitude no setter
double get magnitudeGet the straight line (Euclidean) distance between the origin (0, 0) and this point.
Example:
dart
var magnitude = const Point(0, 0).magnitude; // 0.0
magnitude = const Point(10, 0).magnitude; // 10.0
magnitude = const Point(0, -10).magnitude; // 10.0
magnitude = const Point(10, 10).magnitude; // 14.142135623730951Implementation
dart
double get magnitude => sqrt(x * x + y * y);runtimeType no setter inherited
Type get runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
dart
external Type get runtimeType;x final
final T xImplementation
dart
final T x;y final
final T yImplementation
dart
final T y;Methods
distanceTo()
Returns the distance between this and other.
dart
var distanceTo = const Point(0, 0).distanceTo(const Point(0, 0)); // 0.0
distanceTo = const Point(0, 0).distanceTo(const Point(10, 0)); // 10.0
distanceTo = const Point(0, 0).distanceTo(const Point(0, -10)); // 10.0
distanceTo = const Point(-10, 0).distanceTo(const Point(100, 0)); // 110.0Implementation
dart
double distanceTo(Point<T> other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}noSuchMethod() inherited
dynamic noSuchMethod(Invocation invocation)Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
dart
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);squaredDistanceTo()
T squaredDistanceTo(Point<T> other)Returns the squared distance between this and other.
Squared distances can be used for comparisons when the actual value is not required.
Example:
dart
var squaredDistance =
const Point(0, 0).squaredDistanceTo(const Point(0, 0)); // 0.0
squaredDistance =
const Point(0, 0).squaredDistanceTo(const Point(10, 0)); // 100
squaredDistance =
const Point(0, 0).squaredDistanceTo(const Point(0, -10)); // 100
squaredDistance =
const Point(-10, 0).squaredDistanceTo(const Point(100, 0)); // 12100Implementation
dart
T squaredDistanceTo(Point<T> other) {
var dx = x - other.x;
var dy = y - other.y;
return (dx * dx + dy * dy) as T;
}toString() override
String toString()A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Implementation
dart
String toString() => 'Point($x, $y)';Operators
operator *()
Scale this point by factor as if it were a vector.
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 * operator always returns the same type of Point as it is called on, passing in a double factor on a Point<int> causes aruntime error.
Example:
dart
// Integer values.
var point = const Point(10, 100) * 10; // Point(100, 1000)
point = const Point(-10, -100) * 5; // Point(-50, -500)
// Double values.
var doublePoint = Point(10.0, 100.0) * 1.5; // Point(15.0, 150.0)
// Runtime error due the invalid type cast.
var newPoint = const Point(10, 100) * 1.5; // Throws.Implementation
dart
Point<T> operator *(num /*T|int*/ factor) {
return Point<T>((x * factor) as T, (y * factor) as T);
}operator +()
Add other to this, as if both points were vectors.
Returns the resulting "vector" as a Point.
Example:
dart
var point = const Point(10, 100) + const Point(10, 10); // Point(20, 110)
point = const Point(-10, -20) + const Point(10, 100); // Point(0, 80)Implementation
dart
Point<T> operator +(Point<T> other) {
return Point<T>((x + other.x) as T, (y + other.y) as T);
}operator -()
Subtract other from this, as if both points were vectors.
Returns the resulting "vector" as a Point.
Example:
dart
var point = const Point(10, 100) - const Point(10, 10); // Point(0, 90)
point = const Point(-10, -20) - const Point(100, 100); // Point(-110, -120)Implementation
dart
Point<T> operator -(Point<T> other) {
return Point<T>((x - other.x) as T, (y - other.y) as T);
}operator ==() override
Whether other is a point with the same coordinates as this point.
Returns true if other is a Point with x and y coordinates equal to the corresponding coordinates of this point, and false otherwise.
Example:
dart
var result = const Point(0, 0) == const Point(0, 0); // true
result = const Point(1.0, 0) == const Point(-1.0, 0); // falseImplementation
dart
bool operator ==(Object other) =>
other is Point && x == other.x && y == other.y;