Copy folder
This commit is contained in:
commit
f40d182e88
7 changed files with 343 additions and 0 deletions
13
js/vec2.js
Normal file
13
js/vec2.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
const magnitude = ([x, y]) => Math.sqrt(x * x + y * y);
|
||||
|
||||
const add = ([x1, y1], [x2, y2]) => [x1 + x2, y1 + y2];
|
||||
|
||||
const subtract = ([x1, y1], [x2, y2]) => [x1 - x2, y1 - y2];
|
||||
|
||||
const limit = ([x, y], maxMagnitude) => {
|
||||
const m = magnitude([x, y]);
|
||||
if (m <= maxMagnitude) return [x, y];
|
||||
|
||||
const scalingFactor = maxMagnitude / m;
|
||||
return [x * scalingFactor, y * scalingFactor];
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue