point.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * Convert the coordiante object to a point.
  61. *
  62. * @param coords The coordinate object with x and y properties
  63. * @returns
  64. */
  65. export function pointFromCoords<Point extends GlobalPoint | LocalPoint>({
  66. x,
  67. y,
  68. }: {
  69. x: number;
  70. y: number;
  71. }) {
  72. return [x, y] as Point;
  73. }
  74. /**
  75. * Checks if the provided value has the shape of a Point.
  76. *
  77. * @param p The value to attempt verification on
  78. * @returns TRUE if the provided value has the shape of a local or global point
  79. */
  80. export function isPoint(p: unknown): p is LocalPoint | GlobalPoint {
  81. return (
  82. Array.isArray(p) &&
  83. p.length === 2 &&
  84. typeof p[0] === "number" &&
  85. !isNaN(p[0]) &&
  86. typeof p[1] === "number" &&
  87. !isNaN(p[1])
  88. );
  89. }
  90. /**
  91. * Compare two points coordinate-by-coordinate and if
  92. * they are closer than INVERSE_PRECISION it returns TRUE.
  93. *
  94. * @param a Point The first point to compare
  95. * @param b Point The second point to compare
  96. * @returns TRUE if the points are sufficiently close to each other
  97. */
  98. export function pointsEqual<Point extends GlobalPoint | LocalPoint>(
  99. a: Point,
  100. b: Point,
  101. ): boolean {
  102. const abs = Math.abs;
  103. return abs(a[0] - b[0]) < PRECISION && abs(a[1] - b[1]) < PRECISION;
  104. }
  105. /**
  106. * Roate a point by [angle] radians.
  107. *
  108. * @param point The point to rotate
  109. * @param center The point to rotate around, the center point
  110. * @param angle The radians to rotate the point by
  111. * @returns The rotated point
  112. */
  113. export function pointRotateRads<Point extends GlobalPoint | LocalPoint>(
  114. [x, y]: Point,
  115. [cx, cy]: Point,
  116. angle: Radians,
  117. ): Point {
  118. return pointFrom(
  119. (x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
  120. (x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy,
  121. );
  122. }
  123. /**
  124. * Roate a point by [angle] degree.
  125. *
  126. * @param point The point to rotate
  127. * @param center The point to rotate around, the center point
  128. * @param angle The degree to rotate the point by
  129. * @returns The rotated point
  130. */
  131. export function pointRotateDegs<Point extends GlobalPoint | LocalPoint>(
  132. point: Point,
  133. center: Point,
  134. angle: Degrees,
  135. ): Point {
  136. return pointRotateRads(point, center, degreesToRadians(angle));
  137. }
  138. /**
  139. * Translate a point by a vector.
  140. *
  141. * WARNING: This is not for translating Excalidraw element points!
  142. * You need to account for rotation on base coordinates
  143. * on your own.
  144. * CONSIDER USING AN APPROPRIATE ELEMENT-AWARE TRANSLATE!
  145. *
  146. * @param p The point to apply the translation on
  147. * @param v The vector to translate by
  148. * @returns
  149. */
  150. // TODO 99% of use is translating between global and local coords, which need to be formalized
  151. export function pointTranslate<
  152. From extends GlobalPoint | LocalPoint,
  153. To extends GlobalPoint | LocalPoint,
  154. >(p: From, v: Vector = [0, 0] as Vector): To {
  155. return pointFrom(p[0] + v[0], p[1] + v[1]);
  156. }
  157. /**
  158. * Find the center point at equal distance from both points.
  159. *
  160. * @param a One of the points to create the middle point for
  161. * @param b The other point to create the middle point for
  162. * @returns The middle point
  163. */
  164. export function pointCenter<P extends LocalPoint | GlobalPoint>(a: P, b: P): P {
  165. return pointFrom((a[0] + b[0]) / 2, (a[1] + b[1]) / 2);
  166. }
  167. /**
  168. * Add together two points by their coordinates like you'd apply a translation
  169. * to a point by a vector.
  170. *
  171. * @param a One point to act as a basis
  172. * @param b The other point to act like the vector to translate by
  173. * @returns
  174. */
  175. export function pointAdd<Point extends LocalPoint | GlobalPoint>(
  176. a: Point,
  177. b: Point,
  178. ): Point {
  179. return pointFrom(a[0] + b[0], a[1] + b[1]);
  180. }
  181. /**
  182. * Subtract a point from another point like you'd translate a point by an
  183. * invese vector.
  184. *
  185. * @param a The point to translate
  186. * @param b The point which will act like a vector
  187. * @returns The resulting point
  188. */
  189. export function pointSubtract<Point extends LocalPoint | GlobalPoint>(
  190. a: Point,
  191. b: Point,
  192. ): Point {
  193. return pointFrom(a[0] - b[0], a[1] - b[1]);
  194. }
  195. /**
  196. * Calculate the distance between two points.
  197. *
  198. * @param a First point
  199. * @param b Second point
  200. * @returns The euclidean distance between the two points.
  201. */
  202. export function pointDistance<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. * Calculate the squared distance between two points.
  210. *
  211. * Note: Use this if you only compare distances, it saves a square root.
  212. *
  213. * @param a First point
  214. * @param b Second point
  215. * @returns The euclidean distance between the two points.
  216. */
  217. export function pointDistanceSq<P extends LocalPoint | GlobalPoint>(
  218. a: P,
  219. b: P,
  220. ): number {
  221. const xDiff = b[0] - a[0];
  222. const yDiff = b[1] - a[1];
  223. return xDiff * xDiff + yDiff * yDiff;
  224. }
  225. /**
  226. * Scale a point from a given origin by the multiplier.
  227. *
  228. * @param p The point to scale
  229. * @param mid The origin to scale from
  230. * @param multiplier The scaling factor
  231. * @returns
  232. */
  233. export const pointScaleFromOrigin = <P extends GlobalPoint | LocalPoint>(
  234. p: P,
  235. mid: P,
  236. multiplier: number,
  237. ) => pointTranslate(mid, vectorScale(vectorFromPoint(p, mid), multiplier));
  238. /**
  239. * Returns whether `q` lies inside the segment/rectangle defined by `p` and `r`.
  240. * This is an approximation to "does `q` lie on a segment `pr`" check.
  241. *
  242. * @param p The first point to compare against
  243. * @param q The actual point this function checks whether is in between
  244. * @param r The other point to compare against
  245. * @returns TRUE if q is indeed between p and r
  246. */
  247. export const isPointWithinBounds = <P extends GlobalPoint | LocalPoint>(
  248. p: P,
  249. q: P,
  250. r: P,
  251. ) => {
  252. return (
  253. q[0] <= Math.max(p[0], r[0]) &&
  254. q[0] >= Math.min(p[0], r[0]) &&
  255. q[1] <= Math.max(p[1], r[1]) &&
  256. q[1] >= Math.min(p[1], r[1])
  257. );
  258. };