Advertisement
okpalan

Vector.diff.js

Nov 9th, 2023
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     export var Vector;
  2.     (function (Vector) {
  3.         Vector.Vec2 = function (x, y) {
  4.             return this;
  5.         };
  6.        
  7.         Object.defineProperty(Vector.Vec2, "create", {
  8.             configurable: false,
  9.             writable: false,
  10.             value: function (x, y) {
  11.                 return new Vector.Vec2(x, y);
  12.             }
  13.         });
  14.         Object.defineProperty(Vector.Vec2, "I", {
  15.             configurable: false,
  16.             writable: false,
  17.             value: new Vector.Vec2(1, 0)
  18.         });
  19.         Object.defineProperty(Vector.Vec2, "J", {
  20.             configurable: false,
  21.             writable: false,
  22.             value: new Vector.Vec2(0, 1)
  23.         });
  24.         Vector.Vec2.prototype.add = function (other) {
  25.             if (typeof other == "number") {
  26.                 this.x += other;
  27.                 this.y += other;
  28.             }
  29.             else {
  30.                 this.x += other.x;
  31.                 this.y += other.y;
  32.             }
  33.             return this;
  34.         };
  35.         Vector.Vec2.prototype.subtract = function (other) {
  36.             if (typeof other == "number") {
  37.                 this.x -= other;
  38.                 this.y -= other;
  39.             }
  40.             else {
  41.                 this.x -= other.x;
  42.                 this.y -= other.y;
  43.             }
  44.             return this;
  45.         };
  46.         Vector.Vec2.prototype.dot = function (other) {
  47.             if (typeof other == "number") {
  48.                 this.x *= other;
  49.                 this.y *= other;
  50.             }
  51.             else {
  52.                 this.x *= other.x;
  53.                 this.y *= other.y;
  54.             }
  55.             return this;
  56.         };
  57.         Vector.Vec2.prototype.divide = function (other) {
  58.             if (typeof other == "number") {
  59.                 this.x /= other;
  60.                 this.y /= other;
  61.             }
  62.             else {
  63.                 this.x /= other.x;
  64.                 this.y /= other.y;
  65.             }
  66.             return this;
  67.         };
  68.         Vector.Vec2.prototype.cross = function (other) {
  69.             if (typeof other == "number") {
  70.                 this.x *= other;
  71.                 this.y *= other;
  72.             }
  73.             else {
  74.                 this.x *= other.y;
  75.                 this.y *= other.x;
  76.             }
  77.             return this;
  78.         };
  79.         Vector.Vec2.prototype.magnitude = function () {
  80.             return Math.sqrt(this.x * this.x + this.y * this.y);
  81.         };
  82.         Vector.Vec2.prototype.normalize = function () {
  83.             this.x /= this.magnitude();
  84.             this.y /= this.magnitude();
  85.             return this;
  86.         };
  87.         Vector.Vec2.prototype.distanceTo = function (other) {
  88.             return Math.sqrt((this.y - other.y) + (this.x - other.x));
  89.         };
  90.         Vector.Vec2.prototype.rotateX = function (theta) {
  91.             var angle = Math.PI / 180 * theta;
  92.             this.y += Math.asin(angle);
  93.             return this;
  94.         };
  95.         Vector.Vec2.prototype.rotateY = function (theta) {
  96.             var angle = Math.PI / 180 * theta;
  97.             this.x += Math.acos(angle);
  98.             return this;
  99.         };
  100.         Vector.Vec2.prototype.negate = function () {
  101.             this.x = -this.x;
  102.             this.y = -this.y;
  103.             return this;
  104.         };
  105.         Vector.Vec2.prototype.toString = function () {
  106.             return "<" + this.x + "," + this.y + ">";
  107.         };
  108.         Vector.Vec3 = function (x = 0, y = 0, z = 0) {
  109.             this.x = x;
  110.             this.y = y;
  111.             this.z = z;
  112.             return this;
  113.         };
  114.         Object.defineProperty(Vector.Vec3, "I", {
  115.             get: function () {
  116.                 return new Vector.Vec3(1, 0, 0);
  117.             }
  118.         });
  119.         Object.defineProperty(Vector.Vec3, "J", {
  120.             get: function () {
  121.                 return new Vector.Vec3(0, 1, 0);
  122.             }
  123.         });
  124.         Object.defineProperty(Vector.Vec3, "K", {
  125.             get: function () {
  126.                 return new Vector.Vec3(0, 0, 1);
  127.             }
  128.         });
  129.         Vector.Vec3.prototype.add = function (v) {
  130.             if (v instanceof Vector.Vec3) {
  131.                 this.x += v.x;
  132.                 this.y += v.y;
  133.                 this.z += v.z;
  134.             }
  135.             else {
  136.                 this.x += v;
  137.                 this.y += v;
  138.                 this.z += v;
  139.             }
  140.             return this;
  141.         };
  142.         Vector.Vec3.prototype.subtract = function (v) {
  143.             if (v instanceof Vector.Vec3) {
  144.                 this.x -= v.x;
  145.                 this.y -= v.y;
  146.                 this.z -= v.z;
  147.             }
  148.             else {
  149.                 this.x -= v;
  150.                 this.y -= v;
  151.                 this.z -= v;
  152.             }
  153.             return this;
  154.         };
  155.         Vector.Vec3.prototype.clone = function () {
  156.             return new Vector.Vec3(this.x, this.y, this.z);
  157.         };
  158.         Vector.Vec3.prototype.dot = function (v) {
  159.             if (v instanceof Vector.Vec3) {
  160.                 this.x *= v.x;
  161.                 this.y *= v.y;
  162.                 this.z *= v.z;
  163.             }
  164.             else {
  165.                 this.x *= v;
  166.                 this.y *= v;
  167.                 this.z *= v;
  168.             }
  169.             return this;
  170.         };
  171.         Vector.Vec3.prototype.create = function (x, y, z) {
  172.             return new Vector.Vec3(x, y, z);
  173.         };
  174.         Vector.Vec3.prototype.cross = function (v) {
  175.             var x = this.x;
  176.             var y = this.y;
  177.             var z = this.z;
  178.             this.x = y * v.z - z * v.y;
  179.             this.y = x * v.z - z * v.x;
  180.             this.z = x * v.y - y * v.x;
  181.             return this;
  182.         };
  183.         Vector.Vec3.prototype.magnitude = function () {
  184.             return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  185.         };
  186.         Vector.Vec3.prototype.normalize = function () {
  187.             var len = this.magnitude();
  188.             this.x /= len;
  189.             this.y /= len;
  190.             this.z /= len;
  191.             return this;
  192.         };
  193.         Vector.Vec3.prototype.rotateX = function (angle) {
  194.             var y = this.y;
  195.             var z = this.z;
  196.             this.y = y * Math.cos(angle) - z * Math.sin(angle);
  197.             this.z = z * Math.cos(angle) + y * Math.sin(angle);
  198.         };
  199.         Vector.Vec3.prototype.rotateY = function (angle) {
  200.             var x = this.x;
  201.             var z = this.z;
  202.             this.x = x * Math.cos(angle) - z * Math.sin(angle);
  203.             this.z = z * Math.cos(angle) + x * Math.sin(angle);
  204.         };
  205.         Vector.Vec3.prototype.rotateZ = function (angle) {
  206.             var x = this.x;
  207.             var y = this.y;
  208.             this.x = x * Math.cos(angle) - y * Math.sin(angle);
  209.             this.y = y * Math.cos(angle) + x * Math.sin(angle);
  210.         };
  211.         Vector.Vec3.prototype.distance = function (v) {
  212.             if (v instanceof Vector.Vec3) {
  213.                 var dx = this.x - v.x;
  214.                 var dy = this.y - v.y;
  215.                 var dz = this.z - v.z;
  216.                 return Math.sqrt(dx * dx + dy * dy + dz * dz);
  217.             }
  218.             else {
  219.                 var dx = this.x - v;
  220.                 var dy = this.y - v;
  221.                 var dz = this.z - v;
  222.                 return Math.sqrt(dx * dx + dy * dy + dz * dz);
  223.             }
  224.         };
  225.         Vector.Vec3.prototype.clone = function () {
  226.             return new Vector.Vec3(this.x, this.y, this.z);
  227.         };
  228.         Vector.Vec3.prototype.toString = function () {
  229.             return "<" + this.x + ", " + this.y + ", " + this.z + ">";
  230.         };
  231.         Vector.Vec3.prototype.toArray = function () {
  232.             return [this.x, this.y, this.z];
  233.         };
  234.         Vector.Vec3.prototype.project = function (v) {
  235.             var dot = this.dot(v);
  236.             var len = v.magnitude();
  237.             return v.clone().dot(dot / len);
  238.         };
  239.         Vector.Vec3.prototype.reflect = function (v) {
  240.             var dot = this.dot(v);
  241.             var len = v.magnitude();
  242.             return v
  243.                 .clone()
  244.                 .dot(dot / len)
  245.                 .dot(2)
  246.                 .sub(this);
  247.         };
  248.         `
  249.          `;
  250.     })(Vector || (Vector = {}));
  251.  
Tags: vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement