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.

vec.ts 9.5KB

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