vector.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* global Float32Array */
  2. var ArrayCtor = typeof Float32Array === 'undefined' ? Array : Float32Array;
  3. /**
  4. * 创建一个向量
  5. * @param {number} [x=0]
  6. * @param {number} [y=0]
  7. * @return {Vector2}
  8. */
  9. function create(x, y) {
  10. var out = new ArrayCtor(2);
  11. if (x == null) {
  12. x = 0;
  13. }
  14. if (y == null) {
  15. y = 0;
  16. }
  17. out[0] = x;
  18. out[1] = y;
  19. return out;
  20. }
  21. /**
  22. * 复制向量数据
  23. * @param {Vector2} out
  24. * @param {Vector2} v
  25. * @return {Vector2}
  26. */
  27. function copy(out, v) {
  28. out[0] = v[0];
  29. out[1] = v[1];
  30. return out;
  31. }
  32. /**
  33. * 克隆一个向量
  34. * @param {Vector2} v
  35. * @return {Vector2}
  36. */
  37. function clone(v) {
  38. var out = new ArrayCtor(2);
  39. out[0] = v[0];
  40. out[1] = v[1];
  41. return out;
  42. }
  43. /**
  44. * 设置向量的两个项
  45. * @param {Vector2} out
  46. * @param {number} a
  47. * @param {number} b
  48. * @return {Vector2} 结果
  49. */
  50. function set(out, a, b) {
  51. out[0] = a;
  52. out[1] = b;
  53. return out;
  54. }
  55. /**
  56. * 向量相加
  57. * @param {Vector2} out
  58. * @param {Vector2} v1
  59. * @param {Vector2} v2
  60. */
  61. function add(out, v1, v2) {
  62. out[0] = v1[0] + v2[0];
  63. out[1] = v1[1] + v2[1];
  64. return out;
  65. }
  66. /**
  67. * 向量缩放后相加
  68. * @param {Vector2} out
  69. * @param {Vector2} v1
  70. * @param {Vector2} v2
  71. * @param {number} a
  72. */
  73. function scaleAndAdd(out, v1, v2, a) {
  74. out[0] = v1[0] + v2[0] * a;
  75. out[1] = v1[1] + v2[1] * a;
  76. return out;
  77. }
  78. /**
  79. * 向量相减
  80. * @param {Vector2} out
  81. * @param {Vector2} v1
  82. * @param {Vector2} v2
  83. */
  84. function sub(out, v1, v2) {
  85. out[0] = v1[0] - v2[0];
  86. out[1] = v1[1] - v2[1];
  87. return out;
  88. }
  89. /**
  90. * 向量长度
  91. * @param {Vector2} v
  92. * @return {number}
  93. */
  94. function len(v) {
  95. return Math.sqrt(lenSquare(v));
  96. }
  97. var length = len; // jshint ignore:line
  98. /**
  99. * 向量长度平方
  100. * @param {Vector2} v
  101. * @return {number}
  102. */
  103. function lenSquare(v) {
  104. return v[0] * v[0] + v[1] * v[1];
  105. }
  106. var lengthSquare = lenSquare;
  107. /**
  108. * 向量乘法
  109. * @param {Vector2} out
  110. * @param {Vector2} v1
  111. * @param {Vector2} v2
  112. */
  113. function mul(out, v1, v2) {
  114. out[0] = v1[0] * v2[0];
  115. out[1] = v1[1] * v2[1];
  116. return out;
  117. }
  118. /**
  119. * 向量除法
  120. * @param {Vector2} out
  121. * @param {Vector2} v1
  122. * @param {Vector2} v2
  123. */
  124. function div(out, v1, v2) {
  125. out[0] = v1[0] / v2[0];
  126. out[1] = v1[1] / v2[1];
  127. return out;
  128. }
  129. /**
  130. * 向量点乘
  131. * @param {Vector2} v1
  132. * @param {Vector2} v2
  133. * @return {number}
  134. */
  135. function dot(v1, v2) {
  136. return v1[0] * v2[0] + v1[1] * v2[1];
  137. }
  138. /**
  139. * 向量缩放
  140. * @param {Vector2} out
  141. * @param {Vector2} v
  142. * @param {number} s
  143. */
  144. function scale(out, v, s) {
  145. out[0] = v[0] * s;
  146. out[1] = v[1] * s;
  147. return out;
  148. }
  149. /**
  150. * 向量归一化
  151. * @param {Vector2} out
  152. * @param {Vector2} v
  153. */
  154. function normalize(out, v) {
  155. var d = len(v);
  156. if (d === 0) {
  157. out[0] = 0;
  158. out[1] = 0;
  159. } else {
  160. out[0] = v[0] / d;
  161. out[1] = v[1] / d;
  162. }
  163. return out;
  164. }
  165. /**
  166. * 计算向量间距离
  167. * @param {Vector2} v1
  168. * @param {Vector2} v2
  169. * @return {number}
  170. */
  171. function distance(v1, v2) {
  172. return Math.sqrt((v1[0] - v2[0]) * (v1[0] - v2[0]) + (v1[1] - v2[1]) * (v1[1] - v2[1]));
  173. }
  174. var dist = distance;
  175. /**
  176. * 向量距离平方
  177. * @param {Vector2} v1
  178. * @param {Vector2} v2
  179. * @return {number}
  180. */
  181. function distanceSquare(v1, v2) {
  182. return (v1[0] - v2[0]) * (v1[0] - v2[0]) + (v1[1] - v2[1]) * (v1[1] - v2[1]);
  183. }
  184. var distSquare = distanceSquare;
  185. /**
  186. * 求负向量
  187. * @param {Vector2} out
  188. * @param {Vector2} v
  189. */
  190. function negate(out, v) {
  191. out[0] = -v[0];
  192. out[1] = -v[1];
  193. return out;
  194. }
  195. /**
  196. * 插值两个点
  197. * @param {Vector2} out
  198. * @param {Vector2} v1
  199. * @param {Vector2} v2
  200. * @param {number} t
  201. */
  202. function lerp(out, v1, v2, t) {
  203. out[0] = v1[0] + t * (v2[0] - v1[0]);
  204. out[1] = v1[1] + t * (v2[1] - v1[1]);
  205. return out;
  206. }
  207. /**
  208. * 矩阵左乘向量
  209. * @param {Vector2} out
  210. * @param {Vector2} v
  211. * @param {Vector2} m
  212. */
  213. function applyTransform(out, v, m) {
  214. var x = v[0];
  215. var y = v[1];
  216. out[0] = m[0] * x + m[2] * y + m[4];
  217. out[1] = m[1] * x + m[3] * y + m[5];
  218. return out;
  219. }
  220. /**
  221. * 求两个向量最小值
  222. * @param {Vector2} out
  223. * @param {Vector2} v1
  224. * @param {Vector2} v2
  225. */
  226. function min(out, v1, v2) {
  227. out[0] = Math.min(v1[0], v2[0]);
  228. out[1] = Math.min(v1[1], v2[1]);
  229. return out;
  230. }
  231. /**
  232. * 求两个向量最大值
  233. * @param {Vector2} out
  234. * @param {Vector2} v1
  235. * @param {Vector2} v2
  236. */
  237. function max(out, v1, v2) {
  238. out[0] = Math.max(v1[0], v2[0]);
  239. out[1] = Math.max(v1[1], v2[1]);
  240. return out;
  241. }
  242. exports.create = create;
  243. exports.copy = copy;
  244. exports.clone = clone;
  245. exports.set = set;
  246. exports.add = add;
  247. exports.scaleAndAdd = scaleAndAdd;
  248. exports.sub = sub;
  249. exports.len = len;
  250. exports.length = length;
  251. exports.lenSquare = lenSquare;
  252. exports.lengthSquare = lengthSquare;
  253. exports.mul = mul;
  254. exports.div = div;
  255. exports.dot = dot;
  256. exports.scale = scale;
  257. exports.normalize = normalize;
  258. exports.distance = distance;
  259. exports.dist = dist;
  260. exports.distanceSquare = distanceSquare;
  261. exports.distSquare = distSquare;
  262. exports.negate = negate;
  263. exports.lerp = lerp;
  264. exports.applyTransform = applyTransform;
  265. exports.min = min;
  266. exports.max = max;