vectors.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class vec3
  2. {
  3. constructor(x, y, z)
  4. {
  5. this.x = x;
  6. this.y = y;
  7. this.z = z;
  8. }
  9. mag()
  10. {
  11. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
  12. }
  13. add(v)
  14. {
  15. this.x += v.x;
  16. this.y += v.y;
  17. this.z += v.z;
  18. }
  19. static add(v1, v2)
  20. {
  21. let result = new vec3(0, 0, 0);
  22. result.x = v1.x + v2.x;
  23. result.y = v1.y + v2.y;
  24. result.z = v1.z + v2.z;
  25. return result;
  26. }
  27. sub(v)
  28. {
  29. this.x -= v.x;
  30. this.y -= v.y;
  31. this.z -= v.z;
  32. }
  33. static sub(v1, v2)
  34. {
  35. return new vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
  36. }
  37. scale(n)
  38. {
  39. this.x *= n;
  40. this.y *= n;
  41. this.z *= n;
  42. }
  43. translate(n) { this.add(n); }
  44. rotateX(n) { /* TODO */ }
  45. rotateY(n) { /* TODO */ }
  46. rotateZ(n) { /* TODO */ }
  47. dot(v)
  48. {
  49. return (this.x * v.x) + (this.y + v.y) + (this.y * v.z)
  50. }
  51. cross(v)
  52. {
  53. return new vec3(this.y * v.z - this.z * v.y,
  54. this.z * v.x - this.x * v.z,
  55. this.x * v.y - this.y * v.x);
  56. }
  57. norm()
  58. {
  59. let m = this.mag();
  60. this.x /= m;
  61. this.y /= m;
  62. this.z /= m;
  63. }
  64. draw(colour)
  65. {
  66. fill(colour);
  67. circle(this.x, this.y, 10);
  68. }
  69. }
  70. class vec2
  71. {
  72. constructor(x, y)
  73. {
  74. this.x = x;
  75. this.y = y;
  76. }
  77. mag()
  78. {
  79. return Math.sqrt((this.x * this.x) + (this.y * this.y));
  80. }
  81. add(v)
  82. {
  83. this.x += v.x;
  84. this.y += v.y;
  85. }
  86. static add(v1, v2)
  87. {
  88. let result = new vec2(0, 0);
  89. result.x = v1.x + v2.x;
  90. result.y = v1.y + v2.y;
  91. return result;
  92. }
  93. sub(v)
  94. {
  95. this.x -= v.x;
  96. this.y -= v.y;
  97. }
  98. static sub(v1, v2)
  99. {
  100. return new vec2(v1.x - v2.x, v1.y - v2.y);
  101. }
  102. scale(n)
  103. {
  104. this.x *= n;
  105. this.y *= n;
  106. }
  107. translate(n) { this.add(n); }
  108. rotate(angle)
  109. {
  110. return new vec2(x * Math.cos(angle) - y * Math.sin(angle),
  111. this.y = x * Math.sin(angle) + y * Math.cos(angle));
  112. }
  113. dot(v)
  114. {
  115. return (this.x * v.x) + (this.y + v.y)
  116. }
  117. perp()
  118. {
  119. return new vec2(this.y, -this.x);
  120. }
  121. draw(colour)
  122. {
  123. fill(colour);
  124. circle(this.x, this.y, 10);
  125. }
  126. norm()
  127. {
  128. let m = this.mag();
  129. this.x /= m;
  130. this.y /= m;
  131. }
  132. }
  133. let position = new vec2(100, 100);
  134. let velocity = new vec2(1, 2);
  135. function setup()
  136. {
  137. createCanvas(windowWidth, windowHeight);
  138. }
  139. function draw()
  140. {
  141. background("black");
  142. position = position.rotate(0.1);
  143. position.draw("red");
  144. }