您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

vec.ts 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. const rounds = [1, 10, 100, 1000]
  303. export function round(a: number[], d = 2) {
  304. return [
  305. Math.round(a[0] * rounds[d]) / rounds[d],
  306. Math.round(a[1] * rounds[d]) / rounds[d],
  307. ]
  308. }
  309. /**
  310. * Get the minimum distance from a point P to a line with a segment AB.
  311. * @param A The start of the line.
  312. * @param B The end of the line.
  313. * @param P A point.
  314. * @returns
  315. */
  316. // export function distanceToLine(A: number[], B: number[], P: number[]) {
  317. // const delta = sub(B, A)
  318. // const angle = Math.atan2(delta[1], delta[0])
  319. // const dir = rot(sub(P, A), -angle)
  320. // return dir[1]
  321. // }
  322. /**
  323. * Get the nearest point on a line segment AB.
  324. * @param A The start of the line.
  325. * @param B The end of the line.
  326. * @param P A point.
  327. * @param clamp Whether to clamp the resulting point to the segment.
  328. * @returns
  329. */
  330. // export function nearestPointOnLine(
  331. // A: number[],
  332. // B: number[],
  333. // P: number[],
  334. // clamp = true
  335. // ) {
  336. // const delta = sub(B, A)
  337. // const length = len(delta)
  338. // const angle = Math.atan2(delta[1], delta[0])
  339. // const dir = rot(sub(P, A), -angle)
  340. // if (clamp) {
  341. // if (dir[0] < 0) return A
  342. // if (dir[0] > length) return B
  343. // }
  344. // return add(A, div(mul(delta, dir[0]), length))
  345. // }
  346. /**
  347. * Get the nearest point on a line with a known unit vector that passes through point A
  348. * @param A Any point on the line
  349. * @param u The unit vector for the line.
  350. * @param P A point not on the line to test.
  351. * @returns
  352. */
  353. export function nearestPointOnLineThroughPoint(
  354. A: number[],
  355. u: number[],
  356. P: number[]
  357. ) {
  358. return add(A, mul(u, pry(sub(P, A), u)))
  359. }
  360. /**
  361. * Distance between a point and a line with a known unit vector that passes through a point.
  362. * @param A Any point on the line
  363. * @param u The unit vector for the line.
  364. * @param P A point not on the line to test.
  365. * @returns
  366. */
  367. export function distanceToLineThroughPoint(
  368. A: number[],
  369. u: number[],
  370. P: number[]
  371. ) {
  372. return dist(P, nearestPointOnLineThroughPoint(A, u, P))
  373. }
  374. /**
  375. * Get the nearest point on a line segment between A and B
  376. * @param A The start of the line segment
  377. * @param B The end of the line segment
  378. * @param P The off-line point
  379. * @param clamp Whether to clamp the point between A and B.
  380. * @returns
  381. */
  382. export function nearestPointOnLineSegment(
  383. A: number[],
  384. B: number[],
  385. P: number[],
  386. clamp = true
  387. ) {
  388. const delta = sub(B, A)
  389. const length = len(delta)
  390. const u = div(delta, length)
  391. const pt = add(A, mul(u, pry(sub(P, A), u)))
  392. if (clamp) {
  393. const da = dist(A, pt)
  394. const db = dist(B, pt)
  395. if (db < da && da > length) return B
  396. if (da < db && db > length) return A
  397. }
  398. return pt
  399. }
  400. /**
  401. * Distance between a point and the nearest point on a line segment between A and B
  402. * @param A The start of the line segment
  403. * @param B The end of the line segment
  404. * @param P The off-line point
  405. * @param clamp Whether to clamp the point between A and B.
  406. * @returns
  407. */
  408. export function distanceToLineSegment(
  409. A: number[],
  410. B: number[],
  411. P: number[],
  412. clamp = true
  413. ) {
  414. return dist(P, nearestPointOnLineSegment(A, B, P, clamp))
  415. }
  416. /**
  417. * Get a vector d distance from A towards B.
  418. * @param A
  419. * @param B
  420. * @param d
  421. * @returns
  422. */
  423. export function nudge(A: number[], B: number[], d: number) {
  424. return add(A, mul(uni(vec(A, B)), d))
  425. }
  426. /**
  427. * Round a vector to a precision length.
  428. * @param a
  429. * @param n
  430. */
  431. export function toPrecision(a: number[], n = 4) {
  432. return [+a[0].toPrecision(n), +a[1].toPrecision(n)]
  433. }