24 lines
508 B
JavaScript
24 lines
508 B
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
const root = window.PixelIslandModules ||= {};
|
|
|
|
root.Vec2 = {
|
|
add(a, b) {
|
|
return { x: a.x + b.x, y: a.y + b.y };
|
|
},
|
|
sub(a, b) {
|
|
return { x: a.x - b.x, y: a.y - b.y };
|
|
},
|
|
scale(v, amount) {
|
|
return { x: v.x * amount, y: v.y * amount };
|
|
},
|
|
length(v) {
|
|
return Math.hypot(v.x, v.y);
|
|
},
|
|
normalize(v) {
|
|
const length = Math.hypot(v.x, v.y) || 1;
|
|
return { x: v.x / length, y: v.y / length };
|
|
}
|
|
};
|
|
})();
|