Vector.diff.js


SUBMITTED BY: okpalan86

DATE: May 9, 2023, 2:47 p.m.

FORMAT: Text only

SIZE: 7.4 kB

HITS: 594

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

comments powered by Disqus