MathUtil.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. import Constant from "../Constant";
  2. import bezierUtil from "./bezierUtil.js";
  3. export default class MathUtil {
  4. constructor() {}
  5. getFixed(num, decimal) {
  6. if (!decimal) {
  7. decimal = 5;
  8. }
  9. // return Math.floor(num * 10000) / 10000;
  10. return parseFloat(num.toFixed(decimal));
  11. }
  12. // 求两个点的距离
  13. getDistance(p1, p2) {
  14. const x1 = p1.x;
  15. const y1 = p1.y;
  16. const x2 = p2.x;
  17. const y2 = p2.y;
  18. const num = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
  19. return this.getFixed(num);
  20. }
  21. createLine1(point1, point2) {
  22. if (point1.x == point2.x && point1.y == point2.y) {
  23. return null;
  24. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  25. return { x: point1.x };
  26. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  27. return { y: point1.y };
  28. }
  29. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  30. const parameterb =
  31. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  32. if (this.getFixed(parametera) == 0) {
  33. return { y: this.getFixed(parameterb) };
  34. }
  35. const parameter = {
  36. a: this.getFixed(parametera),
  37. b: this.getFixed(parameterb),
  38. };
  39. return parameter;
  40. }
  41. createLine2(point, angle) {
  42. if (angle == 90 || angle == 270) {
  43. return { x: point.x };
  44. }
  45. let a = Math.tan((angle / 180) * Math.PI);
  46. let b = point.y - a * point.x;
  47. if (a != 0) {
  48. return { a: a, b: b };
  49. } else {
  50. return { y: point.y };
  51. }
  52. }
  53. // 与lineA平行并且point在线上
  54. createLine3(lineA, point) {
  55. const parameter = {};
  56. if (typeof lineA.a === "undefined") {
  57. if (typeof lineA.x !== "undefined") {
  58. parameter.x = point.x;
  59. } else if (typeof lineA.y !== "undefined") {
  60. parameter.y = point.y;
  61. }
  62. } else {
  63. parameter.a = lineA.a;
  64. parameter.b = point.y - point.x * lineA.a;
  65. }
  66. return parameter;
  67. }
  68. create2AngleLine(point, angle, driftAngle) {
  69. let line1 = this.createLine2(point, angle - driftAngle / 2);
  70. let line2 = this.createLine2(point, angle + driftAngle / 2);
  71. return { line1: line1, line2: line2 };
  72. }
  73. distanceForPoints(point1, point2) {
  74. return Math.sqrt(
  75. Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2)
  76. );
  77. }
  78. //与line平行且两条线直接的距离是distance的两条线
  79. getParallelLineForDistance(line, distance) {
  80. let line1 = {};
  81. let line2 = {};
  82. if (!line.hasOwnProperty("a")) {
  83. if (line.hasOwnProperty("x")) {
  84. let x = line.x;
  85. line1.x = x + distance;
  86. line2.x = x - distance;
  87. } else if (line.hasOwnProperty("y")) {
  88. let y = line.y;
  89. line1.y = y + distance;
  90. line2.y = y - distance;
  91. }
  92. } else {
  93. line1.a = line.a;
  94. line1.b = line.b;
  95. line2.a = line.a;
  96. line2.b = line.b;
  97. let angle = Math.atan(line.a);
  98. let db = Math.abs(distance / Math.cos(angle));
  99. let b = line.b;
  100. line1.b = b + db;
  101. line2.b = b - db;
  102. }
  103. return { line1: line1, line2: line2 };
  104. }
  105. //获取扇形的两个端点
  106. getEndpoint(point, angle, sectorAngle) {
  107. const distance = 15;
  108. //line1是减,line2是加
  109. let lines1 = this.create2AngleLine(point, angle, sectorAngle);
  110. let line = this.createLine2(point, angle);
  111. line = this.getLineForPoint(line, point);
  112. let lines2 = this.getParallelLineForDistance(line, distance);
  113. let point1 = this.getIntersectionPoint(lines1.line1, lines2.line1);
  114. let point2 = this.getIntersectionPoint(lines1.line1, lines2.line2);
  115. let point3 = this.getIntersectionPoint(lines1.line2, lines2.line1);
  116. let point4 = this.getIntersectionPoint(lines1.line2, lines2.line2);
  117. let angle1 = this.Angle(point, point1, { x: point.x + 1, y: point.y });
  118. let angle2 = this.Angle(point, point2, { x: point.x + 1, y: point.y });
  119. let angle3 = this.Angle(point, point3, { x: point.x + 1, y: point.y });
  120. let angle4 = this.Angle(point, point4, { x: point.x + 1, y: point.y });
  121. if (angle > 180) {
  122. angle = 360 - angle;
  123. }
  124. if (
  125. Math.abs((angle1 + angle3) / 2 - angle) <
  126. Math.abs((angle2 + angle4) / 2 - angle)
  127. ) {
  128. return { p1: point1, p2: point3 };
  129. } else {
  130. return { p1: point2, p2: point4 };
  131. }
  132. }
  133. // true表示逆时针,false表示顺时针
  134. isClockwise(vertices) {
  135. let area = 0;
  136. for (let i = 0; i < vertices.length; i++) {
  137. const j = (i + 1) % vertices.length;
  138. area += vertices[i].x * vertices[j].y;
  139. area -= vertices[j].x * vertices[i].y;
  140. }
  141. const sub = area / 2;
  142. if (sub > 0) {
  143. // 逆时针
  144. return true;
  145. } else {
  146. // 顺时针
  147. return false;
  148. }
  149. }
  150. reverse(points) {
  151. const _points = [];
  152. for (let i = points.length - 1; i > -1; --i) {
  153. _points.push(points[i]);
  154. }
  155. return _points;
  156. }
  157. //两条线的交点
  158. getIntersectionPoint(parameter1, parameter2) {
  159. if (this.isParallel(parameter1, parameter2)) {
  160. return null;
  161. }
  162. if (
  163. typeof parameter1.a == "undefined" &&
  164. typeof parameter2.a != "undefined"
  165. ) {
  166. if (parameter1.x) {
  167. return {
  168. x: parameter1.x,
  169. y: parameter2.a * parameter1.x + parameter2.b,
  170. };
  171. } else if (parameter1.y) {
  172. return {
  173. x: (parameter1.y - parameter2.b) / parameter2.a,
  174. y: parameter1.y,
  175. };
  176. }
  177. } else if (
  178. typeof parameter2.a == "undefined" &&
  179. typeof parameter1.a != "undefined"
  180. ) {
  181. if (parameter2.x) {
  182. return {
  183. x: parameter2.x,
  184. y: parameter1.a * parameter2.x + parameter1.b,
  185. };
  186. } else if (parameter2.y) {
  187. return {
  188. x: (parameter2.y - parameter1.b) / parameter1.a,
  189. y: parameter2.y,
  190. };
  191. }
  192. } else if (
  193. typeof parameter2.a == "undefined" &&
  194. typeof parameter1.a == "undefined"
  195. ) {
  196. if (parameter1.hasOwnProperty("x") && parameter2.hasOwnProperty("y")) {
  197. return { x: parameter1.x, y: parameter2.y };
  198. } else if (
  199. parameter1.hasOwnProperty("y") &&
  200. parameter2.hasOwnProperty("x")
  201. ) {
  202. return { x: parameter2.x, y: parameter1.y };
  203. } else {
  204. return null;
  205. }
  206. }
  207. if (parameter1.a == parameter2.a) {
  208. return null;
  209. }
  210. let joinpointx =
  211. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  212. let joinpointy =
  213. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  214. (parameter1.a - parameter2.a);
  215. let point = { x: joinpointx, y: joinpointy };
  216. return point;
  217. }
  218. // 直线的交点
  219. getIntersectionPoint2(a, b, c, d) {
  220. /** 1 解线性方程组, 求线段交点. **/
  221. // 如果分母为0 则平行或共线, 不相交
  222. const denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x) * (c.y - d.y);
  223. if (denominator == 0) {
  224. return null;
  225. }
  226. // 线段所在直线的交点坐标 (x , y)
  227. const x =
  228. ((b.x - a.x) * (d.x - c.x) * (c.y - a.y) +
  229. (b.y - a.y) * (d.x - c.x) * a.x -
  230. (d.y - c.y) * (b.x - a.x) * c.x) /
  231. denominator;
  232. const y =
  233. -(
  234. (b.y - a.y) * (d.y - c.y) * (c.x - a.x) +
  235. (b.x - a.x) * (d.y - c.y) * a.y -
  236. (d.x - c.x) * (b.y - a.y) * c.y
  237. ) / denominator;
  238. return { x: x, y: y };
  239. }
  240. //两条线段交点
  241. getIntersectionPoint3(a, b, c, d) {
  242. const join = this.getIntersectionPoint2(a, b, c, d);
  243. if (join) {
  244. const x = join.x;
  245. const y = join.y; // 交点在线段1上 且交点也在线段2上
  246. /** 2 判断交点是否在两条线段上 **/
  247. if (
  248. (x - a.x) * (x - b.x) <= 0.001 &&
  249. (y - a.y) * (y - b.y) <= 0.001 &&
  250. (x - c.x) * (x - d.x) <= 0.001 &&
  251. (y - c.y) * (y - d.y) <= 0.001
  252. ) {
  253. // 返回交点p
  254. return {
  255. x: x,
  256. y: y,
  257. };
  258. }
  259. return null;
  260. }
  261. return null;
  262. }
  263. // 线段和直线是否相交
  264. getIntersectionPoint4(point1, point2, line) {
  265. const line1 = this.createLine1(point1, point2);
  266. const join = this.getIntersectionPoint(line1, line);
  267. if (join == null) {
  268. return null;
  269. }
  270. if (this.PointInSegment(join, point1, point2)) {
  271. return join; // 相交
  272. } else {
  273. return null;
  274. }
  275. }
  276. //返回true表示平行
  277. isParallel(line1, line2) {
  278. if (typeof line1.a == "undefined" && typeof line2.a == "undefined") {
  279. if (line1.hasOwnProperty("x") && line2.hasOwnProperty("x")) {
  280. return true;
  281. } else if (line1.hasOwnProperty("y") && line2.hasOwnProperty("y")) {
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. } else if (typeof line1.a == "undefined" || typeof line2.a == "undefined") {
  287. return false;
  288. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  289. return true;
  290. } else {
  291. return false;
  292. }
  293. }
  294. //两条相交的线段的夹角,永远小于180度
  295. Angle(o, s, e) {
  296. let cosfi = 0,
  297. fi = 0,
  298. norm = 0;
  299. let dsx = s.x - o.x;
  300. let dsy = s.y - o.y;
  301. let dex = e.x - o.x;
  302. let dey = e.y - o.y;
  303. cosfi = dsx * dex + dsy * dey;
  304. norm = (dsx * dsx + dsy * dsy) * (dex * dex + dey * dey);
  305. cosfi /= Math.sqrt(norm);
  306. if (cosfi >= 1.0) return 0;
  307. //if (cosfi <= -1.0) return Math.PI;
  308. if (cosfi <= -1.0) return 180;
  309. fi = Math.acos(cosfi);
  310. if ((180 * fi) / Math.PI < 180) {
  311. //return 180 * fi / Math.PI;
  312. return (fi * 180) / Math.PI;
  313. } else {
  314. //return 360 - 180 * fi / Math.PI;
  315. return ((2 * Math.PI - fi) * 180) / Math.PI;
  316. }
  317. }
  318. Angle1(o, s, e) {
  319. let cosfi = 0,
  320. fi = 0,
  321. norm = 0;
  322. let dsx = s.x - o.x;
  323. let dsy = s.y - o.y;
  324. let dex = e.x - o.x;
  325. let dey = e.y - o.y;
  326. cosfi = dsx * dex + dsy * dey;
  327. norm = (dsx * dsx + dsy * dsy) * (dex * dex + dey * dey);
  328. cosfi /= Math.sqrt(norm);
  329. if (cosfi >= 1.0) return 0;
  330. //if (cosfi <= -1.0) return Math.PI;
  331. if (cosfi <= -1.0) return 180;
  332. fi = Math.acos(cosfi);
  333. return (fi * 180) / Math.PI;
  334. }
  335. getArrow(start, end, ange = 30, L = 20) {
  336. let a = Math.atan2(end.y - start.y, end.x - start.x);
  337. let xC = end.x - L * Math.cos(a + (ange * Math.PI) / 180); // θ=30
  338. let yC = end.y - L * Math.sin(a + (ange * Math.PI) / 180);
  339. let xD = end.x - L * Math.cos(a - (ange * Math.PI) / 180);
  340. let yD = end.y - L * Math.sin(a - (ange * Math.PI) / 180);
  341. return [{ x: xC, y: yC }, end, { x: xD, y: yD }];
  342. }
  343. //经过point且与line垂直的线
  344. getLineForPoint(line, point) {
  345. let parameter = {};
  346. if (line.a == 0 || typeof line.a == "undefined") {
  347. if (line.hasOwnProperty("x")) {
  348. parameter.x = line.x;
  349. parameter.y = point.y;
  350. } else if (line.hasOwnProperty("y")) {
  351. parameter.x = point.x;
  352. parameter.y = line.y;
  353. }
  354. } else {
  355. parameter.a = -1 / line.a;
  356. parameter.b = point.y - point.x * parameter.a;
  357. }
  358. return parameter;
  359. }
  360. // 经过point且与line垂直的直线,该直线与line的交点
  361. getJoinLinePoint(point, line) {
  362. const verticalLine = this.getVerticalLine(line, point);
  363. const join = this.getIntersectionPoint(line, verticalLine);
  364. return join;
  365. }
  366. // 点到直线的距离
  367. getDisForPoinLine(point, line) {
  368. const join = this.getJoinLinePoint(point, line);
  369. return this.getDistance(point, join);
  370. }
  371. // 垂直线
  372. getVerticalLine(line, point) {
  373. if (typeof line.a === "undefined") {
  374. if (line.hasOwnProperty("x")) {
  375. return { y: point.y };
  376. } else if (line.hasOwnProperty("y")) {
  377. return { x: point.x };
  378. } else {
  379. return null;
  380. }
  381. } else if (line.a == 0) {
  382. return { x: point.x };
  383. } else {
  384. const tl = {};
  385. tl.a = -1 / line.a;
  386. const result = this.createLine3(tl, point);
  387. return result;
  388. }
  389. }
  390. //point在直线上,只是不确定是否在线段上
  391. //方法:point到startPoint和endPoint的距离之和与startPoint和endPoint之间的距离对比
  392. isContainForSegment(point, startPoint, endPoint, minDis) {
  393. if (!minDis) {
  394. minDis = Constant.minLen;
  395. }
  396. let dis1 =
  397. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  398. let dis2 = this.getDistance(startPoint, endPoint);
  399. if (Math.abs(dis1 - dis2) < minDis) {
  400. return true;
  401. } else {
  402. return false;
  403. }
  404. }
  405. /*
  406. //minDis
  407. isPointInPoly(point, points, minDis) {
  408. if (!minDis) {
  409. minDis = Constant.minRealDis
  410. }
  411. const x = point.x
  412. const y = point.y
  413. let inside = false
  414. // 是否在顶点附近
  415. for (let i = 0; i < points.length; ++i) {
  416. let distance = this.getDistance(point, points[i])
  417. if (distance < minDis) {
  418. return true
  419. }
  420. }
  421. // 是否在边沿
  422. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  423. let pt1 = points[i]
  424. let pt2 = points[j]
  425. const flag = this.isContainForSegment(point, pt1, pt2, minDis)
  426. if (flag) {
  427. return true
  428. }
  429. }
  430. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  431. let pt1 = points[i]
  432. let pt2 = points[j]
  433. const xi = pt1.x
  434. const yi = pt1.y
  435. const xj = pt2.x
  436. const yj = pt2.y
  437. const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
  438. if (intersect) inside = !inside
  439. }
  440. return inside
  441. }
  442. */
  443. isPointInPoly(point, points) {
  444. const x = point.x;
  445. const y = point.y;
  446. let inside = false;
  447. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  448. let pt1 = points[i];
  449. let pt2 = points[j];
  450. const xi = pt1.x;
  451. const yi = pt1.y;
  452. const xj = pt2.x;
  453. const yj = pt2.y;
  454. const intersect =
  455. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  456. if (intersect) inside = !inside;
  457. }
  458. return inside;
  459. }
  460. //a表示横轴,b表示竖轴
  461. isPointInElliptic(point, center, a, b) {
  462. let r =
  463. Math.pow((point.x - center.x) / a, 2) +
  464. Math.pow((point.y - center.y) / b, 2);
  465. if (r <= 1) {
  466. return true;
  467. } else {
  468. return false;
  469. }
  470. }
  471. // 点到线段的距离
  472. // 在minDistance范围内,会吸附到point1/point2上
  473. // 返回值:type是1表示吸附在point1,是2表示吸附在point2,是0表示在线段point1-point2上;
  474. getDisForPoinSegment(point, point1, point2, minDistance) {
  475. const line = this.createLine1(point1, point2);
  476. const join = this.getJoinLinePoint(point, line);
  477. const dis = this.getDistance(point1, point2);
  478. const dis1 = this.getDistance(join, point1);
  479. const dis2 = this.getDistance(join, point2);
  480. if (
  481. this.getDistance(join, point1) > dis ||
  482. this.getDistance(join, point2) > dis
  483. ) {
  484. // 在线段外
  485. if (dis1 < dis2 && dis1 < minDistance) {
  486. return { type: 1, join: point1 };
  487. } else if (dis2 < dis1 && dis2 < minDistance) {
  488. return { type: 2, join: point2 };
  489. } else {
  490. return null;
  491. }
  492. } else {
  493. if (dis1 < minDistance) {
  494. return { type: 1, join: point1 };
  495. } else if (dis2 < minDistance) {
  496. return { type: 2, join: point2 };
  497. } else if (this.getDistance(point, join) < minDistance) {
  498. return { type: 0, join: join };
  499. }
  500. }
  501. }
  502. PointInSegment(Q, pi, pj, minDis) {
  503. if (
  504. this.getDistance(Q, pi) < Constant.minAdsorbPix ||
  505. this.getDistance(Q, pj) < Constant.minAdsorbPix
  506. ) {
  507. return true;
  508. }
  509. if (!minDis) {
  510. minDis = 0.1;
  511. }
  512. minDis = minDis / 2;
  513. const offset1 = (Q.x - pi.x) * (pj.y - pi.y) - (pj.x - pi.x) * (Q.y - pi.y);
  514. const offset2 = Math.min(pi.x, pj.x) - Q.x;
  515. const offset3 = Q.x - Math.max(pi.x, pj.x);
  516. const offset4 = Math.min(pi.y, pj.y) - Q.y;
  517. const offset5 = Q.y - Math.max(pi.y, pj.y);
  518. if (
  519. Math.abs(offset1) < minDis &&
  520. (offset2 <= 0 || Math.abs(offset2) < minDis) &&
  521. (offset3 <= 0 || Math.abs(offset3) < minDis) &&
  522. (offset4 <= 0 || Math.abs(offset4) < minDis) &&
  523. (offset5 <= 0 || Math.abs(offset5) < minDis)
  524. ) {
  525. return true;
  526. } else {
  527. return false;
  528. }
  529. }
  530. //点p是否在线段AB上
  531. isPointOnSegment(p, A, B) {
  532. // 计算向量 AP 和 BP
  533. const AP = {
  534. x: p.x - A.x,
  535. y: p.y - A.y,
  536. };
  537. const BP = {
  538. x: p.x - B.x,
  539. y: p.y - B.y,
  540. };
  541. // 计算向量 AB 的长度和方向
  542. const AB = {
  543. x: B.x - A.x,
  544. y: B.y - A.y,
  545. };
  546. const AB_length = this.getDistance(A, B);
  547. const AB_direction = {
  548. x: AB.x / AB_length,
  549. y: AB.y / AB_length,
  550. };
  551. // 检查 AP 和 BP 的方向是否与 AB 相同,并检查它们的长度是否小于等于 AB 的长度
  552. const dot_product_AP = AP.x * AB_direction.x + AP.y * AB_direction.y;
  553. const dot_product_BP = BP.x * AB_direction.x + BP.y * AB_direction.y;
  554. //return dot_product_AP >= 0 && dot_product_BP <= 0 && Math.abs(AP.x * BP.y - AP.y * BP.x) <= AB_length * Number.EPSILON;
  555. return (
  556. dot_product_AP >= 0 &&
  557. dot_product_BP <= 0 &&
  558. Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.01
  559. );
  560. }
  561. clonePoint(p1, p2) {
  562. p1.x = p2.x;
  563. p1.y = p2.y;
  564. }
  565. clonePoints(points1, points2) {
  566. for (let i = 0; i < points1.length; ++i) {
  567. this.clonePoint(points1[i], points2[i]);
  568. }
  569. }
  570. equalPoint(p1, p2) {
  571. if (p1.x == p2.x && p1.y == p2.y) {
  572. return true;
  573. } else {
  574. return false;
  575. }
  576. }
  577. equalPoints(points1, points2) {
  578. if (points1.length != points2.length) {
  579. return false;
  580. }
  581. for (let i = 0; i < points1.length; ++i) {
  582. let flag = this.equalPoint(points1[i], points2[i]);
  583. if (!flag) {
  584. return false;
  585. }
  586. }
  587. return true;
  588. }
  589. equalJSON(json1, json2) {
  590. for (let key in json1) {
  591. if (json2.hasOwnProperty(key) && json1[key] == json2[key]) {
  592. continue;
  593. } else {
  594. return false;
  595. }
  596. }
  597. for (let key in json2) {
  598. if (json1.hasOwnProperty(key) && json1[key] == json2[key]) {
  599. continue;
  600. } else {
  601. return false;
  602. }
  603. }
  604. return true;
  605. }
  606. crossTwoLines(point1, point2, point3, point4, dis) {
  607. if (typeof dis == "undefined") {
  608. dis = Constant.minAdsorbPix;
  609. }
  610. const join = this.getIntersectionPoint2(point1, point2, point3, point4);
  611. if (join != null) {
  612. if (
  613. this.getDistance(point1, join) > dis &&
  614. this.getDistance(point2, join) > dis &&
  615. this.getDistance(point3, join) > dis &&
  616. this.getDistance(point4, join) > dis
  617. ) {
  618. if (
  619. this.getDistance(point1, join) < this.getDistance(point1, point2) &&
  620. this.getDistance(point2, join) < this.getDistance(point1, point2) &&
  621. this.getDistance(point3, join) < this.getDistance(point3, point4) &&
  622. this.getDistance(point4, join) < this.getDistance(point3, point4)
  623. ) {
  624. return true;
  625. } else {
  626. return false;
  627. }
  628. }
  629. } else {
  630. if (
  631. this.PointInSegment(point1, point3, point4) ||
  632. this.PointInSegment(point2, point3, point4)
  633. ) {
  634. return true;
  635. }
  636. }
  637. return false;
  638. }
  639. getDisPointsLine(line, point, distance1, distance2) {
  640. const newpoint1 = {};
  641. const newpoint2 = {};
  642. const result = {};
  643. if (line.hasOwnProperty("x")) {
  644. newpoint1.x = line.x;
  645. newpoint1.y = point.y - distance1;
  646. newpoint2.x = line.x;
  647. newpoint2.y = point.y + distance2;
  648. } else if (line.hasOwnProperty("y")) {
  649. newpoint1.y = line.y;
  650. newpoint1.x = point.x - distance1;
  651. newpoint2.y = line.y;
  652. newpoint2.x = point.x + distance2;
  653. } else {
  654. const a = Math.atan(line.a);
  655. const t_line = { a: -1 / line.a };
  656. const line_ab2 = this.createLine3(t_line, point);
  657. const join = this.getIntersectionPoint(line, line_ab2);
  658. newpoint1.x = join.x - distance1 * Math.cos(a);
  659. newpoint1.y = join.y - distance1 * Math.sin(a);
  660. newpoint2.x = join.x + distance2 * Math.cos(a);
  661. newpoint2.y = join.y + distance2 * Math.sin(a);
  662. }
  663. result.newpoint1 = newpoint1;
  664. result.newpoint2 = newpoint2;
  665. return result;
  666. }
  667. getBoundingBox(points) {
  668. let minX = points[0].x;
  669. let maxX = points[0].x;
  670. let minY = points[0].y;
  671. let maxY = points[0].y;
  672. for (let i = 1; i < points.length; ++i) {
  673. const point = points[i];
  674. if (minX > point.x) {
  675. minX = point.x;
  676. }
  677. if (minY > point.y) {
  678. minY = point.y;
  679. }
  680. if (maxX < point.x) {
  681. maxX = point.x;
  682. }
  683. if (maxY < point.y) {
  684. maxY = point.y;
  685. }
  686. }
  687. const box = {};
  688. box.minX = minX;
  689. box.minY = minY;
  690. box.maxX = maxX;
  691. box.maxY = maxY;
  692. return box;
  693. }
  694. getBoundingBox2(points) {
  695. let minX = null;
  696. let maxX = null;
  697. let minY = null;
  698. let maxY = null;
  699. for (let key in points) {
  700. const point = points[key];
  701. if (minX == null || minX > point.x) {
  702. minX = point.x;
  703. }
  704. if (minY == null || minY > point.y) {
  705. minY = point.y;
  706. }
  707. if (maxX == null || maxX < point.x) {
  708. maxX = point.x;
  709. }
  710. if (maxY == null || maxY < point.y) {
  711. maxY = point.y;
  712. }
  713. }
  714. const box = {};
  715. box.minX = minX;
  716. box.minY = minY;
  717. box.maxX = maxX;
  718. box.maxY = maxY;
  719. return box;
  720. }
  721. ComputePolygonArea(points) {
  722. const point_num = points.length;
  723. if (point_num < 3) {
  724. return 0;
  725. }
  726. let s = points[0].y * (points[point_num - 1].x - points[1].x);
  727. for (let i = 1; i < point_num; ++i)
  728. s += points[i].y * (points[i - 1].x - points[(i + 1) % point_num].x);
  729. return Math.abs(s / 2.0);
  730. }
  731. // 获取多边形重心
  732. getPolygonCore(points) {
  733. function Area(p0, p1, p2) {
  734. let area = 0.0;
  735. area =
  736. p0.x * p1.y +
  737. p1.x * p2.y +
  738. p2.x * p0.y -
  739. p1.x * p0.y -
  740. p2.x * p1.y -
  741. p0.x * p2.y;
  742. return area / 2;
  743. }
  744. let sum_x = 0;
  745. let sum_y = 0;
  746. let sum_area = 0;
  747. let p1 = points[1];
  748. for (let i = 2; i < points.length; i++) {
  749. const p2 = points[i];
  750. const area = Area(points[0], p1, p2);
  751. sum_area += area;
  752. sum_x += (points[0].x + p1.x + p2.x) * area;
  753. sum_y += (points[0].y + p1.y + p2.y) * area;
  754. p1 = p2;
  755. }
  756. const xx = sum_x / sum_area / 3;
  757. const yy = sum_y / sum_area / 3;
  758. return {
  759. x: xx,
  760. y: yy,
  761. };
  762. }
  763. // points1是否在points2里
  764. isPolyInPoly(points1, points2, minDis) {
  765. for (let i = 0; i < points1.length; ++i) {
  766. let flag = false;
  767. for (let j = 0; j < points2.length; ++j) {
  768. if (this.equalPoint(points1[i], points2[j])) {
  769. flag = true;
  770. break;
  771. }
  772. }
  773. if (!flag) {
  774. if (!this.isPointInPoly(points1[i], points2, minDis)) {
  775. return false;
  776. }
  777. } else {
  778. const nextIndex = i == points1.length - 1 ? 0 : i + 1;
  779. const mid = {
  780. x: (points1[i].x + points1[nextIndex].x) / 2,
  781. y: (points1[i].y + points1[nextIndex].y) / 2,
  782. };
  783. if (!this.isPointInPoly(mid, points2, minDis)) {
  784. return false;
  785. }
  786. }
  787. }
  788. return true;
  789. }
  790. dotPoints(pt1, pt2, point1, point2) {
  791. let vt1 = {};
  792. let vt2 = {};
  793. vt1.start = {};
  794. vt1.end = {};
  795. vt1.start.x = 0;
  796. vt1.start.y = 0;
  797. vt1.end.x = pt2.x - pt1.x;
  798. vt1.end.y = pt2.y - pt1.y;
  799. vt2.start = {};
  800. vt2.end = {};
  801. vt2.start.x = 0;
  802. vt2.start.y = 0;
  803. vt2.end.x = point2.x - point1.x;
  804. vt2.end.y = point2.y - point1.y;
  805. let result = vt1.end.x * vt2.end.x + vt1.end.y * vt2.end.y;
  806. return result;
  807. }
  808. //start是起点,target是朝着目标移动,distance是移动的距离
  809. translate(start, target, point, distance) {
  810. let dx = target.x - start.x;
  811. let dy = target.y - start.y;
  812. let dis = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  813. let result = {
  814. x: point.x + (dx * distance) / dis,
  815. y: point.y + (dy * distance) / dis,
  816. };
  817. return result;
  818. }
  819. //射线与线段相交
  820. // intersection(rayStart, rayEnd, segmentStart, segmentEnd) {
  821. // // 计算射线和线段的方向向量
  822. // const rayDirection = {
  823. // x:rayEnd.x - rayStart.x,
  824. // y:rayEnd.y - rayStart.y,
  825. // };
  826. // const segmentDirection = {
  827. // x:segmentEnd.x - segmentStart.x,
  828. // y:segmentEnd.y - segmentStart.y,
  829. // };
  830. // // 计算射线和线段的起点之间的向量
  831. // const startPointVector = {
  832. // x:rayStart.x - segmentStart.x,
  833. // y:rayStart.y - segmentStart.y,
  834. // };
  835. // // 计算射线和线段的叉积
  836. // const crossProduct = rayDirection.x * segmentDirection.y - rayDirection.y * segmentDirection.x;
  837. // // 如果叉积为0,则表示射线和线段平行
  838. // if (crossProduct === 0) {
  839. // return null;
  840. // }
  841. // // 计算线段起点到射线的交点的向量
  842. // const t = (startPointVector.x * segmentDirection.y - startPointVector.y * segmentDirection.x) / crossProduct;
  843. // // 如果t的值小于0,则交点在射线的起点之后
  844. // if (t < 0) {
  845. // return null;
  846. // }
  847. // // 计算交点的坐标
  848. // const intersectionX = rayStart.x + t * rayDirection.x;
  849. // const intersectionY = rayStart.y + t * rayDirection.y;
  850. // // 如果交点在线段的范围内,则返回交点坐标
  851. // if ((intersectionX >= Math.min(segmentStart.x, segmentEnd.x)) &&
  852. // (intersectionX <= Math.max(segmentStart.x, segmentEnd.x)) &&
  853. // (intersectionY >= Math.min(segmentStart.y, segmentEnd.y)) &&
  854. // (intersectionY <= Math.max(segmentStart.y, segmentEnd.y))) {
  855. // //return [intersectionX, intersectionY];
  856. // return {
  857. // x:intersectionX,
  858. // y:intersectionY,
  859. // };
  860. // }
  861. // // 否则返回null
  862. // return null;
  863. // }
  864. // raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  865. // // 计算射线和线段的交点
  866. // const x1 = rayOrigin.x
  867. // const y1 = rayOrigin.y
  868. // const x2 = segmentStart.x
  869. // const y2 = segmentStart.y
  870. // const dx1 = rayDirection.x
  871. // const dy1 = rayDirection.y
  872. // const dx2 = segmentEnd.x - x2
  873. // const dy2 = segmentEnd.y - y2
  874. // const crossProduct = dx1 * dy2 - dx2 * dy1
  875. // if (Math.abs(crossProduct) < 1e-8) {
  876. // // 射线和线段平行或共线
  877. // return null
  878. // }
  879. // const t1 = (dx2 * (y1 - y2) - dy2 * (x1 - x2)) / crossProduct
  880. // const t2 = (dx1 * (y1 - y2) - dy1 * (x1 - x2)) / crossProduct
  881. // if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
  882. // // 有交点,计算交点坐标
  883. // const intersectionX = x1 + t1 * dx1
  884. // const intersectionY = y1 + t1 * dy1
  885. // return {
  886. // x: intersectionX,
  887. // y: intersectionY,
  888. // }
  889. // } else {
  890. // // 没有交点
  891. // return null
  892. // }
  893. // }
  894. raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  895. const end = {
  896. x: rayOrigin.x + rayDirection.x,
  897. y: rayOrigin.y - rayDirection.z,
  898. };
  899. const line = this.createLine1(rayOrigin, end);
  900. const join = this.getIntersectionPoint4(segmentStart, segmentEnd, line);
  901. if (join == null) {
  902. return null;
  903. } else {
  904. const dis = this.getDistance(end, join);
  905. const dis1 = this.getDistance(rayOrigin, join);
  906. const dis2 = this.getDistance(rayOrigin, end);
  907. if (dis - (dis1 + dis2) + 0.01 > 0) {
  908. return null;
  909. } else {
  910. return join;
  911. }
  912. }
  913. }
  914. RectangleVertex(startPoint, endPoint, width) {
  915. let line = this.createLine1(startPoint, endPoint);
  916. let lines = this.getParallelLineForDistance(line, width / 2);
  917. let leftEdgeStart, rightEdgeStart, rightEdgeEnd, leftEdgeEnd;
  918. let point = null;
  919. let points = [];
  920. //先计算start部分
  921. point = startPoint;
  922. points.push(endPoint);
  923. points.push(startPoint);
  924. let point1 = this.getJoinLinePoint(point, lines.line1);
  925. let point2 = this.getJoinLinePoint(point, lines.line2);
  926. points[2] = point1;
  927. if (this.isClockwise(points)) {
  928. rightEdgeStart = point1;
  929. leftEdgeStart = point2;
  930. } else {
  931. rightEdgeStart = point2;
  932. leftEdgeStart = point1;
  933. }
  934. //再计算end部分
  935. points = [];
  936. point = endPoint;
  937. points.push(startPoint);
  938. points.push(endPoint);
  939. point1 = this.getJoinLinePoint(point, lines.line1);
  940. point2 = this.getJoinLinePoint(point, lines.line2);
  941. points[2] = point1;
  942. if (this.isClockwise(points)) {
  943. rightEdgeEnd = point2;
  944. leftEdgeEnd = point1;
  945. } else {
  946. rightEdgeEnd = point1;
  947. leftEdgeEnd = point2;
  948. }
  949. return {
  950. leftEdgeStart: leftEdgeStart,
  951. rightEdgeStart: rightEdgeStart,
  952. rightEdgeEnd: rightEdgeEnd,
  953. leftEdgeEnd: leftEdgeEnd,
  954. };
  955. }
  956. //start到end的射线中取一点point,start-end和end-point的距离相同
  957. getPositionForExtendedLine(start, end) {
  958. const dx = end.x - start.x;
  959. const dy = end.y - start.y;
  960. const point = {
  961. x: end.x + dx,
  962. y: end.y + dy,
  963. };
  964. return point;
  965. }
  966. isOnRay(start, dir, position) {
  967. const v1 = { x: dir.x - start.x, y: dir.y - start.y };
  968. const v2 = { x: position.x - start.x, y: position.y - start.y };
  969. return v1.x * v2.y - v1.y * v2.x;
  970. }
  971. //向量是否同样的方向
  972. isSameDirForVector(point1, point2, p1, p2) {
  973. const v1 = {
  974. x: point2.x - point1.x,
  975. y: point2.y - point1.y,
  976. };
  977. const v2 = {
  978. x: p2.x - p1.x,
  979. y: p2.y - p1.y,
  980. };
  981. const value = this.dot(v1, v2);
  982. if (value > 0) {
  983. return true;
  984. }
  985. {
  986. return false;
  987. }
  988. }
  989. //生成五角星
  990. createFivePointedStar(position, r) {
  991. let deg = Math.PI / 180; //角度
  992. let points = [];
  993. points[0] = {
  994. x: position.x - r * Math.cos(54 * deg),
  995. y: position.y + r * Math.sin(54 * deg),
  996. };
  997. points[1] = {
  998. x: position.x,
  999. y: position.y - r,
  1000. };
  1001. points[2] = {
  1002. x: position.x + r * Math.cos(54 * deg),
  1003. y: position.y + r * Math.sin(54 * deg),
  1004. };
  1005. points[3] = {
  1006. x: position.x - r * Math.cos(18 * deg),
  1007. y: position.y - r * Math.sin(18 * deg),
  1008. };
  1009. points[4] = {
  1010. x: position.x + r * Math.cos(18 * deg),
  1011. y: position.y - r * Math.sin(18 * deg),
  1012. };
  1013. return points;
  1014. }
  1015. createSixPoint(position, r) {
  1016. let deg = Math.PI / 180; //角度
  1017. let points = [];
  1018. points[0] = {
  1019. x: position.x,
  1020. y: position.y + r,
  1021. };
  1022. points[1] = {
  1023. x: position.x + r * Math.sin(60 * deg),
  1024. y: position.y + r * Math.cos(60 * deg),
  1025. };
  1026. points[2] = {
  1027. x: position.x + r * Math.cos(30 * deg),
  1028. y: position.y - r * Math.sin(30 * deg),
  1029. };
  1030. points[3] = {
  1031. x: position.x,
  1032. y: position.y - r,
  1033. };
  1034. points[4] = {
  1035. x: position.x - r * Math.cos(30 * deg),
  1036. y: position.y - r * Math.sin(30 * deg),
  1037. };
  1038. points[5] = {
  1039. x: position.x - r * Math.sin(60 * deg),
  1040. y: position.y + r * Math.cos(60 * deg),
  1041. };
  1042. return points;
  1043. }
  1044. //求圆和直线之间的交点
  1045. /**
  1046. * 求圆和直线之间的交点
  1047. * 直线方程:y = kx + b
  1048. * 圆的方程:(x - m)² + (x - n)² = r²
  1049. * x1, y1 = 线坐标1, x2, y2 = 线坐标2, m, n = 圆坐标, r = 半径
  1050. */
  1051. getInsertPointBetweenCircleAndLine(x1, y1, x2, y2, m, n, radius) {
  1052. let insertPoints = [];
  1053. if (Math.abs(x1 - x2) < 0.5) {
  1054. insertPoints[0] = {
  1055. x: x1,
  1056. y: n - Math.sqrt(radius * radius - Math.pow(x1 - m, 2)),
  1057. };
  1058. insertPoints[1] = {
  1059. x: x1,
  1060. y: n + Math.sqrt(radius * radius - Math.pow(x1 - m, 2)),
  1061. };
  1062. return insertPoints;
  1063. }
  1064. // console.log(x1, y1, x2, y2, m, n, radius)
  1065. let kbArr = this.binaryEquationGetKB(x1, y1, x2, y2);
  1066. let k = kbArr[0];
  1067. let b = kbArr[1];
  1068. let aX = 1 + k * k;
  1069. let bX = 2 * k * (b - n) - 2 * m;
  1070. let cX = m * m + (b - n) * (b - n) - radius * radius;
  1071. let xArr = this.quadEquationGetX(aX, bX, cX);
  1072. xArr.forEach((x) => {
  1073. let y = k * x + b;
  1074. insertPoints.push({ x: x, y: y });
  1075. });
  1076. return insertPoints;
  1077. }
  1078. /**
  1079. * 求二元一次方程的系数
  1080. * y1 = k * x1 + b => k = (y1 - b) / x1
  1081. * y2 = k * x2 + b => y2 = ((y1 - b) / x1) * x2 + b
  1082. */
  1083. binaryEquationGetKB(x1, y1, x2, y2) {
  1084. let k = (y1 - y2) / (x1 - x2);
  1085. let b = (x1 * y2 - x2 * y1) / (x1 - x2);
  1086. return [k, b];
  1087. }
  1088. /**
  1089. * 一元二次方程求根
  1090. * ax² + bx + c = 0
  1091. */
  1092. quadEquationGetX(a, b, c) {
  1093. let xArr = [];
  1094. let result = Math.pow(b, 2) - 4 * a * c;
  1095. if (result > 0) {
  1096. xArr.push((-b + Math.sqrt(result)) / (2 * a));
  1097. xArr.push((-b - Math.sqrt(result)) / (2 * a));
  1098. }
  1099. //else if (result == 0) {
  1100. else {
  1101. xArr.push(-b / (2 * a));
  1102. }
  1103. return xArr;
  1104. }
  1105. angleTo(v1, v2) {
  1106. const denominator = Math.sqrt(this.lengthSq(v1) * this.lengthSq(v2));
  1107. if (denominator === 0) return 90;
  1108. const theta = this.dot(v1, v2) / denominator;
  1109. //return Math.acos(this.clamp(theta, -1, 1));
  1110. return (Math.acos(this.clamp(theta, -1, 1)) / Math.PI) * 180;
  1111. }
  1112. //点乘
  1113. dot(v1, v2) {
  1114. return v1.x * v2.x + v1.y * v2.y;
  1115. }
  1116. //叉乘
  1117. cross(v1, v2) {
  1118. return v1.x * v2.y - v1.y * v2.x;
  1119. }
  1120. // 两点相减
  1121. pointMinus(v1, v2) {
  1122. return {
  1123. x: v1.x - v2.x,
  1124. y: v1.y - v2.y,
  1125. };
  1126. }
  1127. // 两点相加
  1128. pointPlus(v1, v2) {
  1129. return {
  1130. x: v1.x + v2.x,
  1131. y: v1.y + v2.y,
  1132. };
  1133. }
  1134. // 中心点
  1135. lineCenter(v1, v2) {
  1136. const point = this.pointPlus(v1, v2);
  1137. return {
  1138. x: point.x / 2,
  1139. y: point.y / 2,
  1140. };
  1141. }
  1142. // 点放大
  1143. pointScale(v, a) {
  1144. return {
  1145. x: v.x * a,
  1146. y: v.y * a,
  1147. };
  1148. }
  1149. clamp(value, min, max) {
  1150. return Math.max(min, Math.min(max, value));
  1151. }
  1152. lengthSq(v) {
  1153. return v.x * v.x + v.y * v.y;
  1154. }
  1155. // 当前点 下一个点 下下个点
  1156. getCurvesControls(p1, pt, p2, scale = 0.3) {
  1157. const vec1T = this.pointMinus(p1, pt);
  1158. const vecT2 = this.pointMinus(p1, pt);
  1159. const len1 = Math.hypot(vec1T.x, vec1T.y);
  1160. const len2 = Math.hypot(vecT2.x, vecT2.y);
  1161. const v = len1 / len2;
  1162. let delta;
  1163. if (v > 1) {
  1164. delta = this.pointMinus(
  1165. p1,
  1166. this.pointPlus(pt, this.pointScale(this.pointMinus(p2, pt), 1 / v))
  1167. );
  1168. } else {
  1169. delta = this.pointMinus(
  1170. this.pointPlus(pt, this.pointScale(this.pointMinus(p1, pt), v)),
  1171. p2
  1172. );
  1173. }
  1174. delta = this.pointScale(delta, scale);
  1175. const control1 = {
  1176. x: this.pointPlus(pt, delta).x,
  1177. y: this.pointPlus(pt, delta).y,
  1178. };
  1179. const control2 = {
  1180. x: this.pointMinus(pt, delta).x,
  1181. y: this.pointMinus(pt, delta).y,
  1182. };
  1183. return { control1, control2 };
  1184. }
  1185. getCurvesByPoints(points, scale = 0.2) {
  1186. const curves = [];
  1187. let preControl1, preControl2;
  1188. for (let i = 0; i < points.length - 2; i++) {
  1189. const { control1, control2 } = this.getCurvesControls(
  1190. points[i],
  1191. points[i + 1],
  1192. points[i + 2],
  1193. scale
  1194. );
  1195. curves.push({
  1196. start: points[i],
  1197. end: points[i + 1],
  1198. controls: i === 0 ? [control1] : [preControl2, control1],
  1199. });
  1200. preControl1 = control1;
  1201. preControl2 = control2;
  1202. }
  1203. curves.push({
  1204. start: points[points.length - 2],
  1205. controls: [preControl2],
  1206. end: points[points.length - 1],
  1207. });
  1208. return curves;
  1209. }
  1210. /**
  1211. * 已知四个控制点,及曲线中的某一个点的 x/y,反推求 t
  1212. * @param {number} x1 起点 x/y
  1213. * @param {number} x2 控制点1 x/y
  1214. * @param {number} x3 控制点2 x/y
  1215. * @param {number} x4 终点 x/y
  1216. * @param {number} X 曲线中的某个点 x/y
  1217. * @returns {number[]} t[]
  1218. */
  1219. getThreeBezierT(x1, x2, x3, x4, X) {
  1220. const a = -x1 + 3 * x2 - 3 * x3 + x4;
  1221. const b = 3 * x1 - 6 * x2 + 3 * x3;
  1222. const c = -3 * x1 + 3 * x2;
  1223. const d = x1 - X;
  1224. // 盛金公式, 预先需满足, a !== 0
  1225. // 判别式
  1226. const A = Math.pow(b, 2) - 3 * a * c;
  1227. const B = b * c - 9 * a * d;
  1228. const C = Math.pow(c, 2) - 3 * b * d;
  1229. const delta = Math.pow(B, 2) - 4 * A * C;
  1230. let t1 = -100,
  1231. t2 = -100,
  1232. t3 = -100;
  1233. // 3个相同实数根
  1234. if (A === B && A === 0) {
  1235. t1 = -b / (3 * a);
  1236. t2 = -c / b;
  1237. t3 = (-3 * d) / c;
  1238. return [t1, t2, t3];
  1239. }
  1240. // 1个实数根和1对共轭复数根
  1241. if (delta > 0) {
  1242. const v = Math.pow(B, 2) - 4 * A * C;
  1243. const xsv = v < 0 ? -1 : 1;
  1244. const m1 = A * b + (3 * a * (-B + (v * xsv) ** (1 / 2) * xsv)) / 2;
  1245. const m2 = A * b + (3 * a * (-B - (v * xsv) ** (1 / 2) * xsv)) / 2;
  1246. const xs1 = m1 < 0 ? -1 : 1;
  1247. const xs2 = m2 < 0 ? -1 : 1;
  1248. t1 =
  1249. (-b - (m1 * xs1) ** (1 / 3) * xs1 - (m2 * xs2) ** (1 / 3) * xs2) /
  1250. (3 * a);
  1251. // 涉及虚数,可不考虑。i ** 2 = -1
  1252. }
  1253. // 3个实数根
  1254. if (delta === 0) {
  1255. const K = B / A;
  1256. t1 = -b / a + K;
  1257. t2 = t3 = -K / 2;
  1258. }
  1259. // 3个不相等实数根
  1260. if (delta < 0) {
  1261. const xsA = A < 0 ? -1 : 1;
  1262. const T = (2 * A * b - 3 * a * B) / (2 * (A * xsA) ** (3 / 2) * xsA);
  1263. const theta = Math.acos(T);
  1264. if (A > 0 && T < 1 && T > -1) {
  1265. t1 = (-b - 2 * A ** (1 / 2) * Math.cos(theta / 3)) / (3 * a);
  1266. t2 =
  1267. (-b +
  1268. A ** (1 / 2) *
  1269. (Math.cos(theta / 3) + 3 ** (1 / 2) * Math.sin(theta / 3))) /
  1270. (3 * a);
  1271. t3 =
  1272. (-b +
  1273. A ** (1 / 2) *
  1274. (Math.cos(theta / 3) - 3 ** (1 / 2) * Math.sin(theta / 3))) /
  1275. (3 * a);
  1276. }
  1277. }
  1278. return [t1, t2, t3];
  1279. }
  1280. /**
  1281. * @desc 获取三阶贝塞尔曲线的线上坐标
  1282. * B(t) = P0 * (1-t)^3 + 3 * P1 * t * (1-t)^2 + 3 * P2 * t^2 * (1-t) + P3 * t^3, t ∈ [0,1]
  1283. * @param {number} t 当前百分比
  1284. * @param {Array} p1 起点坐标
  1285. * @param {Array} p2 终点坐标
  1286. * @param {Array} cp1 控制点1
  1287. * @param {Array} cp2 控制点2
  1288. */
  1289. getThreeBezierPoint(t, p1, cp1, cp2, p2) {
  1290. const { x: x1, y: y1 } = p1;
  1291. const { x: x2, y: y2 } = p2;
  1292. const { x: cx1, y: cy1 } = cp1;
  1293. const { x: cx2, y: cy2 } = cp2;
  1294. const x =
  1295. x1 * (1 - t) * (1 - t) * (1 - t) +
  1296. 3 * cx1 * t * (1 - t) * (1 - t) +
  1297. 3 * cx2 * t * t * (1 - t) +
  1298. x2 * t * t * t;
  1299. const y =
  1300. y1 * (1 - t) * (1 - t) * (1 - t) +
  1301. 3 * cy1 * t * (1 - t) * (1 - t) +
  1302. 3 * cy2 * t * t * (1 - t) +
  1303. y2 * t * t * t;
  1304. return { x, y };
  1305. }
  1306. getHitInfoForThreeBezier(position, curve, rang = 3) {
  1307. // 定义三次贝塞尔曲线的控制点和目标点
  1308. var p0 = curve.start;
  1309. var p1 = curve.controls[0];
  1310. var p2 = curve.controls[1];
  1311. var p3 = curve.end;
  1312. var target = position;
  1313. // 参数化方式在曲线上取一系列的点
  1314. var pointsOnCurve = [];
  1315. for (var t = 0; t <= 1; t += 0.01) {
  1316. var x =
  1317. Math.pow(1 - t, 3) * p0.x +
  1318. 3 * Math.pow(1 - t, 2) * t * p1.x +
  1319. 3 * (1 - t) * Math.pow(t, 2) * p2.x +
  1320. Math.pow(t, 3) * p3.x;
  1321. var y =
  1322. Math.pow(1 - t, 3) * p0.y +
  1323. 3 * Math.pow(1 - t, 2) * t * p1.y +
  1324. 3 * (1 - t) * Math.pow(t, 2) * p2.y +
  1325. Math.pow(t, 3) * p3.y;
  1326. pointsOnCurve.push({ x: x, y: y });
  1327. }
  1328. // 计算每个点与目标点的距离
  1329. var shortestDistance = Number.MAX_VALUE;
  1330. var closestPoint;
  1331. for (var i = 0; i < pointsOnCurve.length; i++) {
  1332. var distance = Math.sqrt(
  1333. Math.pow(pointsOnCurve[i].x - target.x, 2) +
  1334. Math.pow(pointsOnCurve[i].y - target.y, 2)
  1335. );
  1336. if (distance < shortestDistance) {
  1337. shortestDistance = distance;
  1338. closestPoint = pointsOnCurve[i];
  1339. }
  1340. }
  1341. return {
  1342. position: closestPoint,
  1343. distance: shortestDistance,
  1344. };
  1345. console.log("最短距离:", shortestDistance);
  1346. console.log("最近点:", closestPoint);
  1347. const { x: offsetX, y: offsetY } = position;
  1348. let results = [];
  1349. // 用 x 求出对应的 t,用 t 求相应位置的 y,再比较得出的 y 与 offsetY 之间的差值
  1350. const tsx = this.getThreeBezierT(
  1351. curve.start.x,
  1352. curve.controls[0].x,
  1353. curve.controls[1].x,
  1354. curve.end.x,
  1355. offsetX
  1356. );
  1357. console.log(tsx);
  1358. for (let x = 0; x < 3; x++) {
  1359. if (tsx[x] <= 1 && tsx[x] >= 0) {
  1360. const point = this.getThreeBezierPoint(
  1361. tsx[x],
  1362. curve.start,
  1363. curve.controls[0],
  1364. curve.controls[1],
  1365. curve.end
  1366. );
  1367. // if (Math.abs(point.y - offsetY) < rang) {
  1368. results.push({
  1369. position: point,
  1370. distance: this.getDistance(point, position),
  1371. });
  1372. // }
  1373. }
  1374. }
  1375. // 如果上述没有结果,则用 y 求出对应的 t,再用 t 求出对应的 x,与 offsetX 进行匹配
  1376. const tsy = this.getThreeBezierT(
  1377. curve.start.y,
  1378. curve.controls[0].y,
  1379. curve.controls[1].y,
  1380. curve.end.y,
  1381. offsetY
  1382. );
  1383. for (let y = 0; y < 3; y++) {
  1384. if (tsy[y] <= 1 && tsy[y] >= 0) {
  1385. const point = this.getThreeBezierPoint(
  1386. tsy[y],
  1387. curve.start,
  1388. curve.controls[0],
  1389. curve.controls[1],
  1390. curve.end
  1391. );
  1392. // if (Math.abs(point.x - offsetX) < rang) {
  1393. results.push({
  1394. position: point,
  1395. distance: this.getDistance(point, position),
  1396. });
  1397. // }
  1398. }
  1399. }
  1400. console.log(results);
  1401. return results.sort((a, b) => a.distance - b.distance)[0];
  1402. }
  1403. // 二次曲线
  1404. getHitInfoForTwoBezier(position, curve) {
  1405. let bezierData = [];
  1406. bezierData.push(curve.start.x);
  1407. bezierData.push(curve.start.y);
  1408. bezierData.push(curve.controls[0].x);
  1409. bezierData.push(curve.controls[0].y);
  1410. bezierData.push(curve.end.x);
  1411. bezierData.push(curve.end.y);
  1412. const { isHit, getInfo } = bezierUtil.measureBezier(...bezierData);
  1413. const { point } = getInfo(position);
  1414. return {
  1415. position: {
  1416. x: point[0],
  1417. y: point[1],
  1418. },
  1419. distance: this.getDistance(position, {
  1420. x: point[0],
  1421. y: point[1],
  1422. }),
  1423. };
  1424. }
  1425. getHitInfoForCurves(pos, curves, roadWidth) {
  1426. let joinInfo;
  1427. for (const curve of curves) {
  1428. const tempJoinInfo =
  1429. curve.controls.length === 2
  1430. ? this.getHitInfoForThreeBezier(pos, curve, roadWidth / 2)
  1431. : this.getHitInfoForTwoBezier(pos, curve);
  1432. if (
  1433. !joinInfo ||
  1434. (tempJoinInfo && tempJoinInfo.distance < joinInfo.distance)
  1435. ) {
  1436. joinInfo = tempJoinInfo;
  1437. }
  1438. }
  1439. return joinInfo;
  1440. }
  1441. getHitInfoForCurve(pos, curve, roadWidth) {
  1442. let joinInfo;
  1443. const tempJoinInfo =
  1444. curve.controls.length === 2
  1445. ? this.getHitInfoForThreeBezier(pos, curve, roadWidth / 2)
  1446. : this.getHitInfoForTwoBezier(pos, curve);
  1447. if (
  1448. !joinInfo ||
  1449. (tempJoinInfo && tempJoinInfo.distance < joinInfo.distance)
  1450. ) {
  1451. joinInfo = tempJoinInfo;
  1452. }
  1453. return joinInfo;
  1454. }
  1455. getIndexForCurvesPoints(position, points) {
  1456. let minDis = null;
  1457. let minDisToPoint = null;
  1458. let minPointIndex = -1;
  1459. let index = -1;
  1460. for (let i = 0; i < points.length - 1; ++i) {
  1461. const line = this.createLine1(points[i], points[i + 1]);
  1462. const join = this.getJoinLinePoint(position, line);
  1463. const dis = this.getDistance(position, join);
  1464. if (this.isContainForSegment(join, points[i], points[i + 1])) {
  1465. if (minDis == null || minDis > dis) {
  1466. minDis = dis;
  1467. index = i + 1;
  1468. }
  1469. }
  1470. if (minDisToPoint == null) {
  1471. minDisToPoint = mathUtil.getDistance(position, points[i]);
  1472. minPointIndex = i;
  1473. } else if (minDisToPoint > mathUtil.getDistance(position, points[i])) {
  1474. minDisToPoint = mathUtil.getDistance(position, points[i]);
  1475. minPointIndex = i;
  1476. }
  1477. }
  1478. if (index == -1) {
  1479. if (
  1480. minDisToPoint >
  1481. mathUtil.getDistance(position, points[points.length - 1])
  1482. ) {
  1483. return points.length;
  1484. } else {
  1485. return minPointIndex;
  1486. }
  1487. } else {
  1488. return index;
  1489. }
  1490. }
  1491. getCurvesIndexForCurvesPoints(position, points) {
  1492. let minDis = null;
  1493. let minDisToPoint = null;
  1494. let minPointIndex = -1;
  1495. let index = -1;
  1496. for (let i = 0; i < points.length - 1; ++i) {
  1497. const line = this.createLine1(points[i], points[i + 1]);
  1498. const join = this.getJoinLinePoint(position, line);
  1499. const dis = this.getDistance(position, join);
  1500. if (this.isContainForSegment(join, points[i], points[i + 1])) {
  1501. if (minDis == null || minDis > dis) {
  1502. minDis = dis;
  1503. index = i;
  1504. }
  1505. }
  1506. if (minDisToPoint == null) {
  1507. minDisToPoint = mathUtil.getDistance(position, points[i]);
  1508. minPointIndex = i;
  1509. } else if (minDisToPoint > mathUtil.getDistance(position, points[i])) {
  1510. minDisToPoint = mathUtil.getDistance(position, points[i]);
  1511. minPointIndex = i;
  1512. }
  1513. }
  1514. if ((index = -1)) {
  1515. if (
  1516. minDisToPoint >
  1517. mathUtil.getDistance(position, points[points.length - 1])
  1518. ) {
  1519. return points.length - 2;
  1520. } else {
  1521. return minPointIndex;
  1522. }
  1523. } else {
  1524. return index;
  1525. }
  1526. }
  1527. // //获取一组点的偏移
  1528. // getOffset(points, leftWidth, rightWidth, dir) {
  1529. // //斜边长度d已知,角度angle已知
  1530. // //对边长度就是y的偏移量 就是 d * sin(angle) ==> d * Math.sin(angle * Math.PI / 180)
  1531. // //邻边长度就是x的偏移量 就是 d * cos(angle) ==> d * Math.cos(angle * Math.PI / 180)
  1532. // let result = {};
  1533. // if (dir == "left" || !dir) {
  1534. // let angle = 90;
  1535. // let d = leftWidth;
  1536. // result.leftEdgePoints = points.map((coords) => {
  1537. // let ox = d * Math.cos((angle * Math.PI) / 180);
  1538. // let oy = d * Math.sin((angle * Math.PI) / 180);
  1539. // return {
  1540. // x: coords.x + ox,
  1541. // y: coords.y + oy,
  1542. // };
  1543. // });
  1544. // }
  1545. // if (dir == "right" || !dir) {
  1546. // let angle = -90;
  1547. // let d = rightWidth;
  1548. // result.rightEdgePoints = points.map((coords) => {
  1549. // let ox = d * Math.cos((angle * Math.PI) / 180);
  1550. // let oy = d * Math.sin((angle * Math.PI) / 180);
  1551. // return {
  1552. // x: coords.x + ox,
  1553. // y: coords.y + oy,
  1554. // };
  1555. // });
  1556. // }
  1557. // return result;
  1558. // }
  1559. getOffset(points, leftWidth, rightWidth, dir) {
  1560. let leftEdgePoints = [];
  1561. let rightEdgePoints = [];
  1562. for (let i = 0; i < points.length - 1; ++i) {
  1563. if (dir == "left" || !dir) {
  1564. let leftEdgePoins1 = this.RectangleVertex(
  1565. points[i],
  1566. points[i + 1],
  1567. leftWidth * 2
  1568. );
  1569. let leftLine1 = mathUtil.createLine1(
  1570. leftEdgePoins1.leftEdgeStart,
  1571. leftEdgePoins1.leftEdgeEnd
  1572. );
  1573. if (i != points.length - 2) {
  1574. let leftEdgePoins2 = this.RectangleVertex(
  1575. points[i + 1],
  1576. points[i + 2],
  1577. leftWidth * 2
  1578. );
  1579. let leftLine2 = mathUtil.createLine1(
  1580. leftEdgePoins2.leftEdgeStart,
  1581. leftEdgePoins2.leftEdgeEnd
  1582. );
  1583. let join = mathUtil.getIntersectionPoint(leftLine1, leftLine2);
  1584. if (join != null) {
  1585. leftEdgePoints[i + 1] = join;
  1586. } else {
  1587. leftEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1588. points[i + 1],
  1589. leftLine1
  1590. );
  1591. }
  1592. } else {
  1593. leftEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1594. points[i + 1],
  1595. leftLine1
  1596. );
  1597. }
  1598. if (!leftEdgePoints[0]) {
  1599. leftEdgePoints[0] = mathUtil.getJoinLinePoint(points[0], leftLine1);
  1600. }
  1601. }
  1602. if (dir == "right" || !dir) {
  1603. let rightEdgePoins1 = this.RectangleVertex(
  1604. points[i],
  1605. points[i + 1],
  1606. rightWidth * 2
  1607. );
  1608. let rightLine1 = mathUtil.createLine1(
  1609. rightEdgePoins1.rightEdgeStart,
  1610. rightEdgePoins1.rightEdgeEnd
  1611. );
  1612. if (i != points.length - 2) {
  1613. let rightEdgePoins2 = this.RectangleVertex(
  1614. points[i + 1],
  1615. points[i + 2],
  1616. rightWidth * 2
  1617. );
  1618. let rightLine2 = mathUtil.createLine1(
  1619. rightEdgePoins2.rightEdgeStart,
  1620. rightEdgePoins2.rightEdgeEnd
  1621. );
  1622. let join = mathUtil.getIntersectionPoint(rightLine1, rightLine2);
  1623. if (join != null) {
  1624. rightEdgePoints[i + 1] = join;
  1625. } else {
  1626. rightEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1627. points[i + 1],
  1628. rightLine1
  1629. );
  1630. }
  1631. } else {
  1632. rightEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1633. points[i + 1],
  1634. rightLine1
  1635. );
  1636. }
  1637. if (!rightEdgePoints[0]) {
  1638. rightEdgePoints[0] = mathUtil.getJoinLinePoint(points[0], rightLine1);
  1639. }
  1640. }
  1641. }
  1642. return {
  1643. leftEdgePoints: leftEdgePoints,
  1644. rightEdgePoints: rightEdgePoints,
  1645. };
  1646. }
  1647. twoOrderBezier(t, p1, cp, p2) {
  1648. //参数分别是t,起始点,控制点和终点
  1649. var x1 = p1.x;
  1650. var y1 = p1.y;
  1651. var cx = cp.x;
  1652. var cy = cp.y;
  1653. var x2 = p2.x;
  1654. var y2 = p2.y;
  1655. // var [x1, y1] = p1,
  1656. // [cx, cy] = cp,
  1657. // [x2, y2] = p2;
  1658. var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2,
  1659. y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
  1660. return {
  1661. x: x,
  1662. y: y,
  1663. };
  1664. }
  1665. //t是0.5,求cp。p是曲线上的点
  1666. twoOrderBezier2(t, p1, p, p2) {
  1667. var x1 = p1.x;
  1668. var y1 = p1.y;
  1669. var x2 = p2.x;
  1670. var y2 = p2.y;
  1671. let cx = (p.x - t * t * x2 - (1 - t) * (1 - t) * x1) / (2 * t * (1 - t));
  1672. let cy = (p.y - t * t * y2 - (1 - t) * (1 - t) * y1) / (2 * t * (1 - t));
  1673. return {
  1674. x: cx,
  1675. y: cy,
  1676. };
  1677. }
  1678. rgb() {
  1679. //rgb颜色随机
  1680. const r = Math.floor(Math.random() * 256);
  1681. const g = Math.floor(Math.random() * 256);
  1682. const b = Math.floor(Math.random() * 256);
  1683. return `rgb(${r},${g},${b})`;
  1684. }
  1685. }
  1686. const mathUtil = new MathUtil();
  1687. export { mathUtil };