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.2KB

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