Advertisement
okpalan

vector.js

Nov 7th, 2023 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.78 KB | Source Code | 0 0
  1. (function (global, factory) {
  2.   "use strict";
  3.   if (typeof define === "function" && define.amd) {
  4.     define(factory);
  5.   } else if (typeof module === "object" && typeof module.exports === "object") {
  6.     module.exports = factory(global);
  7.   } else {
  8.     global.Vector = factory(global);
  9.   }
  10. })(typeof window !== "undefined" ? globalThis : this,function (global) {
  11.   "use strict";
  12.   const Vector = {};
  13.  
  14.   function Vec2(x, y) {
  15.     this.x = x || 0;
  16.     this.y = y || 0;
  17.   }
  18.  
  19.   Object.defineProperty(Vec2, "I", {
  20.     value: new Vec2(1, 0),
  21.     writable: false,
  22.     configurable: false,
  23.   });
  24.  
  25.   Object.defineProperty(Vec2, "J", {
  26.     value: new Vec2(0, 1),
  27.     writable: false,
  28.     configurable: false,
  29.   });
  30.   Object.defineProperty(Vec2, "Scale", {
  31.     value: function (v0, num) {
  32.       return new Vec2(v0.x * num, v0.y * num);
  33.     },
  34.     writable: true,
  35.     configurable: true,
  36.   });
  37.   Object.defineProperty(Vec2, "Add", {
  38.     value: function (v0, v1) {
  39.       return new Vec2(v0.x + v1.x, v0.y + v1.y);
  40.     },
  41.     writable: true,
  42.     configurable: true,
  43.   });
  44.  
  45.   Object.defineProperty(Vec2, "Sub", {
  46.     value: function () {
  47.       return new Vec2(v0.x - v1.x, v0.y - v1.y);
  48.     },
  49.   });
  50.   Object.defineProperty(Vec2, "Cross", {
  51.     value: function () {
  52.       return new Vec2(v0.x * v1.y, v0.y * v1.x);
  53.     },
  54.   });
  55.   Object.defineProperty(Vec2, "Dot", {
  56.     value: function () {
  57.       return new Vec2(v0.x * v1.x, v0.y * v1.y);
  58.     },
  59.   });
  60.   Object.defineProperty(Vec2, "Div", {
  61.     value: function () {
  62.       return new Vec2(v0.x / v1.x, v0.y / v1.y);
  63.     },
  64.   });
  65.   Object.defineProperty(Vec2, "RotX", {
  66.     value: function (v0, theta) {
  67.       if (!v0 instanceof Vec2) {
  68.         v0 = new Vec2(v0.x, v0.y);
  69.       }
  70.       var angleInRadian = (Math.PI / 180) * theta;
  71.       v0.y += -Math.sin(angleInRadian);
  72.       return v0;
  73.     },
  74.   });
  75.   Object.defineProperty(Vec2, "RotY", {
  76.     value: function (v0, theta) {
  77.       if (!v0 instanceof Vec2) {
  78.         v0 = new Vec2(v0.x, v0.y);
  79.       }
  80.       var angleInRadian = (Math.PI / 180) * theta;
  81.       this.x += -Math.cos(angleInRadian);
  82.       return v0;
  83.     },
  84.   });
  85.  
  86.   Object.defineProperty(Vec2, "FromArray", {
  87.     value: function (arr) {
  88.       return new Vec2(arr[0], arr[1]);
  89.     },
  90.   });
  91.  
  92.   Vec2.prototype = {
  93.     Scale: function (num) {
  94.       this.x *= num;
  95.       this.y *= num;
  96.       return this;
  97.     },
  98.     Add: function (obj) {
  99.       this.x += obj.x;
  100.       this.y += obj.y;
  101.       return this;
  102.     },
  103.     Sub: function (obj) {
  104.       this.x -= obj.x;
  105.       this.y -= obj.y;
  106.       return this;
  107.     },
  108.     Cross: function (obj) {
  109.       this.x *= obj.y;
  110.       this.y *= obj.x;
  111.       return this;
  112.     },
  113.     Dot: function (obj) {
  114.       this.x *= obj.y;
  115.       this.y *= obj.x;
  116.       return this;
  117.     },
  118.     Div: function (obj) {
  119.       this.x /= obj.x;
  120.       this.y /= obj.y;
  121.       return this;
  122.     },
  123.     Mag: function () {
  124.       return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
  125.     },
  126.     Unit: function () {
  127.       this.x = this.x / this.Mag();
  128.       this.y = this.y / this.Mag();
  129.       return this;
  130.     },
  131.     RotX: function (theta) {
  132.       var angleInRadian = (Math.PI / 180) * theta;
  133.       this.y += -Math.sin(angleInRadian);
  134.     },
  135.     RotY: function (theta) {
  136.       var angleInRadian = (Math.PI / 180) * theta;
  137.       this.x += -Math.cos(angleInRadian);
  138.     },
  139.     ToArray: function () {
  140.       return [this.x, this.y];
  141.     },
  142.     Clone: function () {
  143.       return new Vec2(this.x, this.y);
  144.     },
  145.   };
  146.   Vector.Vec2 = Vec2;
  147.  
  148.   function Vec3(x, y, z) {
  149.     this.x = x || 0;
  150.     this.y = y || 0;
  151.     this.z = z || 0;
  152.   }
  153.  
  154.   Object.defineProperty(Vec3, "I", {
  155.     value: new Vec3(1, 0, 0),
  156.     configurable: false,
  157.     writable: false,
  158.   });
  159.  
  160.   Object.defineProperty(Vec3, "J", {
  161.     value: new Vec3(0, 1, 0),
  162.     configurable: false,
  163.     writable: false,
  164.   });
  165.  
  166.   Object.defineProperty(Vec3, "K", {
  167.     value: new Vec3(0, 0, 1),
  168.     configurable: false,
  169.     writable: false,
  170.   });
  171.  
  172.   Object.defineProperty(Vec3, "Scale", {
  173.     value: function (obj, num) {
  174.       return new Vec3(obj.x * num, obj.y * num, obj.z * num);
  175.     },
  176.     configurable: false,
  177.     writable: false,
  178.   });
  179.   Object.defineProperty(Vec3, "Add", {
  180.     value: function (obj, obj2) {
  181.       return new Vec3(obj.x + obj2.x, obj.y + obj2.y, obj.z + obj2.z);
  182.     },
  183.     configurable: false,
  184.     writable: false,
  185.   });
  186.   Object.defineProperty(Vec3, "Sub", {
  187.     value: function (obj, obj2) {
  188.       return new Vec3(obj.x - obj2.x, obj.y - obj2.y, obj.z - obj2.z);
  189.     },
  190.     configurable: false,
  191.     writable: false,
  192.   });
  193.  
  194.   Object.defineProperty(Vec3, "Div", {
  195.     value: function (obj, obj2) {
  196.       return new Vec3(obj.x / obj2.x, obj.y / obj2.y, obj.z / obj2.z);
  197.     },
  198.     configurable: false,
  199.     writable: false,
  200.   });
  201.  
  202.   Object.defineProperty(Vec3, "Cross", {
  203.     value: function (obj, obj2) {
  204.       var x = obj.x * obj2.y + obj.x * obj2.z;
  205.       var y = obj.y * obj2.x + obj.y * obj2.z;
  206.       var z = obj.z * obj2.x + obj.z * obj2.y;
  207.       return new Vec3(x, y, z);
  208.     },
  209.     configurable: false,
  210.     writable: false,
  211.   });
  212.   Object.defineProperty(Vec3, "Dot", {
  213.     value: function (obj, obj2) {
  214.       var x = obj.x * obj2.x;
  215.       var y = obj.y * obj2.y;
  216.       var z = obj.z * obj2.z;
  217.       return new Vec3(x, y, z);
  218.     },
  219.     configurable: false,
  220.     writable: false,
  221.   });
  222.   Object.defineProperty(Vec3, "RotX", {
  223.     value: function (obj, theta) {
  224.       var angleInRadian = (Math.PI / 180) * theta;
  225.       obj.z += Math.cos(angleInRadian) - Math.sin(angleInRadian);
  226.       obj.y += Math.sin(angleInRadian) + Math.cos(angleInRadian);
  227.       return new Vec3(obj);
  228.     },
  229.     configurable: false,
  230.     writable: false,
  231.   });
  232.   Object.defineProperty(Vec3, "RotY", {
  233.     value: function (obj, theta) {
  234.       var angleInRadian = (Math.PI / 180) * theta;
  235.       obj.z += Math.cos(angleInRadian) + Math.sin(angleInRadian);
  236.       obj.x += -Math.sin(angleInRadian) + Math.cos(angleInRadian);
  237.       return new Vec3(obj);
  238.     },
  239.     configurable: false,
  240.     writable: false,
  241.   });
  242.   Object.defineProperty(Vec3, "RotZ", {
  243.     value: function (obj, theta) {
  244.       var angleInRadian = (Math.PI / 180) * theta;
  245.       obj.x += Math.cos(angleInRadian) - Math.sin(angleInRadian);
  246.       obj.y += Math.sin(angleInRadian) + Math.cos(angleInRadian);
  247.       return new Vec3(obj);
  248.     },
  249.     configurable: false,
  250.     writable: false,
  251.   });
  252.  
  253.   Object.defineProperty(Vec3, "FromArray", {
  254.     value: function (obj) {
  255.       return new Vec3(obj[0], obj[1], obj[2]);
  256.     },
  257.     configurable: false,
  258.     writable: false,
  259.   });
  260.  
  261.   Vec3.prototype = {
  262.     Scale: function (num) {
  263.       this.x *= num;
  264.       this.y *= num;
  265.       this.z *= num;
  266.       return this;
  267.     },
  268.     Add: function (obj) {
  269.       this.x += obj.x;
  270.       this.y += obj.y;
  271.       this.z += obj.z;
  272.       return this;
  273.     },
  274.     Sub: function (obj) {
  275.       this.x -= obj.x;
  276.       this.y -= obj.y;
  277.       this.z -= obj.z;
  278.       return this;
  279.     },
  280.     Cross: function (obj) {
  281.       this.x = this.x * obj.y + this.x * obj.z;
  282.       this.y = this.y * obj.x + this.y * obj.z;
  283.       this.z = this.z * obj.x + this.z * obj.y;
  284.       return this;
  285.     },
  286.     Dot: function (obj) {
  287.       this.x *= obj.x;
  288.       this.y *= obj.y;
  289.       this.z *= obj.z;
  290.       return this;
  291.     },
  292.     Div: function (obj) {
  293.       this.x /= obj.x;
  294.       this.y /= obj.y;
  295.       this.z /= obj.z;
  296.       return this;
  297.     },
  298.     Mag: function () {
  299.       return Math.sqrt(
  300.         Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2)
  301.       );
  302.     },
  303.     Unit: function () {
  304.       this.x = this.x / this.Mag();
  305.       this.y = this.y / this.Mag();
  306.       this.z = this.z / this.Mag();
  307.       return this;
  308.     },
  309.     RotX: function (theta) {
  310.       var angleInRadian = (Math.PI / 180) * theta;
  311.       this.z += Math.cos(angleInRadian) - Math.sin(angleInRadian);
  312.       this.y += Math.sin(angleInRadian) + Math.cos(angleInRadian);
  313.     },
  314.     RotY: function (theta) {
  315.       var angleInRadian = (Math.PI / 180) * theta;
  316.       this.z += Math.cos(angleInRadian) + Math.sin(angleInRadian);
  317.       this.x += -Math.sin(angleInRadian) + Math.cos(angleInRadian);
  318.     },
  319.     RotZ: function (theta) {
  320.       var angleInRadian = (Math.PI / 180) * theta;
  321.       this.x += Math.cos(angleInRadian) - Math.sin(angleInRadian);
  322.       this.y += Math.sin(angleInRadian) + Math.cos(angleInRadian);
  323.     },
  324.     ToArray: function () {
  325.       return [this.x, this.y, this.z];
  326.     },
  327.     Clone: function () {
  328.       return new Vec3(this.x, this.y, this.z);
  329.     },
  330.     Project: function (obj) {
  331.       return Vec3.FromArray(
  332.         [this].forEach((p) => (p.Dot(obj) / Math.pow(obj.Mag(), 2)) * p)
  333.       );
  334.     },
  335.   };
  336.   Vector.Vec3 = Vec3;
  337.   return Vector;
  338. });
  339.  
Tags: vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement