MathUtil.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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. getArrow(start, end, ange = 30, L = 20) {
  319. let a = Math.atan2(end.y - start.y, end.x - start.x);
  320. let xC = end.x - L * Math.cos(a + (ange * Math.PI) / 180); // θ=30
  321. let yC = end.y - L * Math.sin(a + (ange * Math.PI) / 180);
  322. let xD = end.x - L * Math.cos(a - (ange * Math.PI) / 180);
  323. let yD = end.y - L * Math.sin(a - (ange * Math.PI) / 180);
  324. return [{ x: xC, y: yC }, end, { x: xD, y: yD }];
  325. }
  326. //经过point且与line垂直的线
  327. getLineForPoint(line, point) {
  328. let parameter = {};
  329. if (line.a == 0 || typeof line.a == "undefined") {
  330. if (line.hasOwnProperty("x")) {
  331. parameter.x = line.x;
  332. parameter.y = point.y;
  333. } else if (line.hasOwnProperty("y")) {
  334. parameter.x = point.x;
  335. parameter.y = line.y;
  336. }
  337. } else {
  338. parameter.a = -1 / line.a;
  339. parameter.b = point.y - point.x * parameter.a;
  340. }
  341. return parameter;
  342. }
  343. // 经过point且与line垂直的直线,该直线与line的交点
  344. getJoinLinePoint(point, line) {
  345. const verticalLine = this.getVerticalLine(line, point);
  346. const join = this.getIntersectionPoint(line, verticalLine);
  347. return join;
  348. }
  349. // 点到直线的距离
  350. getDisForPoinLine(point, line) {
  351. const join = this.getJoinLinePoint(point, line);
  352. return this.getDistance(point, join);
  353. }
  354. // 垂直线
  355. getVerticalLine(line, point) {
  356. if (typeof line.a === "undefined") {
  357. if (line.hasOwnProperty("x")) {
  358. return { y: point.y };
  359. } else if (line.hasOwnProperty("y")) {
  360. return { x: point.x };
  361. } else {
  362. return null;
  363. }
  364. } else if (line.a == 0) {
  365. return { x: point.x };
  366. } else {
  367. const tl = {};
  368. tl.a = -1 / line.a;
  369. const result = this.createLine3(tl, point);
  370. return result;
  371. }
  372. }
  373. //point在直线上,只是不确定是否在线段上
  374. //方法:point到startPoint和endPoint的距离之和与startPoint和endPoint之间的距离对比
  375. isContainForSegment(point, startPoint, endPoint, minDis) {
  376. if (!minDis) {
  377. minDis = Constant.minLen;
  378. }
  379. let dis1 =
  380. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  381. let dis2 = this.getDistance(startPoint, endPoint);
  382. if (Math.abs(dis1 - dis2) < minDis) {
  383. return true;
  384. } else {
  385. return false;
  386. }
  387. }
  388. /*
  389. //minDis
  390. isPointInPoly(point, points, minDis) {
  391. if (!minDis) {
  392. minDis = Constant.minRealDis
  393. }
  394. const x = point.x
  395. const y = point.y
  396. let inside = false
  397. // 是否在顶点附近
  398. for (let i = 0; i < points.length; ++i) {
  399. let distance = this.getDistance(point, points[i])
  400. if (distance < minDis) {
  401. return true
  402. }
  403. }
  404. // 是否在边沿
  405. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  406. let pt1 = points[i]
  407. let pt2 = points[j]
  408. const flag = this.isContainForSegment(point, pt1, pt2, minDis)
  409. if (flag) {
  410. return true
  411. }
  412. }
  413. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  414. let pt1 = points[i]
  415. let pt2 = points[j]
  416. const xi = pt1.x
  417. const yi = pt1.y
  418. const xj = pt2.x
  419. const yj = pt2.y
  420. const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
  421. if (intersect) inside = !inside
  422. }
  423. return inside
  424. }
  425. */
  426. isPointInPoly(point, points) {
  427. const x = point.x;
  428. const y = point.y;
  429. let inside = false;
  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 =
  438. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  439. if (intersect) inside = !inside;
  440. }
  441. return inside;
  442. }
  443. //a表示横轴,b表示竖轴
  444. isPointInElliptic(point, center, a, b) {
  445. let r =
  446. Math.pow((point.x - center.x) / a, 2) +
  447. Math.pow((point.y - center.y) / b, 2);
  448. if (r <= 1) {
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. }
  454. // 点到线段的距离
  455. // 在minDistance范围内,会吸附到point1/point2上
  456. // 返回值:type是1表示吸附在point1,是2表示吸附在point2,是0表示在线段point1-point2上;
  457. getDisForPoinSegment(point, point1, point2, minDistance) {
  458. const line = this.createLine1(point1, point2);
  459. const join = this.getJoinLinePoint(point, line);
  460. const dis = this.getDistance(point1, point2);
  461. const dis1 = this.getDistance(join, point1);
  462. const dis2 = this.getDistance(join, point2);
  463. if (
  464. this.getDistance(join, point1) > dis ||
  465. this.getDistance(join, point2) > dis
  466. ) {
  467. // 在线段外
  468. if (dis1 < dis2 && dis1 < minDistance) {
  469. return { type: 1, join: point1 };
  470. } else if (dis2 < dis1 && dis2 < minDistance) {
  471. return { type: 2, join: point2 };
  472. } else {
  473. return null;
  474. }
  475. } else {
  476. if (dis1 < minDistance) {
  477. return { type: 1, join: point1 };
  478. } else if (dis2 < minDistance) {
  479. return { type: 2, join: point2 };
  480. } else if (this.getDistance(point, join) < minDistance) {
  481. return { type: 0, join: join };
  482. }
  483. }
  484. }
  485. PointInSegment(Q, pi, pj, minDis) {
  486. if (
  487. this.getDistance(Q, pi) < Constant.minAdsorbPix ||
  488. this.getDistance(Q, pj) < Constant.minAdsorbPix
  489. ) {
  490. return true;
  491. }
  492. if (!minDis) {
  493. minDis = 0.1;
  494. }
  495. minDis = minDis / 2;
  496. const offset1 = (Q.x - pi.x) * (pj.y - pi.y) - (pj.x - pi.x) * (Q.y - pi.y);
  497. const offset2 = Math.min(pi.x, pj.x) - Q.x;
  498. const offset3 = Q.x - Math.max(pi.x, pj.x);
  499. const offset4 = Math.min(pi.y, pj.y) - Q.y;
  500. const offset5 = Q.y - Math.max(pi.y, pj.y);
  501. if (
  502. Math.abs(offset1) < minDis &&
  503. (offset2 <= 0 || Math.abs(offset2) < minDis) &&
  504. (offset3 <= 0 || Math.abs(offset3) < minDis) &&
  505. (offset4 <= 0 || Math.abs(offset4) < minDis) &&
  506. (offset5 <= 0 || Math.abs(offset5) < minDis)
  507. ) {
  508. return true;
  509. } else {
  510. return false;
  511. }
  512. }
  513. //点p是否在线段AB上
  514. isPointOnSegment(p, A, B) {
  515. // 计算向量 AP 和 BP
  516. const AP = {
  517. x: p.x - A.x,
  518. y: p.y - A.y,
  519. };
  520. const BP = {
  521. x: p.x - B.x,
  522. y: p.y - B.y,
  523. };
  524. // 计算向量 AB 的长度和方向
  525. const AB = {
  526. x: B.x - A.x,
  527. y: B.y - A.y,
  528. };
  529. const AB_length = this.getDistance(A, B);
  530. const AB_direction = {
  531. x: AB.x / AB_length,
  532. y: AB.y / AB_length,
  533. };
  534. // 检查 AP 和 BP 的方向是否与 AB 相同,并检查它们的长度是否小于等于 AB 的长度
  535. const dot_product_AP = AP.x * AB_direction.x + AP.y * AB_direction.y;
  536. const dot_product_BP = BP.x * AB_direction.x + BP.y * AB_direction.y;
  537. //return dot_product_AP >= 0 && dot_product_BP <= 0 && Math.abs(AP.x * BP.y - AP.y * BP.x) <= AB_length * Number.EPSILON;
  538. return (
  539. dot_product_AP >= 0 &&
  540. dot_product_BP <= 0 &&
  541. Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.01
  542. );
  543. }
  544. clonePoint(p1, p2) {
  545. p1.x = p2.x;
  546. p1.y = p2.y;
  547. }
  548. clonePoints(points1, points2) {
  549. for (let i = 0; i < points1.length; ++i) {
  550. this.clonePoint(points1[i], points2[i]);
  551. }
  552. }
  553. equalPoint(p1, p2) {
  554. if (p1.x == p2.x && p1.y == p2.y) {
  555. return true;
  556. } else {
  557. return false;
  558. }
  559. }
  560. equalPoints(points1, points2) {
  561. for (let i = 0; i < points1.length; ++i) {
  562. let flag = this.equalPoint(points1[i], points2[i]);
  563. if (!flag) {
  564. return false;
  565. }
  566. }
  567. return true;
  568. }
  569. equalJSON(json1, json2) {
  570. for (let key in json1) {
  571. if (json2.hasOwnProperty(key) && json1[key] == json2[key]) {
  572. continue;
  573. } else {
  574. return false;
  575. }
  576. }
  577. for (let key in json2) {
  578. if (json1.hasOwnProperty(key) && json1[key] == json2[key]) {
  579. continue;
  580. } else {
  581. return false;
  582. }
  583. }
  584. return true;
  585. }
  586. crossTwoLines(point1, point2, point3, point4, dis) {
  587. if (typeof dis == "undefined") {
  588. dis = Constant.minAdsorbPix;
  589. }
  590. const join = this.getIntersectionPoint2(point1, point2, point3, point4);
  591. if (join != null) {
  592. if (
  593. this.getDistance(point1, join) > dis &&
  594. this.getDistance(point2, join) > dis &&
  595. this.getDistance(point3, join) > dis &&
  596. this.getDistance(point4, join) > dis
  597. ) {
  598. if (
  599. this.getDistance(point1, join) < this.getDistance(point1, point2) &&
  600. this.getDistance(point2, join) < this.getDistance(point1, point2) &&
  601. this.getDistance(point3, join) < this.getDistance(point3, point4) &&
  602. this.getDistance(point4, join) < this.getDistance(point3, point4)
  603. ) {
  604. return true;
  605. } else {
  606. return false;
  607. }
  608. }
  609. } else {
  610. if (
  611. this.PointInSegment(point1, point3, point4) ||
  612. this.PointInSegment(point2, point3, point4)
  613. ) {
  614. return true;
  615. }
  616. }
  617. return false;
  618. }
  619. getDisPointsLine(line, point, distance1, distance2) {
  620. const newpoint1 = {};
  621. const newpoint2 = {};
  622. const result = {};
  623. if (line.hasOwnProperty("x")) {
  624. newpoint1.x = line.x;
  625. newpoint1.y = point.y - distance1;
  626. newpoint2.x = line.x;
  627. newpoint2.y = point.y + distance2;
  628. } else if (line.hasOwnProperty("y")) {
  629. newpoint1.y = line.y;
  630. newpoint1.x = point.x - distance1;
  631. newpoint2.y = line.y;
  632. newpoint2.x = point.x + distance2;
  633. } else {
  634. const a = Math.atan(line.a);
  635. const t_line = { a: -1 / line.a };
  636. const line_ab2 = this.createLine3(t_line, point);
  637. const join = this.getIntersectionPoint(line, line_ab2);
  638. newpoint1.x = join.x - distance1 * Math.cos(a);
  639. newpoint1.y = join.y - distance1 * Math.sin(a);
  640. newpoint2.x = join.x + distance2 * Math.cos(a);
  641. newpoint2.y = join.y + distance2 * Math.sin(a);
  642. }
  643. result.newpoint1 = newpoint1;
  644. result.newpoint2 = newpoint2;
  645. return result;
  646. }
  647. getBoundingBox(points) {
  648. let minX = points[0].x;
  649. let maxX = points[0].x;
  650. let minY = points[0].y;
  651. let maxY = points[0].y;
  652. for (let i = 1; i < points.length; ++i) {
  653. const point = points[i];
  654. if (minX > point.x) {
  655. minX = point.x;
  656. }
  657. if (minY > point.y) {
  658. minY = point.y;
  659. }
  660. if (maxX < point.x) {
  661. maxX = point.x;
  662. }
  663. if (maxY < point.y) {
  664. maxY = point.y;
  665. }
  666. }
  667. const box = {};
  668. box.minX = minX;
  669. box.minY = minY;
  670. box.maxX = maxX;
  671. box.maxY = maxY;
  672. return box;
  673. }
  674. getBoundingBox2(points) {
  675. let minX = null;
  676. let maxX = null;
  677. let minY = null;
  678. let maxY = null;
  679. for (let key in points) {
  680. const point = points[key];
  681. if (minX == null || minX > point.x) {
  682. minX = point.x;
  683. }
  684. if (minY == null || minY > point.y) {
  685. minY = point.y;
  686. }
  687. if (maxX == null || maxX < point.x) {
  688. maxX = point.x;
  689. }
  690. if (maxY == null || maxY < point.y) {
  691. maxY = point.y;
  692. }
  693. }
  694. const box = {};
  695. box.minX = minX;
  696. box.minY = minY;
  697. box.maxX = maxX;
  698. box.maxY = maxY;
  699. return box;
  700. }
  701. ComputePolygonArea(points) {
  702. const point_num = points.length;
  703. if (point_num < 3) {
  704. return 0;
  705. }
  706. let s = points[0].y * (points[point_num - 1].x - points[1].x);
  707. for (let i = 1; i < point_num; ++i)
  708. s += points[i].y * (points[i - 1].x - points[(i + 1) % point_num].x);
  709. return Math.abs(s / 2.0);
  710. }
  711. // 获取多边形重心
  712. getPolygonCore(points) {
  713. function Area(p0, p1, p2) {
  714. let area = 0.0;
  715. area =
  716. p0.x * p1.y +
  717. p1.x * p2.y +
  718. p2.x * p0.y -
  719. p1.x * p0.y -
  720. p2.x * p1.y -
  721. p0.x * p2.y;
  722. return area / 2;
  723. }
  724. let sum_x = 0;
  725. let sum_y = 0;
  726. let sum_area = 0;
  727. let p1 = points[1];
  728. for (let i = 2; i < points.length; i++) {
  729. const p2 = points[i];
  730. const area = Area(points[0], p1, p2);
  731. sum_area += area;
  732. sum_x += (points[0].x + p1.x + p2.x) * area;
  733. sum_y += (points[0].y + p1.y + p2.y) * area;
  734. p1 = p2;
  735. }
  736. const xx = sum_x / sum_area / 3;
  737. const yy = sum_y / sum_area / 3;
  738. return {
  739. x: xx,
  740. y: yy,
  741. };
  742. }
  743. // points1是否在points2里
  744. isPolyInPoly(points1, points2, minDis) {
  745. for (let i = 0; i < points1.length; ++i) {
  746. let flag = false;
  747. for (let j = 0; j < points2.length; ++j) {
  748. if (this.equalPoint(points1[i], points2[j])) {
  749. flag = true;
  750. break;
  751. }
  752. }
  753. if (!flag) {
  754. if (!this.isPointInPoly(points1[i], points2, minDis)) {
  755. return false;
  756. }
  757. } else {
  758. const nextIndex = i == points1.length - 1 ? 0 : i + 1;
  759. const mid = {
  760. x: (points1[i].x + points1[nextIndex].x) / 2,
  761. y: (points1[i].y + points1[nextIndex].y) / 2,
  762. };
  763. if (!this.isPointInPoly(mid, points2, minDis)) {
  764. return false;
  765. }
  766. }
  767. }
  768. return true;
  769. }
  770. dotPoints(pt1, pt2, point1, point2) {
  771. let vt1 = {};
  772. let vt2 = {};
  773. vt1.start = {};
  774. vt1.end = {};
  775. vt1.start.x = 0;
  776. vt1.start.y = 0;
  777. vt1.end.x = pt2.x - pt1.x;
  778. vt1.end.y = pt2.y - pt1.y;
  779. vt2.start = {};
  780. vt2.end = {};
  781. vt2.start.x = 0;
  782. vt2.start.y = 0;
  783. vt2.end.x = point2.x - point1.x;
  784. vt2.end.y = point2.y - point1.y;
  785. let result = vt1.end.x * vt2.end.x + vt1.end.y * vt2.end.y;
  786. return result;
  787. }
  788. //start是起点,target是朝着目标移动,distance是移动的距离
  789. translate(start, target, point, distance) {
  790. let dx = target.x - start.x;
  791. let dy = target.y - start.y;
  792. let dis = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  793. let result = {
  794. x: point.x + (dx * distance) / dis,
  795. y: point.y + (dy * distance) / dis,
  796. };
  797. return result;
  798. }
  799. //射线与线段相交
  800. // intersection(rayStart, rayEnd, segmentStart, segmentEnd) {
  801. // // 计算射线和线段的方向向量
  802. // const rayDirection = {
  803. // x:rayEnd.x - rayStart.x,
  804. // y:rayEnd.y - rayStart.y,
  805. // };
  806. // const segmentDirection = {
  807. // x:segmentEnd.x - segmentStart.x,
  808. // y:segmentEnd.y - segmentStart.y,
  809. // };
  810. // // 计算射线和线段的起点之间的向量
  811. // const startPointVector = {
  812. // x:rayStart.x - segmentStart.x,
  813. // y:rayStart.y - segmentStart.y,
  814. // };
  815. // // 计算射线和线段的叉积
  816. // const crossProduct = rayDirection.x * segmentDirection.y - rayDirection.y * segmentDirection.x;
  817. // // 如果叉积为0,则表示射线和线段平行
  818. // if (crossProduct === 0) {
  819. // return null;
  820. // }
  821. // // 计算线段起点到射线的交点的向量
  822. // const t = (startPointVector.x * segmentDirection.y - startPointVector.y * segmentDirection.x) / crossProduct;
  823. // // 如果t的值小于0,则交点在射线的起点之后
  824. // if (t < 0) {
  825. // return null;
  826. // }
  827. // // 计算交点的坐标
  828. // const intersectionX = rayStart.x + t * rayDirection.x;
  829. // const intersectionY = rayStart.y + t * rayDirection.y;
  830. // // 如果交点在线段的范围内,则返回交点坐标
  831. // if ((intersectionX >= Math.min(segmentStart.x, segmentEnd.x)) &&
  832. // (intersectionX <= Math.max(segmentStart.x, segmentEnd.x)) &&
  833. // (intersectionY >= Math.min(segmentStart.y, segmentEnd.y)) &&
  834. // (intersectionY <= Math.max(segmentStart.y, segmentEnd.y))) {
  835. // //return [intersectionX, intersectionY];
  836. // return {
  837. // x:intersectionX,
  838. // y:intersectionY,
  839. // };
  840. // }
  841. // // 否则返回null
  842. // return null;
  843. // }
  844. // raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  845. // // 计算射线和线段的交点
  846. // const x1 = rayOrigin.x
  847. // const y1 = rayOrigin.y
  848. // const x2 = segmentStart.x
  849. // const y2 = segmentStart.y
  850. // const dx1 = rayDirection.x
  851. // const dy1 = rayDirection.y
  852. // const dx2 = segmentEnd.x - x2
  853. // const dy2 = segmentEnd.y - y2
  854. // const crossProduct = dx1 * dy2 - dx2 * dy1
  855. // if (Math.abs(crossProduct) < 1e-8) {
  856. // // 射线和线段平行或共线
  857. // return null
  858. // }
  859. // const t1 = (dx2 * (y1 - y2) - dy2 * (x1 - x2)) / crossProduct
  860. // const t2 = (dx1 * (y1 - y2) - dy1 * (x1 - x2)) / crossProduct
  861. // if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
  862. // // 有交点,计算交点坐标
  863. // const intersectionX = x1 + t1 * dx1
  864. // const intersectionY = y1 + t1 * dy1
  865. // return {
  866. // x: intersectionX,
  867. // y: intersectionY,
  868. // }
  869. // } else {
  870. // // 没有交点
  871. // return null
  872. // }
  873. // }
  874. raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  875. const end = {
  876. x: rayOrigin.x + rayDirection.x,
  877. y: rayOrigin.y - rayDirection.z,
  878. };
  879. const line = this.createLine1(rayOrigin, end);
  880. const join = this.getIntersectionPoint4(segmentStart, segmentEnd, line);
  881. if (join == null) {
  882. return null;
  883. } else {
  884. const dis = this.getDistance(end, join);
  885. const dis1 = this.getDistance(rayOrigin, join);
  886. const dis2 = this.getDistance(rayOrigin, end);
  887. if (dis - (dis1 + dis2) + 0.01 > 0) {
  888. return null;
  889. } else {
  890. return join;
  891. }
  892. }
  893. }
  894. RectangleVertex(startPoint, endPoint, width) {
  895. let line = this.createLine1(startPoint, endPoint);
  896. let lines = this.getParallelLineForDistance(line, width / 2);
  897. let leftEdgeStart, rightEdgeStart, rightEdgeEnd, leftEdgeEnd;
  898. let point = null;
  899. let points = [];
  900. //先计算start部分
  901. point = startPoint;
  902. points.push(endPoint);
  903. points.push(startPoint);
  904. let point1 = this.getJoinLinePoint(point, lines.line1);
  905. let point2 = this.getJoinLinePoint(point, lines.line2);
  906. points[2] = point1;
  907. if (this.isClockwise(points)) {
  908. rightEdgeStart = point1;
  909. leftEdgeStart = point2;
  910. } else {
  911. rightEdgeStart = point2;
  912. leftEdgeStart = point1;
  913. }
  914. //再计算end部分
  915. points = [];
  916. point = endPoint;
  917. points.push(startPoint);
  918. points.push(endPoint);
  919. point1 = this.getJoinLinePoint(point, lines.line1);
  920. point2 = this.getJoinLinePoint(point, lines.line2);
  921. points[2] = point1;
  922. if (this.isClockwise(points)) {
  923. rightEdgeEnd = point2;
  924. leftEdgeEnd = point1;
  925. } else {
  926. rightEdgeEnd = point1;
  927. leftEdgeEnd = point2;
  928. }
  929. return {
  930. leftEdgeStart: leftEdgeStart,
  931. rightEdgeStart: rightEdgeStart,
  932. rightEdgeEnd: rightEdgeEnd,
  933. leftEdgeEnd: leftEdgeEnd,
  934. };
  935. }
  936. //start到end的射线中取一点point,start-end和end-point的距离相同
  937. getPositionForExtendedLine(start, end) {
  938. const dx = end.x - start.x;
  939. const dy = end.y - start.y;
  940. const point = {
  941. x: end.x + dx,
  942. y: end.y + dy,
  943. };
  944. return point;
  945. }
  946. isOnRay(start, dir, position) {
  947. const v1 = { x: dir.x - start.x, y: dir.y - start.y };
  948. const v2 = { x: position.x - start.x, y: position.y - start.y };
  949. return v1.x * v2.y - v1.y * v2.x;
  950. }
  951. //向量是否同样的方向
  952. isSameDirForVector(point1, point2, p1, p2) {
  953. const v1 = {
  954. x: point2.x - point1.x,
  955. y: point2.y - point1.y,
  956. };
  957. const v2 = {
  958. x: p2.x - p1.x,
  959. y: p2.y - p1.y,
  960. };
  961. const value = this.dot(v1, v2);
  962. if (value > 0) {
  963. return true;
  964. }
  965. {
  966. return false;
  967. }
  968. }
  969. //生成五角星
  970. createFivePointedStar(position, r) {
  971. let deg = Math.PI / 180; //角度
  972. let points = [];
  973. points[0] = {
  974. x: position.x - r * Math.cos(54 * deg),
  975. y: position.y + r * Math.sin(54 * deg),
  976. };
  977. points[1] = {
  978. x: position.x,
  979. y: position.y - r,
  980. };
  981. points[2] = {
  982. x: position.x + r * Math.cos(54 * deg),
  983. y: position.y + r * Math.sin(54 * deg),
  984. };
  985. points[3] = {
  986. x: position.x - r * Math.cos(18 * deg),
  987. y: position.y - r * Math.sin(18 * deg),
  988. };
  989. points[4] = {
  990. x: position.x + r * Math.cos(18 * deg),
  991. y: position.y - r * Math.sin(18 * deg),
  992. };
  993. return points;
  994. }
  995. //求圆和直线之间的交点
  996. /**
  997. * 求圆和直线之间的交点
  998. * 直线方程:y = kx + b
  999. * 圆的方程:(x - m)² + (x - n)² = r²
  1000. * x1, y1 = 线坐标1, x2, y2 = 线坐标2, m, n = 圆坐标, r = 半径
  1001. */
  1002. getInsertPointBetweenCircleAndLine(x1, y1, x2, y2, m, n, radius) {
  1003. let insertPoints = [];
  1004. if (Math.abs(x1 - x2) < 0.5) {
  1005. insertPoints[0] = {
  1006. x: x1,
  1007. y: n - Math.sqrt(radius * radius - Math.pow(x1 - m, 2)),
  1008. };
  1009. insertPoints[1] = {
  1010. x: x1,
  1011. y: n + Math.sqrt(radius * radius - Math.pow(x1 - m, 2)),
  1012. };
  1013. return insertPoints;
  1014. }
  1015. // console.log(x1, y1, x2, y2, m, n, radius)
  1016. let kbArr = this.binaryEquationGetKB(x1, y1, x2, y2);
  1017. let k = kbArr[0];
  1018. let b = kbArr[1];
  1019. let aX = 1 + k * k;
  1020. let bX = 2 * k * (b - n) - 2 * m;
  1021. let cX = m * m + (b - n) * (b - n) - radius * radius;
  1022. let xArr = this.quadEquationGetX(aX, bX, cX);
  1023. xArr.forEach((x) => {
  1024. let y = k * x + b;
  1025. insertPoints.push({ x: x, y: y });
  1026. });
  1027. return insertPoints;
  1028. }
  1029. /**
  1030. * 求二元一次方程的系数
  1031. * y1 = k * x1 + b => k = (y1 - b) / x1
  1032. * y2 = k * x2 + b => y2 = ((y1 - b) / x1) * x2 + b
  1033. */
  1034. binaryEquationGetKB(x1, y1, x2, y2) {
  1035. let k = (y1 - y2) / (x1 - x2);
  1036. let b = (x1 * y2 - x2 * y1) / (x1 - x2);
  1037. return [k, b];
  1038. }
  1039. /**
  1040. * 一元二次方程求根
  1041. * ax² + bx + c = 0
  1042. */
  1043. quadEquationGetX(a, b, c) {
  1044. let xArr = [];
  1045. let result = Math.pow(b, 2) - 4 * a * c;
  1046. if (result > 0) {
  1047. xArr.push((-b + Math.sqrt(result)) / (2 * a));
  1048. xArr.push((-b - Math.sqrt(result)) / (2 * a));
  1049. }
  1050. //else if (result == 0) {
  1051. else {
  1052. xArr.push(-b / (2 * a));
  1053. }
  1054. return xArr;
  1055. }
  1056. angleTo(v1, v2) {
  1057. const denominator = Math.sqrt(this.lengthSq(v1) * this.lengthSq(v2));
  1058. if (denominator === 0) return 90;
  1059. const theta = this.dot(v1, v2) / denominator;
  1060. //return Math.acos(this.clamp(theta, -1, 1));
  1061. return (Math.acos(this.clamp(theta, -1, 1)) / Math.PI) * 180;
  1062. }
  1063. //点乘
  1064. dot(v1, v2) {
  1065. return v1.x * v2.x + v1.y * v2.y;
  1066. }
  1067. //叉乘
  1068. cross(v1, v2) {
  1069. return v1.x * v2.y - v1.y * v2.x;
  1070. }
  1071. // 两点相减
  1072. pointMinus(v1, v2) {
  1073. return {
  1074. x: v1.x - v2.x,
  1075. y: v1.y - v2.y,
  1076. };
  1077. }
  1078. // 两点相加
  1079. pointPlus(v1, v2) {
  1080. return {
  1081. x: v1.x + v2.x,
  1082. y: v1.y + v2.y,
  1083. };
  1084. }
  1085. // 中心点
  1086. lineCenter(v1, v2) {
  1087. const point = this.pointPlus(v1, v2);
  1088. return {
  1089. x: point.x / 2,
  1090. y: point.y / 2,
  1091. };
  1092. }
  1093. // 点放大
  1094. pointScale(v, a) {
  1095. return {
  1096. x: v.x * a,
  1097. y: v.y * a,
  1098. };
  1099. }
  1100. clamp(value, min, max) {
  1101. return Math.max(min, Math.min(max, value));
  1102. }
  1103. lengthSq(v) {
  1104. return v.x * v.x + v.y * v.y;
  1105. }
  1106. // 当前点 下一个点 下下个点
  1107. getCurvesControls(p1, pt, p2, scale = 0.3) {
  1108. const vec1T = this.pointMinus(p1, pt);
  1109. const vecT2 = this.pointMinus(p1, pt);
  1110. const len1 = Math.hypot(vec1T.x, vec1T.y);
  1111. const len2 = Math.hypot(vecT2.x, vecT2.y);
  1112. const v = len1 / len2;
  1113. let delta;
  1114. if (v > 1) {
  1115. delta = this.pointMinus(
  1116. p1,
  1117. this.pointPlus(pt, this.pointScale(this.pointMinus(p2, pt), 1 / v))
  1118. );
  1119. } else {
  1120. delta = this.pointMinus(
  1121. this.pointPlus(pt, this.pointScale(this.pointMinus(p1, pt), v)),
  1122. p2
  1123. );
  1124. }
  1125. delta = this.pointScale(delta, scale);
  1126. const control1 = {
  1127. x: this.pointPlus(pt, delta).x,
  1128. y: this.pointPlus(pt, delta).y,
  1129. };
  1130. const control2 = {
  1131. x: this.pointMinus(pt, delta).x,
  1132. y: this.pointMinus(pt, delta).y,
  1133. };
  1134. return { control1, control2 };
  1135. }
  1136. getCurvesByPoints(points, scale = 0.2) {
  1137. const curves = [];
  1138. let preControl1, preControl2;
  1139. for (let i = 0; i < points.length - 2; i++) {
  1140. const { control1, control2 } = this.getCurvesControls(
  1141. points[i],
  1142. points[i + 1],
  1143. points[i + 2],
  1144. scale
  1145. );
  1146. curves.push({
  1147. start: points[i],
  1148. end: points[i + 1],
  1149. controls: i === 0 ? [control1] : [preControl2, control1],
  1150. });
  1151. preControl1 = control1;
  1152. preControl2 = control2;
  1153. }
  1154. curves.push({
  1155. start: points[points.length - 2],
  1156. controls: [preControl2],
  1157. end: points[points.length - 1],
  1158. });
  1159. return curves;
  1160. }
  1161. /**
  1162. * 已知四个控制点,及曲线中的某一个点的 x/y,反推求 t
  1163. * @param {number} x1 起点 x/y
  1164. * @param {number} x2 控制点1 x/y
  1165. * @param {number} x3 控制点2 x/y
  1166. * @param {number} x4 终点 x/y
  1167. * @param {number} X 曲线中的某个点 x/y
  1168. * @returns {number[]} t[]
  1169. */
  1170. getThreeBezierT(x1, x2, x3, x4, X) {
  1171. const a = -x1 + 3 * x2 - 3 * x3 + x4;
  1172. const b = 3 * x1 - 6 * x2 + 3 * x3;
  1173. const c = -3 * x1 + 3 * x2;
  1174. const d = x1 - X;
  1175. // 盛金公式, 预先需满足, a !== 0
  1176. // 判别式
  1177. const A = Math.pow(b, 2) - 3 * a * c;
  1178. const B = b * c - 9 * a * d;
  1179. const C = Math.pow(c, 2) - 3 * b * d;
  1180. const delta = Math.pow(B, 2) - 4 * A * C;
  1181. let t1 = -100,
  1182. t2 = -100,
  1183. t3 = -100;
  1184. // 3个相同实数根
  1185. if (A === B && A === 0) {
  1186. t1 = -b / (3 * a);
  1187. t2 = -c / b;
  1188. t3 = (-3 * d) / c;
  1189. return [t1, t2, t3];
  1190. }
  1191. // 1个实数根和1对共轭复数根
  1192. if (delta > 0) {
  1193. const v = Math.pow(B, 2) - 4 * A * C;
  1194. const xsv = v < 0 ? -1 : 1;
  1195. const m1 = A * b + (3 * a * (-B + (v * xsv) ** (1 / 2) * xsv)) / 2;
  1196. const m2 = A * b + (3 * a * (-B - (v * xsv) ** (1 / 2) * xsv)) / 2;
  1197. const xs1 = m1 < 0 ? -1 : 1;
  1198. const xs2 = m2 < 0 ? -1 : 1;
  1199. t1 =
  1200. (-b - (m1 * xs1) ** (1 / 3) * xs1 - (m2 * xs2) ** (1 / 3) * xs2) /
  1201. (3 * a);
  1202. // 涉及虚数,可不考虑。i ** 2 = -1
  1203. }
  1204. // 3个实数根
  1205. if (delta === 0) {
  1206. const K = B / A;
  1207. t1 = -b / a + K;
  1208. t2 = t3 = -K / 2;
  1209. }
  1210. // 3个不相等实数根
  1211. if (delta < 0) {
  1212. const xsA = A < 0 ? -1 : 1;
  1213. const T = (2 * A * b - 3 * a * B) / (2 * (A * xsA) ** (3 / 2) * xsA);
  1214. const theta = Math.acos(T);
  1215. if (A > 0 && T < 1 && T > -1) {
  1216. t1 = (-b - 2 * A ** (1 / 2) * Math.cos(theta / 3)) / (3 * a);
  1217. t2 =
  1218. (-b +
  1219. A ** (1 / 2) *
  1220. (Math.cos(theta / 3) + 3 ** (1 / 2) * Math.sin(theta / 3))) /
  1221. (3 * a);
  1222. t3 =
  1223. (-b +
  1224. A ** (1 / 2) *
  1225. (Math.cos(theta / 3) - 3 ** (1 / 2) * Math.sin(theta / 3))) /
  1226. (3 * a);
  1227. }
  1228. }
  1229. return [t1, t2, t3];
  1230. }
  1231. /**
  1232. * @desc 获取三阶贝塞尔曲线的线上坐标
  1233. * B(t) = P0 * (1-t)^3 + 3 * P1 * t * (1-t)^2 + 3 * P2 * t^2 * (1-t) + P3 * t^3, t ∈ [0,1]
  1234. * @param {number} t 当前百分比
  1235. * @param {Array} p1 起点坐标
  1236. * @param {Array} p2 终点坐标
  1237. * @param {Array} cp1 控制点1
  1238. * @param {Array} cp2 控制点2
  1239. */
  1240. getThreeBezierPoint(t, p1, cp1, cp2, p2) {
  1241. const { x: x1, y: y1 } = p1;
  1242. const { x: x2, y: y2 } = p2;
  1243. const { x: cx1, y: cy1 } = cp1;
  1244. const { x: cx2, y: cy2 } = cp2;
  1245. const x =
  1246. x1 * (1 - t) * (1 - t) * (1 - t) +
  1247. 3 * cx1 * t * (1 - t) * (1 - t) +
  1248. 3 * cx2 * t * t * (1 - t) +
  1249. x2 * t * t * t;
  1250. const y =
  1251. y1 * (1 - t) * (1 - t) * (1 - t) +
  1252. 3 * cy1 * t * (1 - t) * (1 - t) +
  1253. 3 * cy2 * t * t * (1 - t) +
  1254. y2 * t * t * t;
  1255. return { x, y };
  1256. }
  1257. getHitInfoForThreeBezier(position, curve, rang = 3) {
  1258. const { x: offsetX, y: offsetY } = position;
  1259. // 用 x 求出对应的 t,用 t 求相应位置的 y,再比较得出的 y 与 offsetY 之间的差值
  1260. const tsx = this.getThreeBezierT(
  1261. curve.start.x,
  1262. curve.controls[0].x,
  1263. curve.controls[1].x,
  1264. curve.end.x,
  1265. offsetX
  1266. );
  1267. for (let x = 0; x < 3; x++) {
  1268. if (tsx[x] <= 1 && tsx[x] >= 0) {
  1269. const point = this.getThreeBezierPoint(
  1270. tsx[x],
  1271. curve.start,
  1272. curve.controls[0],
  1273. curve.controls[1],
  1274. curve.end
  1275. );
  1276. if (Math.abs(point.y - offsetY) < rang) {
  1277. return {
  1278. position: point,
  1279. distance: this.getDistance(point, position),
  1280. };
  1281. }
  1282. }
  1283. }
  1284. // 如果上述没有结果,则用 y 求出对应的 t,再用 t 求出对应的 x,与 offsetX 进行匹配
  1285. const tsy = this.getThreeBezierT(
  1286. curve.start.y,
  1287. curve.controls[0].y,
  1288. curve.controls[1].y,
  1289. curve.end.y,
  1290. offsetY
  1291. );
  1292. for (let y = 0; y < 3; y++) {
  1293. if (tsy[y] <= 1 && tsy[y] >= 0) {
  1294. const point = this.getThreeBezierPoint(
  1295. tsy[y],
  1296. curve.start,
  1297. curve.controls[0],
  1298. curve.controls[1],
  1299. curve.end
  1300. );
  1301. if (Math.abs(point.x - offsetX) < rang) {
  1302. return {
  1303. position: point,
  1304. distance: this.getDistance(point, position),
  1305. };
  1306. }
  1307. }
  1308. }
  1309. }
  1310. // 二次曲线
  1311. getHitInfoForTwoBezier(position, curve) {
  1312. let bezierData = [];
  1313. bezierData.push(curve.start.x);
  1314. bezierData.push(curve.start.y);
  1315. bezierData.push(curve.controls[0].x);
  1316. bezierData.push(curve.controls[0].y);
  1317. bezierData.push(curve.end.x);
  1318. bezierData.push(curve.end.y);
  1319. const { isHit, getInfo } = bezierUtil.measureBezier(...bezierData);
  1320. const { point } = getInfo(position);
  1321. return {
  1322. position: {
  1323. x: point[0],
  1324. y: point[1],
  1325. },
  1326. distance: this.getDistance(position, {
  1327. x: point[0],
  1328. y: point[1],
  1329. }),
  1330. };
  1331. }
  1332. getHitInfoForCurves(pos, curves, roadWidth) {
  1333. let joinInfo;
  1334. for (const curve of curves) {
  1335. const tempJoinInfo =
  1336. curve.controls.length === 2
  1337. ? this.getHitInfoForThreeBezier(pos, curve, roadWidth / 2)
  1338. : this.getHitInfoForTwoBezier(pos, curve);
  1339. if (
  1340. !joinInfo ||
  1341. (tempJoinInfo && tempJoinInfo.distance < joinInfo.distance)
  1342. ) {
  1343. joinInfo = tempJoinInfo;
  1344. }
  1345. }
  1346. return joinInfo;
  1347. }
  1348. getHitInfoForCurve(pos, curve, roadWidth) {
  1349. let joinInfo;
  1350. const tempJoinInfo =
  1351. curve.controls.length === 2
  1352. ? this.getHitInfoForThreeBezier(pos, curve, roadWidth / 2)
  1353. : this.getHitInfoForTwoBezier(pos, curve);
  1354. if (
  1355. !joinInfo ||
  1356. (tempJoinInfo && tempJoinInfo.distance < joinInfo.distance)
  1357. ) {
  1358. joinInfo = tempJoinInfo;
  1359. }
  1360. return joinInfo;
  1361. }
  1362. getIndexForCurvesPoints(position, points) {
  1363. let minDis = null;
  1364. let index = -1;
  1365. for (let i = 0; i < points.length - 1; ++i) {
  1366. const line = this.createLine1(points[i], points[i + 1]);
  1367. const join = this.getJoinLinePoint(position, line);
  1368. const dis = this.getDistance(position, join);
  1369. if (this.isContainForSegment(join, points[i], points[i + 1])) {
  1370. if (minDis == null || minDis > dis) {
  1371. minDis = dis;
  1372. index = i;
  1373. }
  1374. }
  1375. }
  1376. return index;
  1377. }
  1378. // //获取一组点的偏移
  1379. // getOffset(points, leftWidth, rightWidth, dir) {
  1380. // //斜边长度d已知,角度angle已知
  1381. // //对边长度就是y的偏移量 就是 d * sin(angle) ==> d * Math.sin(angle * Math.PI / 180)
  1382. // //邻边长度就是x的偏移量 就是 d * cos(angle) ==> d * Math.cos(angle * Math.PI / 180)
  1383. // let result = {};
  1384. // if (dir == "left" || !dir) {
  1385. // let angle = 90;
  1386. // let d = leftWidth;
  1387. // result.leftEdgePoints = points.map((coords) => {
  1388. // let ox = d * Math.cos((angle * Math.PI) / 180);
  1389. // let oy = d * Math.sin((angle * Math.PI) / 180);
  1390. // return {
  1391. // x: coords.x + ox,
  1392. // y: coords.y + oy,
  1393. // };
  1394. // });
  1395. // }
  1396. // if (dir == "right" || !dir) {
  1397. // let angle = -90;
  1398. // let d = rightWidth;
  1399. // result.rightEdgePoints = points.map((coords) => {
  1400. // let ox = d * Math.cos((angle * Math.PI) / 180);
  1401. // let oy = d * Math.sin((angle * Math.PI) / 180);
  1402. // return {
  1403. // x: coords.x + ox,
  1404. // y: coords.y + oy,
  1405. // };
  1406. // });
  1407. // }
  1408. // return result;
  1409. // }
  1410. getOffset(points, leftWidth, rightWidth, dir) {
  1411. let leftEdgePoints = [];
  1412. let rightEdgePoints = [];
  1413. for (let i = 0; i < points.length - 1; ++i) {
  1414. if (dir == "left" || !dir) {
  1415. let leftEdgePoins1 = this.RectangleVertex(
  1416. points[i],
  1417. points[i + 1],
  1418. leftWidth * 2
  1419. );
  1420. let leftLine1 = mathUtil.createLine1(
  1421. leftEdgePoins1.leftEdgeStart,
  1422. leftEdgePoins1.leftEdgeEnd
  1423. );
  1424. if (i != points.length - 2) {
  1425. let leftEdgePoins2 = this.RectangleVertex(
  1426. points[i + 1],
  1427. points[i + 2],
  1428. leftWidth * 2
  1429. );
  1430. let leftLine2 = mathUtil.createLine1(
  1431. leftEdgePoins2.leftEdgeStart,
  1432. leftEdgePoins2.leftEdgeEnd
  1433. );
  1434. let join = mathUtil.getIntersectionPoint(leftLine1, leftLine2);
  1435. if (join != null) {
  1436. leftEdgePoints[i + 1] = join;
  1437. } else {
  1438. leftEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1439. points[i + 1],
  1440. leftLine1
  1441. );
  1442. }
  1443. } else {
  1444. leftEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1445. points[i + 1],
  1446. leftLine1
  1447. );
  1448. }
  1449. if (!leftEdgePoints[0]) {
  1450. leftEdgePoints[0] = mathUtil.getJoinLinePoint(points[0], leftLine1);
  1451. }
  1452. }
  1453. if (dir == "right" || !dir) {
  1454. let rightEdgePoins1 = this.RectangleVertex(
  1455. points[i],
  1456. points[i + 1],
  1457. rightWidth * 2
  1458. );
  1459. let rightLine1 = mathUtil.createLine1(
  1460. rightEdgePoins1.rightEdgeStart,
  1461. rightEdgePoins1.rightEdgeEnd
  1462. );
  1463. if (i != points.length - 2) {
  1464. let rightEdgePoins2 = this.RectangleVertex(
  1465. points[i + 1],
  1466. points[i + 2],
  1467. rightWidth * 2
  1468. );
  1469. let rightLine2 = mathUtil.createLine1(
  1470. rightEdgePoins2.rightEdgeStart,
  1471. rightEdgePoins2.rightEdgeEnd
  1472. );
  1473. let join = mathUtil.getIntersectionPoint(rightLine1, rightLine2);
  1474. if (join != null) {
  1475. rightEdgePoints[i + 1] = join;
  1476. } else {
  1477. rightEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1478. points[i + 1],
  1479. rightLine1
  1480. );
  1481. }
  1482. } else {
  1483. rightEdgePoints[i + 1] = mathUtil.getJoinLinePoint(
  1484. points[i + 1],
  1485. rightLine1
  1486. );
  1487. }
  1488. if (!rightEdgePoints[0]) {
  1489. rightEdgePoints[0] = mathUtil.getJoinLinePoint(points[0], rightLine1);
  1490. }
  1491. }
  1492. }
  1493. return {
  1494. leftEdgePoints: leftEdgePoints,
  1495. rightEdgePoints: rightEdgePoints,
  1496. };
  1497. }
  1498. twoOrderBezier(t, p1, cp, p2) {
  1499. //参数分别是t,起始点,控制点和终点
  1500. var x1 = p1.x;
  1501. var y1 = p1.y;
  1502. var cx = cp.x;
  1503. var cy = cp.y;
  1504. var x2 = p2.x;
  1505. var y2 = p2.y;
  1506. // var [x1, y1] = p1,
  1507. // [cx, cy] = cp,
  1508. // [x2, y2] = p2;
  1509. var x = (1 - t) * (1 - t) * x1 + 2 * t * (1 - t) * cx + t * t * x2,
  1510. y = (1 - t) * (1 - t) * y1 + 2 * t * (1 - t) * cy + t * t * y2;
  1511. return {
  1512. x: x,
  1513. y: y,
  1514. };
  1515. }
  1516. //t是0.5,求cp。p是曲线上的点
  1517. twoOrderBezier2(t, p1, p, p2) {
  1518. var x1 = p1.x;
  1519. var y1 = p1.y;
  1520. var x2 = p2.x;
  1521. var y2 = p2.y;
  1522. let cx = (p.x - t * t * x2 - (1 - t) * (1 - t) * x1) / (2 * t * (1 - t));
  1523. let cy = (p.y - t * t * y2 - (1 - t) * (1 - t) * y1) / (2 * t * (1 - t));
  1524. return {
  1525. x: cx,
  1526. y: cy,
  1527. };
  1528. }
  1529. rgb() {
  1530. //rgb颜色随机
  1531. const r = Math.floor(Math.random() * 256);
  1532. const g = Math.floor(Math.random() * 256);
  1533. const b = Math.floor(Math.random() * 256);
  1534. return `rgb(${r},${g},${b})`;
  1535. }
  1536. }
  1537. const mathUtil = new MathUtil();
  1538. export { mathUtil };