A pair of numbers [x, y] representing for example:
[x, y]
See Vec2 for full library.
Note that mutating Vector2 is allowed, but discouraged except for memory-demanding situations.
position[0]
position
const onionSize: Vector2 = [width, height]; Copy
const onionSize: Vector2 = [width, height];
const [width, height] = onionSize; Copy
const [width, height] = onionSize;
x += dx * speed;y += dy * speed;// becomes...pos = Vec2.add(pos, Vec2.mul(delta, speed)) Copy
x += dx * speed;y += dy * speed;// becomes...pos = Vec2.add(pos, Vec2.mul(delta, speed))
const vecs: Vector2[] = [[50, 100], [-5, -5], [0, 99]];const sum = vecs.reduce(Vec2.add); Copy
const vecs: Vector2[] = [[50, 100], [-5, -5], [0, 99]];const sum = vecs.reduce(Vec2.add);
// Draw a circle in the center of the canvas.const pos = Vec2.div( like.canvas.getSize(), 2,)like.graphics.circle('fill', 'blue', pos, 20); Copy
// Draw a circle in the center of the canvas.const pos = Vec2.div( like.canvas.getSize(), 2,)like.graphics.circle('fill', 'blue', pos, 20);
Vector2
const upperLeft = myVector2List.reduce(Vec2.min);const lowerRight = myVector2List.reduce(Vec2.max);const boundingBox = Rect.fromPoints(upperLeft, lowerRight); Copy
const upperLeft = myVector2List.reduce(Vec2.min);const lowerRight = myVector2List.reduce(Vec2.max);const boundingBox = Rect.fromPoints(upperLeft, lowerRight);
const squareVec2 = Vec2.map(a: number => a**2);squareVec2([6, 7]) // returns [36, 49]// one in one line...Vec2.map(a: number => a**2)([6, 7]); Copy
const squareVec2 = Vec2.map(a: number => a**2);squareVec2([6, 7]) // returns [36, 49]// one in one line...Vec2.map(a: number => a**2)([6, 7]);
A pair of numbers
[x, y]representing for example:See Vec2 for full library.
Note that mutating Vector2 is allowed, but discouraged except for memory-demanding situations.
position[0]has clunky syntax compared to the librarypositionwill change their value.Examples
Constructing a Vector2
Deconstructing a Vector2
Making math less repetitive.
Summing an array of Vector2
Using LIKE graphics API
Getting the bounding box of an array of
Vector2sSquaring each element of a Vector2