math.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { Point } from "./types";
  2. import { LINE_CONFIRM_THRESHOLD } from "./constants";
  3. import { ExcalidrawLinearElement } from "./element/types";
  4. export const rotate = (
  5. x1: number,
  6. y1: number,
  7. x2: number,
  8. y2: number,
  9. angle: number,
  10. ): [number, number] =>
  11. // 𝑎′𝑥=(𝑎𝑥−𝑐𝑥)cos𝜃−(𝑎𝑦−𝑐𝑦)sin𝜃+𝑐𝑥
  12. // 𝑎′𝑦=(𝑎𝑥−𝑐𝑥)sin𝜃+(𝑎𝑦−𝑐𝑦)cos𝜃+𝑐𝑦.
  13. // https://math.stackexchange.com/questions/2204520/how-do-i-rotate-a-line-segment-in-a-specific-point-on-the-line
  14. [
  15. (x1 - x2) * Math.cos(angle) - (y1 - y2) * Math.sin(angle) + x2,
  16. (x1 - x2) * Math.sin(angle) + (y1 - y2) * Math.cos(angle) + y2,
  17. ];
  18. export const adjustXYWithRotation = (
  19. sides: {
  20. n?: boolean;
  21. e?: boolean;
  22. s?: boolean;
  23. w?: boolean;
  24. },
  25. x: number,
  26. y: number,
  27. angle: number,
  28. deltaX1: number,
  29. deltaY1: number,
  30. deltaX2: number,
  31. deltaY2: number,
  32. ): [number, number] => {
  33. const cos = Math.cos(angle);
  34. const sin = Math.sin(angle);
  35. if (sides.e && sides.w) {
  36. x += deltaX1 + deltaX2;
  37. } else if (sides.e) {
  38. x += deltaX1 * (1 + cos);
  39. y += deltaX1 * sin;
  40. x += deltaX2 * (1 - cos);
  41. y += deltaX2 * -sin;
  42. } else if (sides.w) {
  43. x += deltaX1 * (1 - cos);
  44. y += deltaX1 * -sin;
  45. x += deltaX2 * (1 + cos);
  46. y += deltaX2 * sin;
  47. }
  48. if (sides.n && sides.s) {
  49. y += deltaY1 + deltaY2;
  50. } else if (sides.n) {
  51. x += deltaY1 * sin;
  52. y += deltaY1 * (1 - cos);
  53. x += deltaY2 * -sin;
  54. y += deltaY2 * (1 + cos);
  55. } else if (sides.s) {
  56. x += deltaY1 * -sin;
  57. y += deltaY1 * (1 + cos);
  58. x += deltaY2 * sin;
  59. y += deltaY2 * (1 - cos);
  60. }
  61. return [x, y];
  62. };
  63. export const getFlipAdjustment = (
  64. side: "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se",
  65. nextWidth: number,
  66. nextHeight: number,
  67. nextX1: number,
  68. nextY1: number,
  69. nextX2: number,
  70. nextY2: number,
  71. finalX1: number,
  72. finalY1: number,
  73. finalX2: number,
  74. finalY2: number,
  75. needsRotation: boolean,
  76. angle: number,
  77. ): [number, number] => {
  78. const cos = Math.cos(angle);
  79. const sin = Math.sin(angle);
  80. let flipDiffX = 0;
  81. let flipDiffY = 0;
  82. if (nextWidth < 0) {
  83. if (side === "e" || side === "ne" || side === "se") {
  84. if (needsRotation) {
  85. flipDiffX += (finalX2 - nextX1) * cos;
  86. flipDiffY += (finalX2 - nextX1) * sin;
  87. } else {
  88. flipDiffX += finalX2 - nextX1;
  89. }
  90. }
  91. if (side === "w" || side === "nw" || side === "sw") {
  92. if (needsRotation) {
  93. flipDiffX += (finalX1 - nextX2) * cos;
  94. flipDiffY += (finalX1 - nextX2) * sin;
  95. } else {
  96. flipDiffX += finalX1 - nextX2;
  97. }
  98. }
  99. }
  100. if (nextHeight < 0) {
  101. if (side === "s" || side === "se" || side === "sw") {
  102. if (needsRotation) {
  103. flipDiffY += (finalY2 - nextY1) * cos;
  104. flipDiffX += (finalY2 - nextY1) * -sin;
  105. } else {
  106. flipDiffY += finalY2 - nextY1;
  107. }
  108. }
  109. if (side === "n" || side === "ne" || side === "nw") {
  110. if (needsRotation) {
  111. flipDiffY += (finalY1 - nextY2) * cos;
  112. flipDiffX += (finalY1 - nextY2) * -sin;
  113. } else {
  114. flipDiffY += finalY1 - nextY2;
  115. }
  116. }
  117. }
  118. return [flipDiffX, flipDiffY];
  119. };
  120. export const getPointOnAPath = (point: Point, path: Point[]) => {
  121. const [px, py] = point;
  122. const [start, ...other] = path;
  123. let [lastX, lastY] = start;
  124. let kLine: number = 0;
  125. let idx: number = 0;
  126. // if any item in the array is true, it means that a point is
  127. // on some segment of a line based path
  128. const retVal = other.some(([x2, y2], i) => {
  129. // we always take a line when dealing with line segments
  130. const x1 = lastX;
  131. const y1 = lastY;
  132. lastX = x2;
  133. lastY = y2;
  134. // if a point is not within the domain of the line segment
  135. // it is not on the line segment
  136. if (px < x1 || px > x2) {
  137. return false;
  138. }
  139. // check if all points lie on the same line
  140. // y1 = kx1 + b, y2 = kx2 + b
  141. // y2 - y1 = k(x2 - x2) -> k = (y2 - y1) / (x2 - x1)
  142. // coefficient for the line (p0, p1)
  143. const kL = (y2 - y1) / (x2 - x1);
  144. // coefficient for the line segment (p0, point)
  145. const kP1 = (py - y1) / (px - x1);
  146. // coefficient for the line segment (point, p1)
  147. const kP2 = (py - y2) / (px - x2);
  148. // because we are basing both lines from the same starting point
  149. // the only option for collinearity is having same coefficients
  150. // using it for floating point comparisons
  151. const epsilon = 0.3;
  152. // if coefficient is more than an arbitrary epsilon,
  153. // these lines are nor collinear
  154. if (Math.abs(kP1 - kL) > epsilon && Math.abs(kP2 - kL) > epsilon) {
  155. return false;
  156. }
  157. // store the coefficient because we are goint to need it
  158. kLine = kL;
  159. idx = i;
  160. return true;
  161. });
  162. // Return a coordinate that is always on the line segment
  163. if (retVal === true) {
  164. return { x: point[0], y: kLine * point[0], segment: idx };
  165. }
  166. return null;
  167. };
  168. export const distance2d = (x1: number, y1: number, x2: number, y2: number) => {
  169. const xd = x2 - x1;
  170. const yd = y2 - y1;
  171. return Math.hypot(xd, yd);
  172. };
  173. export const centerPoint = (a: Point, b: Point): Point => {
  174. return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
  175. };
  176. // Checks if the first and last point are close enough
  177. // to be considered a loop
  178. export const isPathALoop = (
  179. points: ExcalidrawLinearElement["points"],
  180. ): boolean => {
  181. if (points.length >= 3) {
  182. const [firstPoint, lastPoint] = [points[0], points[points.length - 1]];
  183. return (
  184. distance2d(firstPoint[0], firstPoint[1], lastPoint[0], lastPoint[1]) <=
  185. LINE_CONFIRM_THRESHOLD
  186. );
  187. }
  188. return false;
  189. };
  190. // Draw a line from the point to the right till infiinty
  191. // Check how many lines of the polygon does this infinite line intersects with
  192. // If the number of intersections is odd, point is in the polygon
  193. export const isPointInPolygon = (
  194. points: Point[],
  195. x: number,
  196. y: number,
  197. ): boolean => {
  198. const vertices = points.length;
  199. // There must be at least 3 vertices in polygon
  200. if (vertices < 3) {
  201. return false;
  202. }
  203. const extreme: Point = [Number.MAX_SAFE_INTEGER, y];
  204. const p: Point = [x, y];
  205. let count = 0;
  206. for (let i = 0; i < vertices; i++) {
  207. const current = points[i];
  208. const next = points[(i + 1) % vertices];
  209. if (doSegmentsIntersect(current, next, p, extreme)) {
  210. if (orderedColinearOrientation(current, p, next) === 0) {
  211. return isPointWithinBounds(current, p, next);
  212. }
  213. count++;
  214. }
  215. }
  216. // true if count is off
  217. return count % 2 === 1;
  218. };
  219. // Returns whether `q` lies inside the segment/rectangle defined by `p` and `r`.
  220. // This is an approximation to "does `q` lie on a segment `pr`" check.
  221. const isPointWithinBounds = (p: Point, q: Point, r: Point) => {
  222. return (
  223. q[0] <= Math.max(p[0], r[0]) &&
  224. q[0] >= Math.min(p[0], r[0]) &&
  225. q[1] <= Math.max(p[1], r[1]) &&
  226. q[1] >= Math.min(p[1], r[1])
  227. );
  228. };
  229. // For the ordered points p, q, r, return
  230. // 0 if p, q, r are colinear
  231. // 1 if Clockwise
  232. // 2 if counterclickwise
  233. const orderedColinearOrientation = (p: Point, q: Point, r: Point) => {
  234. const val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
  235. if (val === 0) {
  236. return 0;
  237. }
  238. return val > 0 ? 1 : 2;
  239. };
  240. // Check is p1q1 intersects with p2q2
  241. const doSegmentsIntersect = (p1: Point, q1: Point, p2: Point, q2: Point) => {
  242. const o1 = orderedColinearOrientation(p1, q1, p2);
  243. const o2 = orderedColinearOrientation(p1, q1, q2);
  244. const o3 = orderedColinearOrientation(p2, q2, p1);
  245. const o4 = orderedColinearOrientation(p2, q2, q1);
  246. if (o1 !== o2 && o3 !== o4) {
  247. return true;
  248. }
  249. // p1, q1 and p2 are colinear and p2 lies on segment p1q1
  250. if (o1 === 0 && isPointWithinBounds(p1, p2, q1)) {
  251. return true;
  252. }
  253. // p1, q1 and p2 are colinear and q2 lies on segment p1q1
  254. if (o2 === 0 && isPointWithinBounds(p1, q2, q1)) {
  255. return true;
  256. }
  257. // p2, q2 and p1 are colinear and p1 lies on segment p2q2
  258. if (o3 === 0 && isPointWithinBounds(p2, p1, q2)) {
  259. return true;
  260. }
  261. // p2, q2 and q1 are colinear and q1 lies on segment p2q2
  262. if (o4 === 0 && isPointWithinBounds(p2, q1, q2)) {
  263. return true;
  264. }
  265. return false;
  266. };
  267. export const getGridPoint = (
  268. x: number,
  269. y: number,
  270. gridSize: number | null,
  271. ): [number, number] => {
  272. if (gridSize) {
  273. return [
  274. Math.round(x / gridSize) * gridSize,
  275. Math.round(y / gridSize) * gridSize,
  276. ];
  277. }
  278. return [x, y];
  279. };