math.ts 8.6 KB

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