Vector class
Class reperesenting a 2d vector.
class Vector extends Coordinate { const Vector([num x = 0, num y = 0]) : super(x,y); /** * Computes the length of this [Vector]. **/ num get length => math.sqrt(x * x + y * y); Vector get normal => this.scale(1 / this.length); /** * Adds a [Coordinate] and returns the result as new [Vector]. **/ Vector operator +(Coordinate other) => new Vector(x + other.x, y + other.y); /** * Multiplies each dimensions by the provided magnitude and returns a new [Vector]. **/ Vector operator *(num magnitude) => this.scale(magnitude); /** * Multiplies each dimensions by the provided magnitude and returns a new [Vector]. **/ Vector scale(num magnitude) => new Vector(x * magnitude, y * magnitude); /** * Computes the dot product with the given [Vector]. **/ num dot(Vector other) => x * other.x + y * other.y; /** * Computes the cross product with the given [Vector]. **/ num cross(Vector other) => x * other.y - y * other.x; /** * Computes the angle between this and another [Vector]. **/ num getAngle (Vector other) => math.acos(dot(other)); Vector rotate(num angle) { var cos = math.cos(angle); var sin = math.sin(angle); var newX = this.x * cos - this.y * sin; var newY = this.y * cos + this.x * sin; return new Vector(newX, newY); } Vector rotateAroundPoint(Coordinate axisPoint, num angle) { return (this - axisPoint).rotate(angle) + axisPoint; } }
Extends
Coordinate > Vector
Constructors
const Vector([num x = 0, num y = 0]) #
const Vector([num x = 0, num y = 0]) : super(x,y);
Properties
final bool isValid #
bool get isValid => isValidNumber(x) && isValidNumber(y);
final Type runtimeType #
A representation of the runtime type of the object.
external Type get runtimeType;
Operators
Vector operator +(Coordinate other) #
Adds a Coordinate and returns the result as new Vector.
Vector operator +(Coordinate other) => new Vector(x + other.x, y + other.y);
Vector operator -(Coordinate other) #
Subtract a Coordinate and returns the result as new Vector.
Vector operator -(Coordinate other) => difference(this, other);
Vector operator *(num magnitude) #
Multiplies each dimensions by the provided magnitude and returns a new Vector.
Vector operator *(num magnitude) => this.scale(magnitude);
bool operator ==(Coordinate other) #
The equality operator.
The default behavior for all Object
s is to return true if and
only if this
and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode
method as well to maintain consistency.
bool operator ==(Coordinate other) { return other != null && x == other.x && y == other.y; }
Methods
const Coordinate([num x = 0, num y = 0]) #
const Coordinate([this.x = 0, this.y = 0]);
num cross(Vector other) #
Computes the cross product with the given Vector.
num cross(Vector other) => x * other.y - y * other.x;
num dot(Vector other) #
Computes the dot product with the given Vector.
num dot(Vector other) => x * other.x + y * other.y;
num getAngle(Vector other) #
Computes the angle between this and another Vector.
num getAngle (Vector other) => math.acos(dot(other));
num getDistance(Coordinate other) #
Computes the distance to another Coordinate.
num getDistance (Coordinate other) => (this - other).length;
int hashCode() #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==
. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode
it should override the
equality operator as well to maintain consistency.
external int hashCode();
noSuchMethod(String name, List args) #
noSuchMethod
is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod
. If noSuchMethod
returns a value, that value becomes the result of the original
invocation.
The default behavior of noSuchMethod
is to throw a
noSuchMethodError
.
external Dynamic noSuchMethod(String name, List args);
const Object() #
Creates a new Object
instance.
Object
instances have no meaningful state, and are only useful
through their identity. An Object
instance is equal to itself
only.
const Object();
Vector rotate(num angle) #
Vector rotate(num angle) { var cos = math.cos(angle); var sin = math.sin(angle); var newX = this.x * cos - this.y * sin; var newY = this.y * cos + this.x * sin; return new Vector(newX, newY); }
Vector rotateAroundPoint(Coordinate axisPoint, num angle) #
Vector rotateAroundPoint(Coordinate axisPoint, num angle) { return (this - axisPoint).rotate(angle) + axisPoint; }
Vector scale(num magnitude) #
Multiplies each dimensions by the provided magnitude and returns a new Vector.
Vector scale(num magnitude) => new Vector(x * magnitude, y * magnitude);
String toString() #
Returns a string representation of this object.
String toString() => '{x:${x}, y:${y}}';
const Vector([num x = 0, num y = 0]) #
const Vector([num x = 0, num y = 0]) : super(x,y);