Vector2 class
2D column vector.
class Vector2 {
final Float32List storage = new Float32List(2);
/// Set the values of [result] to the minimum of [a] and [b] for each line.
static void min(Vector2 a, Vector2 b, Vector2 result) {
result.x = Math.min(a.x, b.x);
result.y = Math.min(a.y, b.y);
}
/// Set the values of [result] to the maximum of [a] and [b] for each line.
static void max(Vector2 a, Vector2 b, Vector2 result) {
result.x = Math.max(a.x, b.x);
result.y = Math.max(a.y, b.y);
}
/// Construct a new vector with the specified values.
Vector2(double x_, double y_) {
setValues(x_, y_);
}
/// Initialized with values from [array] starting at [offset].
Vector2.array(List<double> array, [int offset=0]) {
int i = offset;
storage[1] = array[i+1];
storage[0] = array[i+0];
}
/// Zero vector.
Vector2.zero();
/// Copy of [other].
Vector2.copy(Vector2 other) {
setFrom(other);
}
/// Set the values of the vector.
Vector2 setValues(double x_, double y_) {
storage[0] = x_;
storage[1] = y_;
return this;
}
/// Zero the vector.
Vector2 setZero() {
storage[0] = 0.0;
storage[1] = 0.0;
return this;
}
/// Set the values by copying them from [other].
Vector2 setFrom(Vector2 other) {
storage[1] = other.storage[1];
storage[0] = other.storage[0];
return this;
}
/// Splat [arg] into all lanes of the vector.
Vector2 splat(double arg) {
storage[0] = arg;
storage[1] = arg;
return this;
}
/// Returns a printable string
String toString() => '[${storage[0]},${storage[1]}]';
/// Negate.
Vector2 operator-() => new Vector2(-storage[0], -storage[1]);
/// Subtract two vectors.
Vector2 operator-(Vector2 other) => new Vector2(storage[0] - other.storage[0],
storage[1] - other.storage[1]);
/// Add two vectors.
Vector2 operator+(Vector2 other) => new Vector2(storage[0] + other.storage[0],
storage[1] + other.storage[1]);
/// Scale.
Vector2 operator/(double scale) {
var o = 1.0 / scale;
return new Vector2(storage[0] * o, storage[1] * o);
}
/// Scale.
Vector2 operator*(double scale) {
var o = scale;
return new Vector2(storage[0] * o, storage[1] * o);
}
double operator[](int i) => storage[i];
void operator[]=(int i, double v) { storage[i] = v; }
/// Length.
double get length {
double sum;
sum = (storage[0] * storage[0]);
sum += (storage[1] * storage[1]);
return Math.sqrt(sum);
}
/// Length squared.
double get length2 {
double sum;
sum = (storage[0] * storage[0]);
sum += (storage[1] * storage[1]);
return sum;
}
/// Normalize [this].
Vector2 normalize() {
double l = length;
// TODO(johnmccutchan): Use an epsilon.
if (l == 0.0) {
return this;
}
l = 1.0 / l;
storage[0] *= l;
storage[1] *= l;
return this;
}
/// Normalize [this]. Returns length of vector before normalization.
double normalizeLength() {
double l = length;
if (l == 0.0) {
return 0.0;
}
l = 1.0 / l;
storage[0] *= l;
storage[1] *= l;
return l;
}
/// Normalized copy of [this].
Vector2 normalized() {
return new Vector2.copy(this).normalize();
}
/// Normalize vector into [out].
Vector2 normalizeInto(Vector2 out) {
out.setFrom(this);
return out.normalize();
}
/// Inner product.
double dot(Vector2 other) {
double sum;
sum = storage[0] * other.storage[0];
sum += storage[1] * other.storage[1];
return sum;
}
/// Cross product.
double cross(Vector2 other) {
return storage[0] * other.storage[1] - storage[1] * other.storage[0];
}
/// Rotate [this] by 90 degrees then scale it. Store result in [out]. Return [out].
Vector2 scaleOrthogonalInto(double scale, Vector2 out) {
out.setValues(-scale * storage[1], scale * storage[0]);
return out;
}
/// Reflect [this].
Vector2 reflect(Vector2 normal) {
sub(normal.scaled(2 * normal.dot(this)));
return this;
}
/// Reflected copy of [this].
Vector2 reflected(Vector2 normal) {
return new Vector2.copy(this).reflect(normal);
}
/// Relative error between [this] and [correct]
double relativeError(Vector2 correct) {
double correct_norm = correct.length;
double diff_norm = (this - correct).length;
return diff_norm/correct_norm;
}
/// Absolute error between [this] and [correct]
double absoluteError(Vector2 correct) {
return (this - correct).length;
}
/// True if any component is infinite.
bool get isInfinite {
bool is_infinite = false;
is_infinite = is_infinite || storage[0].isInfinite;
is_infinite = is_infinite || storage[1].isInfinite;
return is_infinite;
}
/// True if any component is NaN.
bool get isNaN {
bool is_nan = false;
is_nan = is_nan || storage[0].isNaN;
is_nan = is_nan || storage[1].isNaN;
return is_nan;
}
/// Add [arg] to [this].
Vector2 add(Vector2 arg) {
storage[0] = storage[0] + arg.storage[0];
storage[1] = storage[1] + arg.storage[1];
return this;
}
/// Subtract [arg] from [this].
Vector2 sub(Vector2 arg) {
storage[0] = storage[0] - arg.storage[0];
storage[1] = storage[1] - arg.storage[1];
return this;
}
/// Multiply entries in [this] with entries in [arg].
Vector2 multiply(Vector2 arg) {
storage[0] = storage[0] * arg.storage[0];
storage[1] = storage[1] * arg.storage[1];
return this;
}
/// Divide entries in [this] with entries in [arg].
Vector2 divide(Vector2 arg) {
storage[0] = storage[0] / arg.storage[0];
storage[1] = storage[1] / arg.storage[1];
return this;
}
/// Scale [this].
Vector2 scale(double arg) {
storage[1] = storage[1] * arg;
storage[0] = storage[0] * arg;
return this;
}
Vector2 scaled(double arg) {
return clone().scale(arg);
}
/// Negate.
Vector2 negate() {
storage[1] = -storage[1];
storage[0] = -storage[0];
return this;
}
/// Absolute value.
Vector2 absolute() {
storage[1] = storage[1].abs();
storage[0] = storage[0].abs();
return this;
}
/// Clone of [this].
Vector2 clone() {
return new Vector2.copy(this);
}
/// Copy [this] into [arg]. Returns [arg].
Vector2 copyInto(Vector2 arg) {
arg.storage[1] = storage[1];
arg.storage[0] = storage[0];
return arg;
}
/// Copies [this] into [array] starting at [offset].
void copyIntoArray(List<double> array, [int offset=0]) {
array[offset+1] = storage[1];
array[offset+0] = storage[0];
}
/// Copies elements from [array] into [this] starting at [offset].
void copyFromArray(List<double> array, [int offset=0]) {
storage[1] = array[offset+1];
storage[0] = array[offset+0];
}
set xy(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
set yx(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
set r(double arg) => storage[0] = arg;
set g(double arg) => storage[1] = arg;
set s(double arg) => storage[0] = arg;
set t(double arg) => storage[1] = arg;
set x(double arg) => storage[0] = arg;
set y(double arg) => storage[1] = arg;
set rg(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
set gr(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
set st(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
set ts(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
Vector2 get xx => new Vector2(storage[0], storage[0]);
Vector2 get xy => new Vector2(storage[0], storage[1]);
Vector2 get yx => new Vector2(storage[1], storage[0]);
Vector2 get yy => new Vector2(storage[1], storage[1]);
Vector3 get xxx => new Vector3(storage[0], storage[0], storage[0]);
Vector3 get xxy => new Vector3(storage[0], storage[0], storage[1]);
Vector3 get xyx => new Vector3(storage[0], storage[1], storage[0]);
Vector3 get xyy => new Vector3(storage[0], storage[1], storage[1]);
Vector3 get yxx => new Vector3(storage[1], storage[0], storage[0]);
Vector3 get yxy => new Vector3(storage[1], storage[0], storage[1]);
Vector3 get yyx => new Vector3(storage[1], storage[1], storage[0]);
Vector3 get yyy => new Vector3(storage[1], storage[1], storage[1]);
Vector4 get xxxx => new Vector4(storage[0], storage[0], storage[0], storage[0]);
Vector4 get xxxy => new Vector4(storage[0], storage[0], storage[0], storage[1]);
Vector4 get xxyx => new Vector4(storage[0], storage[0], storage[1], storage[0]);
Vector4 get xxyy => new Vector4(storage[0], storage[0], storage[1], storage[1]);
Vector4 get xyxx => new Vector4(storage[0], storage[1], storage[0], storage[0]);
Vector4 get xyxy => new Vector4(storage[0], storage[1], storage[0], storage[1]);
Vector4 get xyyx => new Vector4(storage[0], storage[1], storage[1], storage[0]);
Vector4 get xyyy => new Vector4(storage[0], storage[1], storage[1], storage[1]);
Vector4 get yxxx => new Vector4(storage[1], storage[0], storage[0], storage[0]);
Vector4 get yxxy => new Vector4(storage[1], storage[0], storage[0], storage[1]);
Vector4 get yxyx => new Vector4(storage[1], storage[0], storage[1], storage[0]);
Vector4 get yxyy => new Vector4(storage[1], storage[0], storage[1], storage[1]);
Vector4 get yyxx => new Vector4(storage[1], storage[1], storage[0], storage[0]);
Vector4 get yyxy => new Vector4(storage[1], storage[1], storage[0], storage[1]);
Vector4 get yyyx => new Vector4(storage[1], storage[1], storage[1], storage[0]);
Vector4 get yyyy => new Vector4(storage[1], storage[1], storage[1], storage[1]);
double get r => storage[0];
double get g => storage[1];
double get s => storage[0];
double get t => storage[1];
double get x => storage[0];
double get y => storage[1];
Vector2 get rr => new Vector2(storage[0], storage[0]);
Vector2 get rg => new Vector2(storage[0], storage[1]);
Vector2 get gr => new Vector2(storage[1], storage[0]);
Vector2 get gg => new Vector2(storage[1], storage[1]);
Vector3 get rrr => new Vector3(storage[0], storage[0], storage[0]);
Vector3 get rrg => new Vector3(storage[0], storage[0], storage[1]);
Vector3 get rgr => new Vector3(storage[0], storage[1], storage[0]);
Vector3 get rgg => new Vector3(storage[0], storage[1], storage[1]);
Vector3 get grr => new Vector3(storage[1], storage[0], storage[0]);
Vector3 get grg => new Vector3(storage[1], storage[0], storage[1]);
Vector3 get ggr => new Vector3(storage[1], storage[1], storage[0]);
Vector3 get ggg => new Vector3(storage[1], storage[1], storage[1]);
Vector4 get rrrr => new Vector4(storage[0], storage[0], storage[0], storage[0]);
Vector4 get rrrg => new Vector4(storage[0], storage[0], storage[0], storage[1]);
Vector4 get rrgr => new Vector4(storage[0], storage[0], storage[1], storage[0]);
Vector4 get rrgg => new Vector4(storage[0], storage[0], storage[1], storage[1]);
Vector4 get rgrr => new Vector4(storage[0], storage[1], storage[0], storage[0]);
Vector4 get rgrg => new Vector4(storage[0], storage[1], storage[0], storage[1]);
Vector4 get rggr => new Vector4(storage[0], storage[1], storage[1], storage[0]);
Vector4 get rggg => new Vector4(storage[0], storage[1], storage[1], storage[1]);
Vector4 get grrr => new Vector4(storage[1], storage[0], storage[0], storage[0]);
Vector4 get grrg => new Vector4(storage[1], storage[0], storage[0], storage[1]);
Vector4 get grgr => new Vector4(storage[1], storage[0], storage[1], storage[0]);
Vector4 get grgg => new Vector4(storage[1], storage[0], storage[1], storage[1]);
Vector4 get ggrr => new Vector4(storage[1], storage[1], storage[0], storage[0]);
Vector4 get ggrg => new Vector4(storage[1], storage[1], storage[0], storage[1]);
Vector4 get gggr => new Vector4(storage[1], storage[1], storage[1], storage[0]);
Vector4 get gggg => new Vector4(storage[1], storage[1], storage[1], storage[1]);
Vector2 get ss => new Vector2(storage[0], storage[0]);
Vector2 get st => new Vector2(storage[0], storage[1]);
Vector2 get ts => new Vector2(storage[1], storage[0]);
Vector2 get tt => new Vector2(storage[1], storage[1]);
Vector3 get sss => new Vector3(storage[0], storage[0], storage[0]);
Vector3 get sst => new Vector3(storage[0], storage[0], storage[1]);
Vector3 get sts => new Vector3(storage[0], storage[1], storage[0]);
Vector3 get stt => new Vector3(storage[0], storage[1], storage[1]);
Vector3 get tss => new Vector3(storage[1], storage[0], storage[0]);
Vector3 get tst => new Vector3(storage[1], storage[0], storage[1]);
Vector3 get tts => new Vector3(storage[1], storage[1], storage[0]);
Vector3 get ttt => new Vector3(storage[1], storage[1], storage[1]);
Vector4 get ssss => new Vector4(storage[0], storage[0], storage[0], storage[0]);
Vector4 get ssst => new Vector4(storage[0], storage[0], storage[0], storage[1]);
Vector4 get ssts => new Vector4(storage[0], storage[0], storage[1], storage[0]);
Vector4 get sstt => new Vector4(storage[0], storage[0], storage[1], storage[1]);
Vector4 get stss => new Vector4(storage[0], storage[1], storage[0], storage[0]);
Vector4 get stst => new Vector4(storage[0], storage[1], storage[0], storage[1]);
Vector4 get stts => new Vector4(storage[0], storage[1], storage[1], storage[0]);
Vector4 get sttt => new Vector4(storage[0], storage[1], storage[1], storage[1]);
Vector4 get tsss => new Vector4(storage[1], storage[0], storage[0], storage[0]);
Vector4 get tsst => new Vector4(storage[1], storage[0], storage[0], storage[1]);
Vector4 get tsts => new Vector4(storage[1], storage[0], storage[1], storage[0]);
Vector4 get tstt => new Vector4(storage[1], storage[0], storage[1], storage[1]);
Vector4 get ttss => new Vector4(storage[1], storage[1], storage[0], storage[0]);
Vector4 get ttst => new Vector4(storage[1], storage[1], storage[0], storage[1]);
Vector4 get ttts => new Vector4(storage[1], storage[1], storage[1], storage[0]);
Vector4 get tttt => new Vector4(storage[1], storage[1], storage[1], storage[1]);
}
Static Methods
Constructors
new Vector2(double x_, double y_) #
Construct a new vector with the specified values.
Vector2(double x_, double y_) {
setValues(x_, y_);
}
new Vector2.array(List<double> array, [int offset = 0]) #
Initialized with values from array starting at offset.
Vector2.array(List<double> array, [int offset=0]) {
int i = offset;
storage[1] = array[i+1];
storage[0] = array[i+0];
}
new Vector2.zero() #
Zero vector.
Vector2.zero();
Properties
final Vector4 gggg #
Vector4 get gggg => new Vector4(storage[1], storage[1], storage[1], storage[1]);
final Vector4 gggr #
Vector4 get gggr => new Vector4(storage[1], storage[1], storage[1], storage[0]);
final Vector4 ggrg #
Vector4 get ggrg => new Vector4(storage[1], storage[1], storage[0], storage[1]);
final Vector4 ggrr #
Vector4 get ggrr => new Vector4(storage[1], storage[1], storage[0], storage[0]);
Vector2 gr #
Vector2 get gr => new Vector2(storage[1], storage[0]);
set gr(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
final Vector4 grgg #
Vector4 get grgg => new Vector4(storage[1], storage[0], storage[1], storage[1]);
final Vector4 grgr #
Vector4 get grgr => new Vector4(storage[1], storage[0], storage[1], storage[0]);
final Vector4 grrg #
Vector4 get grrg => new Vector4(storage[1], storage[0], storage[0], storage[1]);
final Vector4 grrr #
Vector4 get grrr => new Vector4(storage[1], storage[0], storage[0], storage[0]);
final bool isInfinite #
True if any component is infinite.
bool get isInfinite {
bool is_infinite = false;
is_infinite = is_infinite || storage[0].isInfinite;
is_infinite = is_infinite || storage[1].isInfinite;
return is_infinite;
}
final bool isNaN #
True if any component is NaN.
bool get isNaN {
bool is_nan = false;
is_nan = is_nan || storage[0].isNaN;
is_nan = is_nan || storage[1].isNaN;
return is_nan;
}
final double length #
Length.
double get length {
double sum;
sum = (storage[0] * storage[0]);
sum += (storage[1] * storage[1]);
return Math.sqrt(sum);
}
final double length2 #
Length squared.
double get length2 {
double sum;
sum = (storage[0] * storage[0]);
sum += (storage[1] * storage[1]);
return sum;
}
Vector2 rg #
Vector2 get rg => new Vector2(storage[0], storage[1]);
set rg(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
final Vector4 rggg #
Vector4 get rggg => new Vector4(storage[0], storage[1], storage[1], storage[1]);
final Vector4 rggr #
Vector4 get rggr => new Vector4(storage[0], storage[1], storage[1], storage[0]);
final Vector4 rgrg #
Vector4 get rgrg => new Vector4(storage[0], storage[1], storage[0], storage[1]);
final Vector4 rgrr #
Vector4 get rgrr => new Vector4(storage[0], storage[1], storage[0], storage[0]);
final Vector4 rrgg #
Vector4 get rrgg => new Vector4(storage[0], storage[0], storage[1], storage[1]);
final Vector4 rrgr #
Vector4 get rrgr => new Vector4(storage[0], storage[0], storage[1], storage[0]);
final Vector4 rrrg #
Vector4 get rrrg => new Vector4(storage[0], storage[0], storage[0], storage[1]);
final Vector4 rrrr #
Vector4 get rrrr => new Vector4(storage[0], storage[0], storage[0], storage[0]);
final Vector4 ssss #
Vector4 get ssss => new Vector4(storage[0], storage[0], storage[0], storage[0]);
final Vector4 ssst #
Vector4 get ssst => new Vector4(storage[0], storage[0], storage[0], storage[1]);
final Vector4 ssts #
Vector4 get ssts => new Vector4(storage[0], storage[0], storage[1], storage[0]);
final Vector4 sstt #
Vector4 get sstt => new Vector4(storage[0], storage[0], storage[1], storage[1]);
Vector2 st #
Vector2 get st => new Vector2(storage[0], storage[1]);
set st(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
final Float32List storage #
final Float32List storage = new Float32List(2)
final Vector4 stss #
Vector4 get stss => new Vector4(storage[0], storage[1], storage[0], storage[0]);
final Vector4 stst #
Vector4 get stst => new Vector4(storage[0], storage[1], storage[0], storage[1]);
final Vector4 stts #
Vector4 get stts => new Vector4(storage[0], storage[1], storage[1], storage[0]);
final Vector4 sttt #
Vector4 get sttt => new Vector4(storage[0], storage[1], storage[1], storage[1]);
Vector2 ts #
Vector2 get ts => new Vector2(storage[1], storage[0]);
set ts(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
final Vector4 tsss #
Vector4 get tsss => new Vector4(storage[1], storage[0], storage[0], storage[0]);
final Vector4 tsst #
Vector4 get tsst => new Vector4(storage[1], storage[0], storage[0], storage[1]);
final Vector4 tsts #
Vector4 get tsts => new Vector4(storage[1], storage[0], storage[1], storage[0]);
final Vector4 tstt #
Vector4 get tstt => new Vector4(storage[1], storage[0], storage[1], storage[1]);
final Vector4 ttss #
Vector4 get ttss => new Vector4(storage[1], storage[1], storage[0], storage[0]);
final Vector4 ttst #
Vector4 get ttst => new Vector4(storage[1], storage[1], storage[0], storage[1]);
final Vector4 ttts #
Vector4 get ttts => new Vector4(storage[1], storage[1], storage[1], storage[0]);
final Vector4 tttt #
Vector4 get tttt => new Vector4(storage[1], storage[1], storage[1], storage[1]);
final Vector4 xxxx #
Vector4 get xxxx => new Vector4(storage[0], storage[0], storage[0], storage[0]);
final Vector4 xxxy #
Vector4 get xxxy => new Vector4(storage[0], storage[0], storage[0], storage[1]);
final Vector4 xxyx #
Vector4 get xxyx => new Vector4(storage[0], storage[0], storage[1], storage[0]);
final Vector4 xxyy #
Vector4 get xxyy => new Vector4(storage[0], storage[0], storage[1], storage[1]);
Vector2 xy #
Vector2 get xy => new Vector2(storage[0], storage[1]);
set xy(Vector2 arg) {
storage[0] = arg.storage[0];
storage[1] = arg.storage[1];
}
final Vector4 xyxx #
Vector4 get xyxx => new Vector4(storage[0], storage[1], storage[0], storage[0]);
final Vector4 xyxy #
Vector4 get xyxy => new Vector4(storage[0], storage[1], storage[0], storage[1]);
final Vector4 xyyx #
Vector4 get xyyx => new Vector4(storage[0], storage[1], storage[1], storage[0]);
final Vector4 xyyy #
Vector4 get xyyy => new Vector4(storage[0], storage[1], storage[1], storage[1]);
Vector2 yx #
Vector2 get yx => new Vector2(storage[1], storage[0]);
set yx(Vector2 arg) {
storage[1] = arg.storage[0];
storage[0] = arg.storage[1];
}
final Vector4 yxxx #
Vector4 get yxxx => new Vector4(storage[1], storage[0], storage[0], storage[0]);
final Vector4 yxxy #
Vector4 get yxxy => new Vector4(storage[1], storage[0], storage[0], storage[1]);
final Vector4 yxyx #
Vector4 get yxyx => new Vector4(storage[1], storage[0], storage[1], storage[0]);
final Vector4 yxyy #
Vector4 get yxyy => new Vector4(storage[1], storage[0], storage[1], storage[1]);
final Vector4 yyxx #
Vector4 get yyxx => new Vector4(storage[1], storage[1], storage[0], storage[0]);
final Vector4 yyxy #
Vector4 get yyxy => new Vector4(storage[1], storage[1], storage[0], storage[1]);
Operators
Vector2 operator +(Vector2 other) #
Add two vectors.
Vector2 operator+(Vector2 other) => new Vector2(storage[0] + other.storage[0],
storage[1] + other.storage[1]);
Vector2 operator -(Vector2 other) #
Subtract two vectors.
Vector2 operator-(Vector2 other) => new Vector2(storage[0] - other.storage[0],
storage[1] - other.storage[1]);
Methods
Vector2 absolute() #
Absolute value.
Vector2 absolute() {
storage[1] = storage[1].abs();
storage[0] = storage[0].abs();
return this;
}
double absoluteError(Vector2 correct) #
Absolute error between this and
correct
double absoluteError(Vector2 correct) {
return (this - correct).length;
}
Vector2 add(Vector2 arg) #
Add
arg to this.
Vector2 add(Vector2 arg) {
storage[0] = storage[0] + arg.storage[0];
storage[1] = storage[1] + arg.storage[1];
return this;
}
void copyFromArray(List<double> array, [int offset = 0]) #
Copies elements from
array into this starting at
offset.
void copyFromArray(List<double> array, [int offset=0]) {
storage[1] = array[offset+1];
storage[0] = array[offset+0];
}
Vector2 copyInto(Vector2 arg) #
Copy this into
arg. Returns
arg.
Vector2 copyInto(Vector2 arg) {
arg.storage[1] = storage[1];
arg.storage[0] = storage[0];
return arg;
}
void copyIntoArray(List<double> array, [int offset = 0]) #
Copies this into
array starting at
offset.
void copyIntoArray(List<double> array, [int offset=0]) {
array[offset+1] = storage[1];
array[offset+0] = storage[0];
}
double cross(Vector2 other) #
Cross product.
double cross(Vector2 other) {
return storage[0] * other.storage[1] - storage[1] * other.storage[0];
}
Vector2 divide(Vector2 arg) #
Divide entries in this with entries in
arg.
Vector2 divide(Vector2 arg) {
storage[0] = storage[0] / arg.storage[0];
storage[1] = storage[1] / arg.storage[1];
return this;
}
double dot(Vector2 other) #
Inner product.
double dot(Vector2 other) {
double sum;
sum = storage[0] * other.storage[0];
sum += storage[1] * other.storage[1];
return sum;
}
Vector2 multiply(Vector2 arg) #
Multiply entries in this with entries in
arg.
Vector2 multiply(Vector2 arg) {
storage[0] = storage[0] * arg.storage[0];
storage[1] = storage[1] * arg.storage[1];
return this;
}
Vector2 negate() #
Negate.
Vector2 negate() {
storage[1] = -storage[1];
storage[0] = -storage[0];
return this;
}
Vector2 normalize() #
Normalize this.
Vector2 normalize() {
double l = length;
// TODO(johnmccutchan): Use an epsilon.
if (l == 0.0) {
return this;
}
l = 1.0 / l;
storage[0] *= l;
storage[1] *= l;
return this;
}
Vector2 normalized() #
Normalized copy of this.
Vector2 normalized() {
return new Vector2.copy(this).normalize();
}
Vector2 normalizeInto(Vector2 out) #
Normalize vector into out.
Vector2 normalizeInto(Vector2 out) {
out.setFrom(this);
return out.normalize();
}
double normalizeLength() #
Normalize this. Returns length of vector before normalization.
double normalizeLength() {
double l = length;
if (l == 0.0) {
return 0.0;
}
l = 1.0 / l;
storage[0] *= l;
storage[1] *= l;
return l;
}
Vector2 reflect(Vector2 normal) #
Reflect this.
Vector2 reflect(Vector2 normal) {
sub(normal.scaled(2 * normal.dot(this)));
return this;
}
Vector2 reflected(Vector2 normal) #
Reflected copy of this.
Vector2 reflected(Vector2 normal) {
return new Vector2.copy(this).reflect(normal);
}
double relativeError(Vector2 correct) #
Relative error between this and
correct
double relativeError(Vector2 correct) {
double correct_norm = correct.length;
double diff_norm = (this - correct).length;
return diff_norm/correct_norm;
}
Vector2 scale(double arg) #
Scale this.
Vector2 scale(double arg) {
storage[1] = storage[1] * arg;
storage[0] = storage[0] * arg;
return this;
}
Vector2 scaleOrthogonalInto(double scale, Vector2 out) #
Rotate this by 90 degrees then scale it. Store result in
out. Return
out.
Vector2 scaleOrthogonalInto(double scale, Vector2 out) {
out.setValues(-scale * storage[1], scale * storage[0]);
return out;
}
Vector2 setFrom(Vector2 other) #
Set the values by copying them from other.
Vector2 setFrom(Vector2 other) {
storage[1] = other.storage[1];
storage[0] = other.storage[0];
return this;
}
Vector2 setValues(double x_, double y_) #
Set the values of the vector.
Vector2 setValues(double x_, double y_) {
storage[0] = x_;
storage[1] = y_;
return this;
}
Vector2 setZero() #
Zero the vector.
Vector2 setZero() {
storage[0] = 0.0;
storage[1] = 0.0;
return this;
}
Vector2 splat(double arg) #
Splat arg into all lanes of the vector.
Vector2 splat(double arg) {
storage[0] = arg;
storage[1] = arg;
return this;
}