MathUtil.js 44 KB

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