UNPKG

lr-core

Version:
127 lines (118 loc) 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.len = len; exports.lenSq = lenSq; exports.angle = angle; exports.angleTo = angleTo; exports.dist = dist; exports.distSq = distSq; exports.dot = dot; exports.cross = cross; exports.equals = equals; /** * @typedef Vec2 * * @property {number} x - X component * @property {number} y - Y component */ function len(v) { return Math.sqrt(v.x * v.x + v.y * v.y); } function lenSq(v) { return v.x * v.x + v.y * v.y; } function angle(v) { return Math.atan2(v.y, v.x); } function angleTo(v, u) { return angle(u) - angle(v); } function dist(v, u) { const dx = u.x - v.x; const dy = u.y - v.y; return Math.sqrt(dx * dx + dy * dy); } function distSq(v, u) { const dx = u.x - v.x; const dy = u.y - v.y; return dx * dx + dy * dy; } function dot(v, u) { return v.x * u.x + v.y * u.y; } function cross(v, u) { return v.x * u.y - v.y * u.x; } function equals(v, u) { return v.x === u.x && v.y === u.y; } const V2Functions = { len, lenSq, angle, angleTo, dist, distSq, dot, cross, equals }; const V2Methods = { /* mutating methods */ add(v) { this.x += v.x; this.y += v.y; return this; }, sub(v) { this.x -= v.x; this.y -= v.y; return this; }, mul(s) { this.x *= s; this.y *= s; return this; }, div(s) { this.x /= s; this.y /= s; return this; }, norm() { this.div(this.len()); return this; }, // X axis → // Y axis ↓ // rotates clockwise rot(rads) { const cos = Math.cos(rads); const sin = Math.sin(rads); const x = this.x; const y = this.y; this.x = x * cos - y * sin; this.y = x * sin + y * cos; return this; }, rotCW() { const x = this.x; const y = this.y; this.x = -y; this.y = x; return this; }, rotCCW() { const x = this.x; const y = this.y; this.x = y; this.y = -x; return this; } }; for (let key in V2Functions) { let fn = V2Functions[key]; V2Methods[key] = function (v) { return fn(this, v); }; } function V2(v) { let u = Object.create(V2Methods); u.x = v.x; u.y = v.y; return u; } Object.assign(V2, V2Functions); exports.default = V2;