4.) Reviewing the Vec2
Class
We didn't ask you to write the class for representing vectors, but you will be using it quite a bit in other assignments so it will be good to get at least a little familiar with how it works and what it can do.
The source for Vec2 is divided across its definition in anigraph/math/linalg/2D/Vec2.ts and the parent class VectorBase
definition in anigraph/math/linalg/VectorBase.ts.
Creating Vec2's:
The standard way to create a Vec2
is with its constructor, e.g.:
let xValue = 1;
let yValue = 2;
let myVec2 = new Vec2(xValue, yValue) // this creates the vector [1,2]
For convenience, you can also import the V2
helper function if you want:
import {Vec2, V2} from "__PATH_TO_VEC2__/Vec2.ts"
let xValue = 1;
let yValue = 2;
let myVec = V2(xValue, yValue); // this creates the vector [1,2]
Basic Properties
Classes that inherit from VectorBase
store their coordinates in an array called elements
.
elements: number[];
setElements(els: number[]) {
this.elements = els.slice(); // assigns a copy of the provided elements
return this;
}
Vec2
has getters and setters for x and y that acccess the corresponding members of elements
:
get x() {
return this.elements[0];
}
set x(val: number) {
this.elements[0] = val;
}
get y() {
return this.elements[1];
}
set y(val: number) {
this.elements[1] = val;
}
You can also refer to the number of dimensions in case you don't know whether a given object is a Vec2
or some other type of vector, but you probably won't need to worry about this for a while.
get nDimensions(){return 2;} // 2 for a Vec2
Arithmetic \& Comparisons
Unfortunately, Javascript doesn't have operator overloading. As such, we need functions to take the place of basic operators like +
, -
, *
, and ==
.
The full definitions for these functions are in the source, but to summarize what's available:
/**
* plus: returns a new vector with elements that are the sum of the current vector and an argument vector.
*/
plus(other: VectorType): this;
/**
* minus: returns a new vector with elements that are the values of the current vector minus the argument vector.
*/
minus(other: VectorType): this;
/**
* times: returns a new vector with the value of the current vector scaled by the provided constant.
*/
times(k: number): this;
/**
* timesElementWise: returns a new vector with elements that are the product of corresponding elements from this vector and the provided vector.
*/
timesElementWise(v: VectorBase): this;
/**
* dot: returns the dot product of this vector with the provided vector.
*/
dot(other: VectorType): number;
/**
* addVector: adds the provided vector to the current vector. Think of this as `+=`.
*/
addVector(other: VectorType): void;
/**
* subtractVector: subtracts the provided vector from the current vector. Think of this as `-=`.
*/
subtractVector(other: VectorType): void;
/**
* isEqualTo: Checks to see if the current vector and the provided vector are equal to within some optionally specified tolerance (margin of error).
*/
isEqualTo(other: VectorType, tolerance?: number): boolean;
Norm-y stuff
/**
* L2: Returns the L2 Norm of the vector. This is what we think of as its length. Why not call this length? Because the 'length' of the vector is easily confused with how many entries it has...
*/
L2(): number;
/**
* normalize: modifies the current vector so that it has an L2 norm (length) of 1.
*/
normalize(): void;
Makin' Copies
/**
* clone: Make a deep copy
*/
clone(): this;
/**
* getNormalized: returns a copy of the vector that has been normalized.
*/
getNormalized(): this;
Other
Other potentially useful functions...
/**
* Returns a copy of the vector where each entry is the output of a provided function performed on the corresponding entry of the current vector.
* Not required for course. See source if curious
*/
getMapped(fn: (e: number, i: number) => number, context?: any): this;
/**
* getRounded: get a copy that has its coordinates rounded.
* Not required for course. See source if curious
*/
getRounded(): this;
/**
* Not required for course. See source if curious
*/
forEach(fn: (e: number, i: number) => any, context: any): void;
/**
* Not required for course. See source if curious
*/
sstring(): string;
toJSON(): { [p: string]: any };