math.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import { NormalizedZoomValue, Point, Zoom } from "./types";
  2. import {
  3. DEFAULT_ADAPTIVE_RADIUS,
  4. LINE_CONFIRM_THRESHOLD,
  5. DEFAULT_PROPORTIONAL_RADIUS,
  6. ROUNDNESS,
  7. } from "./constants";
  8. import {
  9. ExcalidrawElement,
  10. ExcalidrawLinearElement,
  11. NonDeleted,
  12. } from "./element/types";
  13. import { getCurvePathOps } from "./element/bounds";
  14. import { Mutable } from "./utility-types";
  15. import { ShapeCache } from "./scene/ShapeCache";
  16. export const rotate = (
  17. x1: number,
  18. y1: number,
  19. x2: number,
  20. y2: number,
  21. angle: number,
  22. ): [number, number] =>
  23. // 𝑎′𝑥=(𝑎𝑥−𝑐𝑥)cos𝜃−(𝑎𝑦−𝑐𝑦)sin𝜃+𝑐𝑥
  24. // 𝑎′𝑦=(𝑎𝑥−𝑐𝑥)sin𝜃+(𝑎𝑦−𝑐𝑦)cos𝜃+𝑐𝑦.
  25. // https://math.stackexchange.com/questions/2204520/how-do-i-rotate-a-line-segment-in-a-specific-point-on-the-line
  26. [
  27. (x1 - x2) * Math.cos(angle) - (y1 - y2) * Math.sin(angle) + x2,
  28. (x1 - x2) * Math.sin(angle) + (y1 - y2) * Math.cos(angle) + y2,
  29. ];
  30. export const rotatePoint = (
  31. point: Point,
  32. center: Point,
  33. angle: number,
  34. ): [number, number] => rotate(point[0], point[1], center[0], center[1], angle);
  35. export const adjustXYWithRotation = (
  36. sides: {
  37. n?: boolean;
  38. e?: boolean;
  39. s?: boolean;
  40. w?: boolean;
  41. },
  42. x: number,
  43. y: number,
  44. angle: number,
  45. deltaX1: number,
  46. deltaY1: number,
  47. deltaX2: number,
  48. deltaY2: number,
  49. ): [number, number] => {
  50. const cos = Math.cos(angle);
  51. const sin = Math.sin(angle);
  52. if (sides.e && sides.w) {
  53. x += deltaX1 + deltaX2;
  54. } else if (sides.e) {
  55. x += deltaX1 * (1 + cos);
  56. y += deltaX1 * sin;
  57. x += deltaX2 * (1 - cos);
  58. y += deltaX2 * -sin;
  59. } else if (sides.w) {
  60. x += deltaX1 * (1 - cos);
  61. y += deltaX1 * -sin;
  62. x += deltaX2 * (1 + cos);
  63. y += deltaX2 * sin;
  64. }
  65. if (sides.n && sides.s) {
  66. y += deltaY1 + deltaY2;
  67. } else if (sides.n) {
  68. x += deltaY1 * sin;
  69. y += deltaY1 * (1 - cos);
  70. x += deltaY2 * -sin;
  71. y += deltaY2 * (1 + cos);
  72. } else if (sides.s) {
  73. x += deltaY1 * -sin;
  74. y += deltaY1 * (1 + cos);
  75. x += deltaY2 * sin;
  76. y += deltaY2 * (1 - cos);
  77. }
  78. return [x, y];
  79. };
  80. export const getPointOnAPath = (point: Point, path: Point[]) => {
  81. const [px, py] = point;
  82. const [start, ...other] = path;
  83. let [lastX, lastY] = start;
  84. let kLine: number = 0;
  85. let idx: number = 0;
  86. // if any item in the array is true, it means that a point is
  87. // on some segment of a line based path
  88. const retVal = other.some(([x2, y2], i) => {
  89. // we always take a line when dealing with line segments
  90. const x1 = lastX;
  91. const y1 = lastY;
  92. lastX = x2;
  93. lastY = y2;
  94. // if a point is not within the domain of the line segment
  95. // it is not on the line segment
  96. if (px < x1 || px > x2) {
  97. return false;
  98. }
  99. // check if all points lie on the same line
  100. // y1 = kx1 + b, y2 = kx2 + b
  101. // y2 - y1 = k(x2 - x2) -> k = (y2 - y1) / (x2 - x1)
  102. // coefficient for the line (p0, p1)
  103. const kL = (y2 - y1) / (x2 - x1);
  104. // coefficient for the line segment (p0, point)
  105. const kP1 = (py - y1) / (px - x1);
  106. // coefficient for the line segment (point, p1)
  107. const kP2 = (py - y2) / (px - x2);
  108. // because we are basing both lines from the same starting point
  109. // the only option for collinearity is having same coefficients
  110. // using it for floating point comparisons
  111. const epsilon = 0.3;
  112. // if coefficient is more than an arbitrary epsilon,
  113. // these lines are nor collinear
  114. if (Math.abs(kP1 - kL) > epsilon && Math.abs(kP2 - kL) > epsilon) {
  115. return false;
  116. }
  117. // store the coefficient because we are goint to need it
  118. kLine = kL;
  119. idx = i;
  120. return true;
  121. });
  122. // Return a coordinate that is always on the line segment
  123. if (retVal === true) {
  124. return { x: point[0], y: kLine * point[0], segment: idx };
  125. }
  126. return null;
  127. };
  128. export const distance2d = (x1: number, y1: number, x2: number, y2: number) => {
  129. const xd = x2 - x1;
  130. const yd = y2 - y1;
  131. return Math.hypot(xd, yd);
  132. };
  133. export const centerPoint = (a: Point, b: Point): Point => {
  134. return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
  135. };
  136. // Checks if the first and last point are close enough
  137. // to be considered a loop
  138. export const isPathALoop = (
  139. points: ExcalidrawLinearElement["points"],
  140. /** supply if you want the loop detection to account for current zoom */
  141. zoomValue: Zoom["value"] = 1 as NormalizedZoomValue,
  142. ): boolean => {
  143. if (points.length >= 3) {
  144. const [first, last] = [points[0], points[points.length - 1]];
  145. const distance = distance2d(first[0], first[1], last[0], last[1]);
  146. // Adjusting LINE_CONFIRM_THRESHOLD to current zoom so that when zoomed in
  147. // really close we make the threshold smaller, and vice versa.
  148. return distance <= LINE_CONFIRM_THRESHOLD / zoomValue;
  149. }
  150. return false;
  151. };
  152. // Draw a line from the point to the right till infiinty
  153. // Check how many lines of the polygon does this infinite line intersects with
  154. // If the number of intersections is odd, point is in the polygon
  155. export const isPointInPolygon = (
  156. points: Point[],
  157. x: number,
  158. y: number,
  159. ): boolean => {
  160. const vertices = points.length;
  161. // There must be at least 3 vertices in polygon
  162. if (vertices < 3) {
  163. return false;
  164. }
  165. const extreme: Point = [Number.MAX_SAFE_INTEGER, y];
  166. const p: Point = [x, y];
  167. let count = 0;
  168. for (let i = 0; i < vertices; i++) {
  169. const current = points[i];
  170. const next = points[(i + 1) % vertices];
  171. if (doSegmentsIntersect(current, next, p, extreme)) {
  172. if (orderedColinearOrientation(current, p, next) === 0) {
  173. return isPointWithinBounds(current, p, next);
  174. }
  175. count++;
  176. }
  177. }
  178. // true if count is off
  179. return count % 2 === 1;
  180. };
  181. // Returns whether `q` lies inside the segment/rectangle defined by `p` and `r`.
  182. // This is an approximation to "does `q` lie on a segment `pr`" check.
  183. export const isPointWithinBounds = (p: Point, q: Point, r: Point) => {
  184. return (
  185. q[0] <= Math.max(p[0], r[0]) &&
  186. q[0] >= Math.min(p[0], r[0]) &&
  187. q[1] <= Math.max(p[1], r[1]) &&
  188. q[1] >= Math.min(p[1], r[1])
  189. );
  190. };
  191. // For the ordered points p, q, r, return
  192. // 0 if p, q, r are colinear
  193. // 1 if Clockwise
  194. // 2 if counterclickwise
  195. const orderedColinearOrientation = (p: Point, q: Point, r: Point) => {
  196. const val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
  197. if (val === 0) {
  198. return 0;
  199. }
  200. return val > 0 ? 1 : 2;
  201. };
  202. // Check is p1q1 intersects with p2q2
  203. const doSegmentsIntersect = (p1: Point, q1: Point, p2: Point, q2: Point) => {
  204. const o1 = orderedColinearOrientation(p1, q1, p2);
  205. const o2 = orderedColinearOrientation(p1, q1, q2);
  206. const o3 = orderedColinearOrientation(p2, q2, p1);
  207. const o4 = orderedColinearOrientation(p2, q2, q1);
  208. if (o1 !== o2 && o3 !== o4) {
  209. return true;
  210. }
  211. // p1, q1 and p2 are colinear and p2 lies on segment p1q1
  212. if (o1 === 0 && isPointWithinBounds(p1, p2, q1)) {
  213. return true;
  214. }
  215. // p1, q1 and p2 are colinear and q2 lies on segment p1q1
  216. if (o2 === 0 && isPointWithinBounds(p1, q2, q1)) {
  217. return true;
  218. }
  219. // p2, q2 and p1 are colinear and p1 lies on segment p2q2
  220. if (o3 === 0 && isPointWithinBounds(p2, p1, q2)) {
  221. return true;
  222. }
  223. // p2, q2 and q1 are colinear and q1 lies on segment p2q2
  224. if (o4 === 0 && isPointWithinBounds(p2, q1, q2)) {
  225. return true;
  226. }
  227. return false;
  228. };
  229. // TODO: Rounding this point causes some shake when free drawing
  230. export const getGridPoint = (
  231. x: number,
  232. y: number,
  233. gridSize: number | null,
  234. ): [number, number] => {
  235. if (gridSize) {
  236. return [
  237. Math.round(x / gridSize) * gridSize,
  238. Math.round(y / gridSize) * gridSize,
  239. ];
  240. }
  241. return [x, y];
  242. };
  243. export const getCornerRadius = (x: number, element: ExcalidrawElement) => {
  244. if (
  245. element.roundness?.type === ROUNDNESS.PROPORTIONAL_RADIUS ||
  246. element.roundness?.type === ROUNDNESS.LEGACY
  247. ) {
  248. return x * DEFAULT_PROPORTIONAL_RADIUS;
  249. }
  250. if (element.roundness?.type === ROUNDNESS.ADAPTIVE_RADIUS) {
  251. const fixedRadiusSize = element.roundness?.value ?? DEFAULT_ADAPTIVE_RADIUS;
  252. const CUTOFF_SIZE = fixedRadiusSize / DEFAULT_PROPORTIONAL_RADIUS;
  253. if (x <= CUTOFF_SIZE) {
  254. return x * DEFAULT_PROPORTIONAL_RADIUS;
  255. }
  256. return fixedRadiusSize;
  257. }
  258. return 0;
  259. };
  260. export const getControlPointsForBezierCurve = (
  261. element: NonDeleted<ExcalidrawLinearElement>,
  262. endPoint: Point,
  263. ) => {
  264. const shape = ShapeCache.generateElementShape(element);
  265. if (!shape) {
  266. return null;
  267. }
  268. const ops = getCurvePathOps(shape[0]);
  269. let currentP: Mutable<Point> = [0, 0];
  270. let index = 0;
  271. let minDistance = Infinity;
  272. let controlPoints: Mutable<Point>[] | null = null;
  273. while (index < ops.length) {
  274. const { op, data } = ops[index];
  275. if (op === "move") {
  276. currentP = data as unknown as Mutable<Point>;
  277. }
  278. if (op === "bcurveTo") {
  279. const p0 = currentP;
  280. const p1 = [data[0], data[1]] as Mutable<Point>;
  281. const p2 = [data[2], data[3]] as Mutable<Point>;
  282. const p3 = [data[4], data[5]] as Mutable<Point>;
  283. const distance = distance2d(p3[0], p3[1], endPoint[0], endPoint[1]);
  284. if (distance < minDistance) {
  285. minDistance = distance;
  286. controlPoints = [p0, p1, p2, p3];
  287. }
  288. currentP = p3;
  289. }
  290. index++;
  291. }
  292. return controlPoints;
  293. };
  294. export const getBezierXY = (
  295. p0: Point,
  296. p1: Point,
  297. p2: Point,
  298. p3: Point,
  299. t: number,
  300. ) => {
  301. const equation = (t: number, idx: number) =>
  302. Math.pow(1 - t, 3) * p3[idx] +
  303. 3 * t * Math.pow(1 - t, 2) * p2[idx] +
  304. 3 * Math.pow(t, 2) * (1 - t) * p1[idx] +
  305. p0[idx] * Math.pow(t, 3);
  306. const tx = equation(t, 0);
  307. const ty = equation(t, 1);
  308. return [tx, ty];
  309. };
  310. export const getPointsInBezierCurve = (
  311. element: NonDeleted<ExcalidrawLinearElement>,
  312. endPoint: Point,
  313. ) => {
  314. const controlPoints: Mutable<Point>[] = getControlPointsForBezierCurve(
  315. element,
  316. endPoint,
  317. )!;
  318. if (!controlPoints) {
  319. return [];
  320. }
  321. const pointsOnCurve: Mutable<Point>[] = [];
  322. let t = 1;
  323. // Take 20 points on curve for better accuracy
  324. while (t > 0) {
  325. const point = getBezierXY(
  326. controlPoints[0],
  327. controlPoints[1],
  328. controlPoints[2],
  329. controlPoints[3],
  330. t,
  331. );
  332. pointsOnCurve.push([point[0], point[1]]);
  333. t -= 0.05;
  334. }
  335. if (pointsOnCurve.length) {
  336. if (arePointsEqual(pointsOnCurve.at(-1)!, endPoint)) {
  337. pointsOnCurve.push([endPoint[0], endPoint[1]]);
  338. }
  339. }
  340. return pointsOnCurve;
  341. };
  342. export const getBezierCurveArcLengths = (
  343. element: NonDeleted<ExcalidrawLinearElement>,
  344. endPoint: Point,
  345. ) => {
  346. const arcLengths: number[] = [];
  347. arcLengths[0] = 0;
  348. const points = getPointsInBezierCurve(element, endPoint);
  349. let index = 0;
  350. let distance = 0;
  351. while (index < points.length - 1) {
  352. const segmentDistance = distance2d(
  353. points[index][0],
  354. points[index][1],
  355. points[index + 1][0],
  356. points[index + 1][1],
  357. );
  358. distance += segmentDistance;
  359. arcLengths.push(distance);
  360. index++;
  361. }
  362. return arcLengths;
  363. };
  364. export const getBezierCurveLength = (
  365. element: NonDeleted<ExcalidrawLinearElement>,
  366. endPoint: Point,
  367. ) => {
  368. const arcLengths = getBezierCurveArcLengths(element, endPoint);
  369. return arcLengths.at(-1) as number;
  370. };
  371. // This maps interval to actual interval t on the curve so that when t = 0.5, its actually the point at 50% of the length
  372. export const mapIntervalToBezierT = (
  373. element: NonDeleted<ExcalidrawLinearElement>,
  374. endPoint: Point,
  375. interval: number, // The interval between 0 to 1 for which you want to find the point on the curve,
  376. ) => {
  377. const arcLengths = getBezierCurveArcLengths(element, endPoint);
  378. const pointsCount = arcLengths.length - 1;
  379. const curveLength = arcLengths.at(-1) as number;
  380. const targetLength = interval * curveLength;
  381. let low = 0;
  382. let high = pointsCount;
  383. let index = 0;
  384. // Doing a binary search to find the largest length that is less than the target length
  385. while (low < high) {
  386. index = Math.floor(low + (high - low) / 2);
  387. if (arcLengths[index] < targetLength) {
  388. low = index + 1;
  389. } else {
  390. high = index;
  391. }
  392. }
  393. if (arcLengths[index] > targetLength) {
  394. index--;
  395. }
  396. if (arcLengths[index] === targetLength) {
  397. return index / pointsCount;
  398. }
  399. return (
  400. 1 -
  401. (index +
  402. (targetLength - arcLengths[index]) /
  403. (arcLengths[index + 1] - arcLengths[index])) /
  404. pointsCount
  405. );
  406. };
  407. export const arePointsEqual = (p1: Point, p2: Point) => {
  408. return p1[0] === p2[0] && p1[1] === p2[1];
  409. };
  410. export const isRightAngle = (angle: number) => {
  411. // if our angles were mathematically accurate, we could just check
  412. //
  413. // angle % (Math.PI / 2) === 0
  414. //
  415. // but since we're in floating point land, we need to round.
  416. //
  417. // Below, after dividing by Math.PI, a multiple of 0.5 indicates a right
  418. // angle, which we can check with modulo after rounding.
  419. return Math.round((angle / Math.PI) * 10000) % 5000 === 0;
  420. };
  421. // Given two ranges, return if the two ranges overlap with each other
  422. // e.g. [1, 3] overlaps with [2, 4] while [1, 3] does not overlap with [4, 5]
  423. export const rangesOverlap = (
  424. [a0, a1]: [number, number],
  425. [b0, b1]: [number, number],
  426. ) => {
  427. if (a0 <= b0) {
  428. return a1 >= b0;
  429. }
  430. if (a0 >= b0) {
  431. return b1 >= a0;
  432. }
  433. return false;
  434. };
  435. // Given two ranges,return ther intersection of the two ranges if any
  436. // e.g. the intersection of [1, 3] and [2, 4] is [2, 3]
  437. export const rangeIntersection = (
  438. rangeA: [number, number],
  439. rangeB: [number, number],
  440. ): [number, number] | null => {
  441. const rangeStart = Math.max(rangeA[0], rangeB[0]);
  442. const rangeEnd = Math.min(rangeA[1], rangeB[1]);
  443. if (rangeStart <= rangeEnd) {
  444. return [rangeStart, rangeEnd];
  445. }
  446. return null;
  447. };
  448. export const isValueInRange = (value: number, min: number, max: number) => {
  449. return value >= min && value <= max;
  450. };