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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. if (r === 0) return A
  226. const s = Math.sin(r)
  227. const c = Math.cos(r)
  228. const px = A[0] - C[0]
  229. const py = A[1] - C[1]
  230. const nx = px * c - py * s
  231. const ny = px * s + py * c
  232. return [nx + C[0], ny + C[1]]
  233. }
  234. /**
  235. * Check of two vectors are identical.
  236. * @param A
  237. * @param B
  238. */
  239. export function isEqual(A: number[], B: number[]) {
  240. return A[0] === B[0] && A[1] === B[1]
  241. }
  242. /**
  243. * Interpolate vector A to B with a scalar t
  244. * @param A
  245. * @param B
  246. * @param t scalar
  247. */
  248. export function lrp(A: number[], B: number[], t: number) {
  249. return add(A, mul(vec(A, B), t))
  250. }
  251. /**
  252. * Interpolate from A to B when curVAL goes fromVAL => to
  253. * @param A
  254. * @param B
  255. * @param from Starting value
  256. * @param to Ending value
  257. * @param s Strength
  258. */
  259. export function int(A: number[], B: number[], from: number, to: number, s = 1) {
  260. const t = (clamp(from, to) - from) / (to - from)
  261. return add(mul(A, 1 - t), mul(B, s))
  262. }
  263. /**
  264. * Get the angle between the three vectors A, B, and C.
  265. * @param p1
  266. * @param pc
  267. * @param p2
  268. */
  269. export function ang3(p1: number[], pc: number[], p2: number[]) {
  270. // this,
  271. const v1 = vec(pc, p1)
  272. const v2 = vec(pc, p2)
  273. return ang(v1, v2)
  274. }
  275. /**
  276. * Absolute value of a vector.
  277. * @param A
  278. * @returns
  279. */
  280. export function abs(A: number[]) {
  281. return [Math.abs(A[0]), Math.abs(A[1])]
  282. }
  283. export function rescale(a: number[], n: number) {
  284. const l = len(a)
  285. return [(n * a[0]) / l, (n * a[1]) / l]
  286. }
  287. /**
  288. * Get whether p1 is left of p2, relative to pc.
  289. * @param p1
  290. * @param pc
  291. * @param p2
  292. */
  293. export function isLeft(p1: number[], pc: number[], p2: number[]) {
  294. // isLeft: >0 for counterclockwise
  295. // =0 for none (degenerate)
  296. // <0 for clockwise
  297. return (pc[0] - p1[0]) * (p2[1] - p1[1]) - (p2[0] - p1[0]) * (pc[1] - p1[1])
  298. }
  299. export function clockwise(p1: number[], pc: number[], p2: number[]) {
  300. return isLeft(p1, pc, p2) > 0
  301. }
  302. export function round(a: number[], d = 5) {
  303. return a.map((v) => Number(v.toPrecision(d)))
  304. }
  305. /**
  306. * Get the minimum distance from a point P to a line with a segment AB.
  307. * @param A The start of the line.
  308. * @param B The end of the line.
  309. * @param P A point.
  310. * @returns
  311. */
  312. // export function distanceToLine(A: number[], B: number[], P: number[]) {
  313. // const delta = sub(B, A)
  314. // const angle = Math.atan2(delta[1], delta[0])
  315. // const dir = rot(sub(P, A), -angle)
  316. // return dir[1]
  317. // }
  318. /**
  319. * Get the nearest point on a line segment AB.
  320. * @param A The start of the line.
  321. * @param B The end of the line.
  322. * @param P A point.
  323. * @param clamp Whether to clamp the resulting point to the segment.
  324. * @returns
  325. */
  326. // export function nearestPointOnLine(
  327. // A: number[],
  328. // B: number[],
  329. // P: number[],
  330. // clamp = true
  331. // ) {
  332. // const delta = sub(B, A)
  333. // const length = len(delta)
  334. // const angle = Math.atan2(delta[1], delta[0])
  335. // const dir = rot(sub(P, A), -angle)
  336. // if (clamp) {
  337. // if (dir[0] < 0) return A
  338. // if (dir[0] > length) return B
  339. // }
  340. // return add(A, div(mul(delta, dir[0]), length))
  341. // }
  342. /**
  343. * Get the nearest point on a line with a known unit vector that passes through point A
  344. * @param A Any point on the line
  345. * @param u The unit vector for the line.
  346. * @param P A point not on the line to test.
  347. * @returns
  348. */
  349. export function nearestPointOnLineThroughPoint(
  350. A: number[],
  351. u: number[],
  352. P: number[]
  353. ) {
  354. return add(A, mul(u, pry(sub(P, A), u)))
  355. }
  356. /**
  357. * Distance between a point and a line with a known unit vector that passes through a point.
  358. * @param A Any point on the line
  359. * @param u The unit vector for the line.
  360. * @param P A point not on the line to test.
  361. * @returns
  362. */
  363. export function distanceToLineThroughPoint(
  364. A: number[],
  365. u: number[],
  366. P: number[]
  367. ) {
  368. return dist(P, nearestPointOnLineThroughPoint(A, u, P))
  369. }
  370. /**
  371. * Get the nearest point on a line segment between A and B
  372. * @param A The start of the line segment
  373. * @param B The end of the line segment
  374. * @param P The off-line point
  375. * @param clamp Whether to clamp the point between A and B.
  376. * @returns
  377. */
  378. export function nearestPointOnLineSegment(
  379. A: number[],
  380. B: number[],
  381. P: number[],
  382. clamp = true
  383. ) {
  384. const delta = sub(B, A)
  385. const length = len(delta)
  386. const u = div(delta, length)
  387. const pt = add(A, mul(u, pry(sub(P, A), u)))
  388. if (clamp) {
  389. const da = dist(A, pt)
  390. const db = dist(B, pt)
  391. if (db < da && da > length) return B
  392. if (da < db && db > length) return A
  393. }
  394. return pt
  395. }
  396. /**
  397. * Distance between a point and the nearest point on a line segment between A and B
  398. * @param A The start of the line segment
  399. * @param B The end of the line segment
  400. * @param P The off-line point
  401. * @param clamp Whether to clamp the point between A and B.
  402. * @returns
  403. */
  404. export function distanceToLineSegment(
  405. A: number[],
  406. B: number[],
  407. P: number[],
  408. clamp = true
  409. ) {
  410. return dist(P, nearestPointOnLineSegment(A, B, P, clamp))
  411. }
  412. /**
  413. * Get a vector d distance from A towards B.
  414. * @param A
  415. * @param B
  416. * @param d
  417. * @returns
  418. */
  419. export function nudge(A: number[], B: number[], d: number) {
  420. return add(A, mul(uni(vec(A, B)), d))
  421. }
  422. /**
  423. * Round a vector to a precision length.
  424. * @param a
  425. * @param n
  426. */
  427. export function toPrecision(a: number[], n = 4) {
  428. return [+a[0].toPrecision(n), +a[1].toPrecision(n)]
  429. }