point.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { degreesToRadians } from "./angle";
  2. import type {
  3. LocalPoint,
  4. GlobalPoint,
  5. Radians,
  6. Degrees,
  7. Vector,
  8. } from "./types";
  9. import { PRECISION } from "./utils";
  10. import { vectorFromPoint, vectorScale } from "./vector";
  11. /**
  12. * Create a properly typed Point instance from the X and Y coordinates.
  13. *
  14. * @param x The X coordinate
  15. * @param y The Y coordinate
  16. * @returns The branded and created point
  17. */
  18. export function pointFrom<Point extends GlobalPoint | LocalPoint>(
  19. x: number,
  20. y: number,
  21. ): Point {
  22. return [x, y] as Point;
  23. }
  24. /**
  25. * Converts and remaps an array containing a pair of numbers to Point.
  26. *
  27. * @param numberArray The number array to check and to convert to Point
  28. * @returns The point instance
  29. */
  30. export function pointFromArray<Point extends GlobalPoint | LocalPoint>(
  31. numberArray: number[],
  32. ): Point | undefined {
  33. return numberArray.length === 2
  34. ? pointFrom<Point>(numberArray[0], numberArray[1])
  35. : undefined;
  36. }
  37. /**
  38. * Converts and remaps a pair of numbers to Point.
  39. *
  40. * @param pair A number pair to convert to Point
  41. * @returns The point instance
  42. */
  43. export function pointFromPair<Point extends GlobalPoint | LocalPoint>(
  44. pair: [number, number],
  45. ): Point {
  46. return pair as Point;
  47. }
  48. /**
  49. * Convert a vector to a point.
  50. *
  51. * @param v The vector to convert
  52. * @returns The point the vector points at with origin 0,0
  53. */
  54. export function pointFromVector<P extends GlobalPoint | LocalPoint>(
  55. v: Vector,
  56. ): P {
  57. return v as unknown as P;
  58. }
  59. /**
  60. * Checks if the provided value has the shape of a Point.
  61. *
  62. * @param p The value to attempt verification on
  63. * @returns TRUE if the provided value has the shape of a local or global point
  64. */
  65. export function isPoint(p: unknown): p is LocalPoint | GlobalPoint {
  66. return (
  67. Array.isArray(p) &&
  68. p.length === 2 &&
  69. typeof p[0] === "number" &&
  70. !isNaN(p[0]) &&
  71. typeof p[1] === "number" &&
  72. !isNaN(p[1])
  73. );
  74. }
  75. /**
  76. * Compare two points coordinate-by-coordinate and if
  77. * they are closer than INVERSE_PRECISION it returns TRUE.
  78. *
  79. * @param a Point The first point to compare
  80. * @param b Point The second point to compare
  81. * @returns TRUE if the points are sufficiently close to each other
  82. */
  83. export function pointsEqual<Point extends GlobalPoint | LocalPoint>(
  84. a: Point,
  85. b: Point,
  86. ): boolean {
  87. const abs = Math.abs;
  88. return abs(a[0] - b[0]) < PRECISION && abs(a[1] - b[1]) < PRECISION;
  89. }
  90. /**
  91. * Roate a point by [angle] radians.
  92. *
  93. * @param point The point to rotate
  94. * @param center The point to rotate around, the center point
  95. * @param angle The radians to rotate the point by
  96. * @returns The rotated point
  97. */
  98. export function pointRotateRads<Point extends GlobalPoint | LocalPoint>(
  99. [x, y]: Point,
  100. [cx, cy]: Point,
  101. angle: Radians,
  102. ): Point {
  103. return pointFrom(
  104. (x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
  105. (x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy,
  106. );
  107. }
  108. /**
  109. * Roate a point by [angle] degree.
  110. *
  111. * @param point The point to rotate
  112. * @param center The point to rotate around, the center point
  113. * @param angle The degree to rotate the point by
  114. * @returns The rotated point
  115. */
  116. export function pointRotateDegs<Point extends GlobalPoint | LocalPoint>(
  117. point: Point,
  118. center: Point,
  119. angle: Degrees,
  120. ): Point {
  121. return pointRotateRads(point, center, degreesToRadians(angle));
  122. }
  123. /**
  124. * Translate a point by a vector.
  125. *
  126. * WARNING: This is not for translating Excalidraw element points!
  127. * You need to account for rotation on base coordinates
  128. * on your own.
  129. * CONSIDER USING AN APPROPRIATE ELEMENT-AWARE TRANSLATE!
  130. *
  131. * @param p The point to apply the translation on
  132. * @param v The vector to translate by
  133. * @returns
  134. */
  135. // TODO 99% of use is translating between global and local coords, which need to be formalized
  136. export function pointTranslate<
  137. From extends GlobalPoint | LocalPoint,
  138. To extends GlobalPoint | LocalPoint,
  139. >(p: From, v: Vector = [0, 0] as Vector): To {
  140. return pointFrom(p[0] + v[0], p[1] + v[1]);
  141. }
  142. /**
  143. * Find the center point at equal distance from both points.
  144. *
  145. * @param a One of the points to create the middle point for
  146. * @param b The other point to create the middle point for
  147. * @returns The middle point
  148. */
  149. export function pointCenter<P extends LocalPoint | GlobalPoint>(a: P, b: P): P {
  150. return pointFrom((a[0] + b[0]) / 2, (a[1] + b[1]) / 2);
  151. }
  152. /**
  153. * Add together two points by their coordinates like you'd apply a translation
  154. * to a point by a vector.
  155. *
  156. * @param a One point to act as a basis
  157. * @param b The other point to act like the vector to translate by
  158. * @returns
  159. */
  160. export function pointAdd<Point extends LocalPoint | GlobalPoint>(
  161. a: Point,
  162. b: Point,
  163. ): Point {
  164. return pointFrom(a[0] + b[0], a[1] + b[1]);
  165. }
  166. /**
  167. * Subtract a point from another point like you'd translate a point by an
  168. * invese vector.
  169. *
  170. * @param a The point to translate
  171. * @param b The point which will act like a vector
  172. * @returns The resulting point
  173. */
  174. export function pointSubtract<Point extends LocalPoint | GlobalPoint>(
  175. a: Point,
  176. b: Point,
  177. ): Point {
  178. return pointFrom(a[0] - b[0], a[1] - b[1]);
  179. }
  180. /**
  181. * Calculate the distance between two points.
  182. *
  183. * @param a First point
  184. * @param b Second point
  185. * @returns The euclidean distance between the two points.
  186. */
  187. export function pointDistance<P extends LocalPoint | GlobalPoint>(
  188. a: P,
  189. b: P,
  190. ): number {
  191. return Math.hypot(b[0] - a[0], b[1] - a[1]);
  192. }
  193. /**
  194. * Calculate the squared distance between two points.
  195. *
  196. * Note: Use this if you only compare distances, it saves a square root.
  197. *
  198. * @param a First point
  199. * @param b Second point
  200. * @returns The euclidean distance between the two points.
  201. */
  202. export function pointDistanceSq<P extends LocalPoint | GlobalPoint>(
  203. a: P,
  204. b: P,
  205. ): number {
  206. return Math.hypot(b[0] - a[0], b[1] - a[1]);
  207. }
  208. /**
  209. * Scale a point from a given origin by the multiplier.
  210. *
  211. * @param p The point to scale
  212. * @param mid The origin to scale from
  213. * @param multiplier The scaling factor
  214. * @returns
  215. */
  216. export const pointScaleFromOrigin = <P extends GlobalPoint | LocalPoint>(
  217. p: P,
  218. mid: P,
  219. multiplier: number,
  220. ) => pointTranslate(mid, vectorScale(vectorFromPoint(p, mid), multiplier));
  221. /**
  222. * Returns whether `q` lies inside the segment/rectangle defined by `p` and `r`.
  223. * This is an approximation to "does `q` lie on a segment `pr`" check.
  224. *
  225. * @param p The first point to compare against
  226. * @param q The actual point this function checks whether is in between
  227. * @param r The other point to compare against
  228. * @returns TRUE if q is indeed between p and r
  229. */
  230. export const isPointWithinBounds = <P extends GlobalPoint | LocalPoint>(
  231. p: P,
  232. q: P,
  233. r: P,
  234. ) => {
  235. return (
  236. q[0] <= Math.max(p[0], r[0]) &&
  237. q[0] >= Math.min(p[0], r[0]) &&
  238. q[1] <= Math.max(p[1], r[1]) &&
  239. q[1] >= Math.min(p[1], r[1])
  240. );
  241. };