You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // A big collection of vector utilities. Collected into a class to improve logging / packaging.
  2. export default class Vec {
  3. /**
  4. * Clamp a value into a range.
  5. * @param n
  6. * @param min
  7. */
  8. static clamp(n: number, min: number): number
  9. static clamp(n: number, min: number, max: number): number
  10. static clamp(n: number, min: number, max?: number): number {
  11. return Math.max(min, typeof max !== 'undefined' ? Math.min(n, max) : n)
  12. }
  13. /**
  14. * Negate a vector.
  15. * @param A
  16. */
  17. static neg = (A: number[]) => {
  18. return [-A[0], -A[1]]
  19. }
  20. /**
  21. * Add vectors.
  22. * @param A
  23. * @param B
  24. */
  25. static add = (A: number[], B: number[]) => {
  26. return [A[0] + B[0], A[1] + B[1]]
  27. }
  28. /**
  29. * Add scalar to vector.
  30. * @param A
  31. * @param B
  32. */
  33. static addScalar = (A: number[], n: number) => {
  34. return [A[0] + n, A[1] + n]
  35. }
  36. /**
  37. * Subtract vectors.
  38. * @param A
  39. * @param B
  40. */
  41. static sub = (A: number[], B: number[]) => {
  42. return [A[0] - B[0], A[1] - B[1]]
  43. }
  44. /**
  45. * Subtract scalar from vector.
  46. * @param A
  47. * @param B
  48. */
  49. static subScalar = (A: number[], n: number) => {
  50. return [A[0] - n, A[1] - n]
  51. }
  52. /**
  53. * Get the vector from vectors A to B.
  54. * @param A
  55. * @param B
  56. */
  57. static vec = (A: number[], B: number[]) => {
  58. // A, B as vectors get the vector from A to B
  59. return [B[0] - A[0], B[1] - A[1]]
  60. }
  61. /**
  62. * Vector multiplication by scalar
  63. * @param A
  64. * @param n
  65. */
  66. static mul = (A: number[], n: number) => {
  67. return [A[0] * n, A[1] * n]
  68. }
  69. static mulV = (A: number[], B: number[]) => {
  70. return [A[0] * B[0], A[1] * B[1]]
  71. }
  72. /**
  73. * Vector division by scalar.
  74. * @param A
  75. * @param n
  76. */
  77. static div = (A: number[], n: number) => {
  78. return [A[0] / n, A[1] / n]
  79. }
  80. /**
  81. * Vector division by vector.
  82. * @param A
  83. * @param n
  84. */
  85. static divV = (A: number[], B: number[]) => {
  86. return [A[0] / B[0], A[1] / B[1]]
  87. }
  88. /**
  89. * Perpendicular rotation of a vector A
  90. * @param A
  91. */
  92. static per(A: number[]) {
  93. return [A[1], -A[0]]
  94. }
  95. /**
  96. * Dot product
  97. * @param A
  98. * @param B
  99. */
  100. static dpr = (A: number[], B: number[]) => {
  101. return A[0] * B[0] + A[1] * B[1]
  102. }
  103. /**
  104. * Cross product (outer product) | A X B |
  105. * @param A
  106. * @param B
  107. */
  108. static cpr = (A: number[], B: number[]) => {
  109. return A[0] * B[1] - B[0] * A[1]
  110. }
  111. /**
  112. * Length of the vector squared
  113. * @param A
  114. */
  115. static len2 = (A: number[]) => {
  116. return A[0] * A[0] + A[1] * A[1]
  117. }
  118. /**
  119. * Length of the vector
  120. * @param A
  121. */
  122. static len = (A: number[]) => {
  123. return Math.hypot(A[0], A[1])
  124. }
  125. /**
  126. * Project A over B
  127. * @param A
  128. * @param B
  129. */
  130. static pry = (A: number[], B: number[]) => {
  131. return Vec.dpr(A, B) / Vec.len(B)
  132. }
  133. /**
  134. * Get normalized / unit vector.
  135. * @param A
  136. */
  137. static uni = (A: number[]) => {
  138. return Vec.div(A, Vec.len(A))
  139. }
  140. /**
  141. * Get normalized / unit vector.
  142. * @param A
  143. */
  144. static normalize = (A: number[]) => {
  145. return Vec.uni(A)
  146. }
  147. /**
  148. * Get the tangent between two vectors.
  149. * @param A
  150. * @param B
  151. * @returns
  152. */
  153. static tangent = (A: number[], B: number[]) => {
  154. return Vec.normalize(Vec.sub(A, B))
  155. }
  156. /**
  157. * Dist length from A to B squared.
  158. * @param A
  159. * @param B
  160. */
  161. static dist2 = (A: number[], B: number[]) => {
  162. return Vec.len2(Vec.sub(A, B))
  163. }
  164. /**
  165. * Dist length from A to B
  166. * @param A
  167. * @param B
  168. */
  169. static dist = (A: number[], B: number[]) => {
  170. return Math.hypot(A[1] - B[1], A[0] - B[0])
  171. }
  172. /**
  173. * A faster, though less accurate method for testing distances. Maybe faster?
  174. * @param A
  175. * @param B
  176. * @returns
  177. */
  178. static fastDist = (A: number[], B: number[]) => {
  179. const V = [B[0] - A[0], B[1] - A[1]]
  180. const aV = [Math.abs(V[0]), Math.abs(V[1])]
  181. let r = 1 / Math.max(aV[0], aV[1])
  182. r = r * (1.29289 - (aV[0] + aV[1]) * r * 0.29289)
  183. return [V[0] * r, V[1] * r]
  184. }
  185. /**
  186. * Angle between vector A and vector B in radians
  187. * @param A
  188. * @param B
  189. */
  190. static ang = (A: number[], B: number[]) => {
  191. return Math.atan2(Vec.cpr(A, B), Vec.dpr(A, B))
  192. }
  193. /**
  194. * Angle between vector A and vector B in radians
  195. * @param A
  196. * @param B
  197. */
  198. static angle = (A: number[], B: number[]) => {
  199. return Math.atan2(B[1] - A[1], B[0] - A[0])
  200. }
  201. /**
  202. * Mean between two vectors or mid vector between two vectors
  203. * @param A
  204. * @param B
  205. */
  206. static med = (A: number[], B: number[]) => {
  207. return Vec.mul(Vec.add(A, B), 0.5)
  208. }
  209. /**
  210. * Vector rotation by r (radians)
  211. * @param A
  212. * @param r rotation in radians
  213. */
  214. static rot = (A: number[], r: number) => {
  215. return [
  216. A[0] * Math.cos(r) - A[1] * Math.sin(r),
  217. A[0] * Math.sin(r) + A[1] * Math.cos(r),
  218. ]
  219. }
  220. /**
  221. * Rotate a vector around another vector by r (radians)
  222. * @param A vector
  223. * @param C center
  224. * @param r rotation in radians
  225. */
  226. static rotWith = (A: number[], C: number[], r: number) => {
  227. if (r === 0) return A
  228. const s = Math.sin(r)
  229. const c = Math.cos(r)
  230. const px = A[0] - C[0]
  231. const py = A[1] - C[1]
  232. const nx = px * c - py * s
  233. const ny = px * s + py * c
  234. return [nx + C[0], ny + C[1]]
  235. }
  236. /**
  237. * Check of two vectors are identical.
  238. * @param A
  239. * @param B
  240. */
  241. static isEqual = (A: number[], B: number[]) => {
  242. return A[0] === B[0] && A[1] === B[1]
  243. }
  244. /**
  245. * Interpolate vector A to B with a scalar t
  246. * @param A
  247. * @param B
  248. * @param t scalar
  249. */
  250. static lrp = (A: number[], B: number[], t: number) => {
  251. return Vec.add(A, Vec.mul(Vec.vec(A, B), t))
  252. }
  253. /**
  254. * Interpolate from A to B when curVAL goes fromVAL => to
  255. * @param A
  256. * @param B
  257. * @param from Starting value
  258. * @param to Ending value
  259. * @param s Strength
  260. */
  261. static int = (A: number[], B: number[], from: number, to: number, s = 1) => {
  262. const t = (Vec.clamp(from, to) - from) / (to - from)
  263. return Vec.add(Vec.mul(A, 1 - t), Vec.mul(B, s))
  264. }
  265. /**
  266. * Get the angle between the three vectors A, B, and C.
  267. * @param p1
  268. * @param pc
  269. * @param p2
  270. */
  271. static ang3 = (p1: number[], pc: number[], p2: number[]) => {
  272. // this,
  273. const v1 = Vec.vec(pc, p1)
  274. const v2 = Vec.vec(pc, p2)
  275. return Vec.ang(v1, v2)
  276. }
  277. /**
  278. * Absolute value of a vector.
  279. * @param A
  280. * @returns
  281. */
  282. static abs = (A: number[]) => {
  283. return [Math.abs(A[0]), Math.abs(A[1])]
  284. }
  285. static rescale = (a: number[], n: number) => {
  286. const l = Vec.len(a)
  287. return [(n * a[0]) / l, (n * a[1]) / l]
  288. }
  289. /**
  290. * Get whether p1 is left of p2, relative to pc.
  291. * @param p1
  292. * @param pc
  293. * @param p2
  294. */
  295. static isLeft = (p1: number[], pc: number[], p2: number[]) => {
  296. // isLeft: >0 for counterclockwise
  297. // =0 for none (degenerate)
  298. // <0 for clockwise
  299. return (pc[0] - p1[0]) * (p2[1] - p1[1]) - (p2[0] - p1[0]) * (pc[1] - p1[1])
  300. }
  301. static clockwise = (p1: number[], pc: number[], p2: number[]) => {
  302. return Vec.isLeft(p1, pc, p2) > 0
  303. }
  304. static round = (a: number[], d = 5) => {
  305. return a.map((v) => Number(v.toPrecision(d)))
  306. }
  307. /**
  308. * Get the minimum distance from a point P to a line with a segment AB.
  309. * @param A The start of the line.
  310. * @param B The end of the line.
  311. * @param P A point.
  312. * @returns
  313. */
  314. // static distanceToLine(A: number[], B: number[], P: number[]) {
  315. // const delta = sub(B, A)
  316. // const angle = Math.atan2(delta[1], delta[0])
  317. // const dir = rot(sub(P, A), -angle)
  318. // return dir[1]
  319. // }
  320. /**
  321. * Get the nearest point on a line segment AB.
  322. * @param A The start of the line.
  323. * @param B The end of the line.
  324. * @param P A point.
  325. * @param clamp Whether to clamp the resulting point to the segment.
  326. * @returns
  327. */
  328. // static nearestPointOnLine(
  329. // A: number[],
  330. // B: number[],
  331. // P: number[],
  332. // clamp = true
  333. // ) {
  334. // const delta = sub(B, A)
  335. // const length = len(delta)
  336. // const angle = Math.atan2(delta[1], delta[0])
  337. // const dir = rot(sub(P, A), -angle)
  338. // if (clamp) {
  339. // if (dir[0] < 0) return A
  340. // if (dir[0] > length) return B
  341. // }
  342. // return add(A, div(mul(delta, dir[0]), length))
  343. // }
  344. /**
  345. * Get the nearest point on a line with a known unit vector that passes through point A
  346. * @param A Any point on the line
  347. * @param u The unit vector for the line.
  348. * @param P A point not on the line to test.
  349. * @returns
  350. */
  351. static nearestPointOnLineThroughPoint = (
  352. A: number[],
  353. u: number[],
  354. P: number[]
  355. ) => {
  356. return Vec.add(A, Vec.mul(u, Vec.pry(Vec.sub(P, A), u)))
  357. }
  358. /**
  359. * Distance between a point and a line with a known unit vector that passes through a point.
  360. * @param A Any point on the line
  361. * @param u The unit vector for the line.
  362. * @param P A point not on the line to test.
  363. * @returns
  364. */
  365. static distanceToLineThroughPoint = (
  366. A: number[],
  367. u: number[],
  368. P: number[]
  369. ) => {
  370. return Vec.dist(P, Vec.nearestPointOnLineThroughPoint(A, u, P))
  371. }
  372. /**
  373. * Get the nearest point on a line segment between A and B
  374. * @param A The start of the line segment
  375. * @param B The end of the line segment
  376. * @param P The off-line point
  377. * @param clamp Whether to clamp the point between A and B.
  378. * @returns
  379. */
  380. static nearestPointOnLineSegment = (
  381. A: number[],
  382. B: number[],
  383. P: number[],
  384. clamp = true
  385. ) => {
  386. const delta = Vec.sub(B, A)
  387. const length = Vec.len(delta)
  388. const u = Vec.div(delta, length)
  389. const pt = Vec.add(A, Vec.mul(u, Vec.pry(Vec.sub(P, A), u)))
  390. if (clamp) {
  391. const da = Vec.dist(A, pt)
  392. const db = Vec.dist(B, pt)
  393. if (db < da && da > length) return B
  394. if (da < db && db > length) return A
  395. }
  396. return pt
  397. }
  398. /**
  399. * Distance between a point and the nearest point on a line segment between A and B
  400. * @param A The start of the line segment
  401. * @param B The end of the line segment
  402. * @param P The off-line point
  403. * @param clamp Whether to clamp the point between A and B.
  404. * @returns
  405. */
  406. static distanceToLineSegment = (
  407. A: number[],
  408. B: number[],
  409. P: number[],
  410. clamp = true
  411. ) => {
  412. return Vec.dist(P, Vec.nearestPointOnLineSegment(A, B, P, clamp))
  413. }
  414. /**
  415. * Get a vector d distance from A towards B.
  416. * @param A
  417. * @param B
  418. * @param d
  419. * @returns
  420. */
  421. static nudge = (A: number[], B: number[], d: number) => {
  422. return Vec.add(A, Vec.mul(Vec.uni(Vec.vec(A, B)), d))
  423. }
  424. /**
  425. * Round a vector to a precision length.
  426. * @param a
  427. * @param n
  428. */
  429. static toPrecision = (a: number[], n = 4) => {
  430. return [+a[0].toPrecision(n), +a[1].toPrecision(n)]
  431. }
  432. }