@rawify/vector2
Version:
The RAW JavaScript 2D Vector library
14 lines (12 loc) • 3.12 kB
JavaScript
/*
Vector2 v0.0.6 8/16/2025
https://github.com/rawify/Vector2.js
Copyright (c) 2025, Robert Eisele (https://raw.org/)
Licensed under the MIT license.
*/
'use strict';(function(g){function d(a,b){const c=Object.create(e.prototype);c.x=a;c.y=b;return c}function e(a,b){let c=this instanceof e?this:Object.create(e.prototype);if("number"===typeof a&&"number"===typeof b)return c.x=a,c.y=b,c;a&&"object"===typeof a&&(Array.isArray(a)?(c.x=a[0],c.y=a[1]):(c.x=a.x,c.y=a.y));return c}e.prototype={x:0,y:0,add:function(a){return d(this.x+a.x,this.y+a.y)},sub:function(a){return d(this.x-a.x,this.y-a.y)},neg:function(){return d(-this.x,-this.y)},scale:function(a){return d(this.x*
a,this.y*a)},prod:function(a){return d(this.x*a.x,this.y*a.y)},dot:function(a){return this.x*a.x+this.y*a.y},cross:function(a){return this.x*a.y-this.y*a.x},perp:function(){return d(-this.y,this.x)},projectTo:function(a){const b=a.x;a=a.y;const c=(this.x*b+this.y*a)/(b*b+a*a);return d(b*c,a*c)},rejectFrom:function(a){const b=a.x;a=a.y;const c=(this.x*b+this.y*a)/(b*b+a*a);return d(this.x-b*c,this.y-a*c)},reflect:function(a){const b=a.x;a=a.y;const c=2*(this.x*b+this.y*a)/(b*b+a*a);return d(c*b-this.x,
c*a-this.y)},refract:function(a,b){var c=this.x*a.x+this.y*a.y;const f=1-b*b*(1-c*c);if(0>f)return null;c=b*c+Math.sqrt(f);return d(b*this.x-c*a.x,b*this.y-c*a.y)},angle:function(){return Math.atan2(this.y,this.x)},norm:function(){const a=this.x,b=this.y;return Math.sqrt(a*a+b*b)},norm2:function(){const a=this.x,b=this.y;return a*a+b*b},normalize:function(){const a=this.x,b=this.y;var c=a*a+b*b;if(0===c||1===c)return this;c=1/Math.sqrt(c);return d(a*c,b*c)},distance:function(a){const b=this.x-a.x;
a=this.y-a.y;return Math.sqrt(b*b+a*a)},set:function(a){this.x=a.x;this.y=a.y},rotate:function(a){const b=Math.cos(a);a=Math.sin(a);const c=this.x,f=this.y;return d(b*c-a*f,a*c+b*f)},apply:function(a,b={x:0,y:0}){return d(a(this.x,b.x),a(this.y,b.y))},toArray:function(){return[this.x,this.y]},clone:function(){return d(this.x,this.y)},equals:function(a){return this===a||1E-13>Math.abs(this.x-a.x)&&1E-13>Math.abs(this.y-a.y)},isParallel:function(a){return 1E-13>Math.abs(this.cross(a))},isUnit:function(){return 1E-13>
Math.abs(this.x*this.x+this.y*this.y-1)},lerp:function(a,b){const c=this.x,f=this.y;return d(c+b*(a.x-c),f+b*(a.y-f))},toString:function(){return"("+this.x+", "+this.y+")"},add$:function(a){this.x+=a.x;this.y+=a.y;return this},sub$:function(a){this.x-=a.x;this.y-=a.y;return this},neg$:function(){this.x=-this.x;this.y=-this.y;return this},scale$:function(a){this.x*=a;this.y*=a;return this},prod$:function(a){this.x*=a.x;this.y*=a.y;return this},normalize$:function(){var a=this.x*this.x+this.y*this.y;
if(0===a||1===a)return this;a=1/Math.sqrt(a);this.x*=a;this.y*=a;return this}};e.random=function(){return d(Math.random(),Math.random())};e.fromPoints=function(a,b){return d(b.x-a.x,b.y-a.y)};"function"===typeof define&&define.amd?define([],function(){return e}):"object"===typeof exports?(Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=e,e.Vector2=e,module.exports=e):g.Vector2=e})(this);