MathUtil.js 53 KB

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