Vector.js


SUBMITTED BY: okpalan86

DATE: Dec. 26, 2022, 4:30 p.m.

FORMAT: JavaScript

SIZE: 9.0 kB

HITS: 532

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

comments powered by Disqus