MathUtil.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. import Constant from "../Constant";
  2. export default class MathUtil {
  3. constructor() {}
  4. getFixed(num, decimal) {
  5. if (!decimal) {
  6. decimal = 5;
  7. }
  8. // return Math.floor(num * 10000) / 10000;
  9. return parseFloat(num.toFixed(decimal));
  10. }
  11. // 求两个点的距离
  12. getDistance(p1, p2) {
  13. const x1 = p1.x;
  14. const y1 = p1.y;
  15. const x2 = p2.x;
  16. const y2 = p2.y;
  17. const num = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
  18. return this.getFixed(num);
  19. }
  20. createLine1(point1, point2) {
  21. if (point1.x == point2.x && point1.y == point2.y) {
  22. return null;
  23. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  24. return { x: point1.x };
  25. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  26. return { y: point1.y };
  27. }
  28. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  29. const parameterb =
  30. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  31. if (this.getFixed(parametera) == 0) {
  32. return { y: this.getFixed(parameterb) };
  33. }
  34. const parameter = {
  35. a: this.getFixed(parametera),
  36. b: this.getFixed(parameterb),
  37. };
  38. return parameter;
  39. }
  40. createLine2(point, angle) {
  41. if (angle == 90 || angle == 270) {
  42. return { x: point.x };
  43. }
  44. let a = Math.tan((angle / 180) * Math.PI);
  45. let b = point.y - a * point.x;
  46. if (a != 0) {
  47. return { a: a, b: b };
  48. } else {
  49. return { y: point.y };
  50. }
  51. }
  52. // 与lineA平行并且point在线上
  53. createLine3(lineA, point) {
  54. const parameter = {};
  55. if (typeof lineA.a === "undefined") {
  56. if (typeof lineA.x !== "undefined") {
  57. parameter.x = point.x;
  58. } else if (typeof lineA.y !== "undefined") {
  59. parameter.y = point.y;
  60. }
  61. } else {
  62. parameter.a = lineA.a;
  63. parameter.b = point.y - point.x * lineA.a;
  64. }
  65. return parameter;
  66. }
  67. create2AngleLine(point, angle, driftAngle) {
  68. let line1 = this.createLine2(point, angle - driftAngle / 2);
  69. let line2 = this.createLine2(point, angle + driftAngle / 2);
  70. return { line1: line1, line2: line2 };
  71. }
  72. distanceForPoints(point1, point2) {
  73. return Math.sqrt(
  74. Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2)
  75. );
  76. }
  77. //与line平行且两条线直接的距离是distance的两条线
  78. getParallelLineForDistance(line, distance) {
  79. let line1 = {};
  80. let line2 = {};
  81. if (!line.hasOwnProperty("a")) {
  82. if (line.hasOwnProperty("x")) {
  83. let x = line.x;
  84. line1.x = x + distance;
  85. line2.x = x - distance;
  86. } else if (line.hasOwnProperty("y")) {
  87. let y = line.y;
  88. line1.y = y + distance;
  89. line2.y = y - distance;
  90. }
  91. } else {
  92. line1.a = line.a;
  93. line1.b = line.b;
  94. line2.a = line.a;
  95. line2.b = line.b;
  96. let angle = Math.atan(line.a);
  97. let db = Math.abs(distance / Math.cos(angle));
  98. let b = line.b;
  99. line1.b = b + db;
  100. line2.b = b - db;
  101. }
  102. return { line1: line1, line2: line2 };
  103. }
  104. //获取扇形的两个端点
  105. getEndpoint(point, angle, sectorAngle) {
  106. const distance = 15;
  107. //line1是减,line2是加
  108. let lines1 = this.create2AngleLine(point, angle, sectorAngle);
  109. let line = this.createLine2(point, angle);
  110. line = this.getLineForPoint(line, point);
  111. let lines2 = this.getParallelLineForDistance(line, distance);
  112. let point1 = this.getIntersectionPoint(lines1.line1, lines2.line1);
  113. let point2 = this.getIntersectionPoint(lines1.line1, lines2.line2);
  114. let point3 = this.getIntersectionPoint(lines1.line2, lines2.line1);
  115. let point4 = this.getIntersectionPoint(lines1.line2, lines2.line2);
  116. let angle1 = this.Angle(point, point1, { x: point.x + 1, y: point.y });
  117. let angle2 = this.Angle(point, point2, { x: point.x + 1, y: point.y });
  118. let angle3 = this.Angle(point, point3, { x: point.x + 1, y: point.y });
  119. let angle4 = this.Angle(point, point4, { x: point.x + 1, y: point.y });
  120. if (angle > 180) {
  121. angle = 360 - angle;
  122. }
  123. if (
  124. Math.abs((angle1 + angle3) / 2 - angle) <
  125. Math.abs((angle2 + angle4) / 2 - angle)
  126. ) {
  127. return { p1: point1, p2: point3 };
  128. } else {
  129. return { p1: point2, p2: point4 };
  130. }
  131. }
  132. // true表示逆时针,false表示顺时针
  133. isClockwise(vertices) {
  134. let area = 0;
  135. for (let i = 0; i < vertices.length; i++) {
  136. const j = (i + 1) % vertices.length;
  137. area += vertices[i].x * vertices[j].y;
  138. area -= vertices[j].x * vertices[i].y;
  139. }
  140. const sub = area / 2;
  141. if (sub > 0) {
  142. // 逆时针
  143. return true;
  144. } else {
  145. // 顺时针
  146. return false;
  147. }
  148. }
  149. reverse(points) {
  150. const _points = [];
  151. for (let i = points.length - 1; i > -1; --i) {
  152. _points.push(points[i]);
  153. }
  154. return _points;
  155. }
  156. //两条线的交点
  157. getIntersectionPoint(parameter1, parameter2) {
  158. if (this.isParallel(parameter1, parameter2)) {
  159. return null;
  160. }
  161. if (
  162. typeof parameter1.a == "undefined" &&
  163. typeof parameter2.a != "undefined"
  164. ) {
  165. if (parameter1.x) {
  166. return {
  167. x: parameter1.x,
  168. y: parameter2.a * parameter1.x + parameter2.b,
  169. };
  170. } else if (parameter1.y) {
  171. return {
  172. x: (parameter1.y - parameter2.b) / parameter2.a,
  173. y: parameter1.y,
  174. };
  175. }
  176. } else if (
  177. typeof parameter2.a == "undefined" &&
  178. typeof parameter1.a != "undefined"
  179. ) {
  180. if (parameter2.x) {
  181. return {
  182. x: parameter2.x,
  183. y: parameter1.a * parameter2.x + parameter1.b,
  184. };
  185. } else if (parameter2.y) {
  186. return {
  187. x: (parameter2.y - parameter1.b) / parameter1.a,
  188. y: parameter2.y,
  189. };
  190. }
  191. } else if (
  192. typeof parameter2.a == "undefined" &&
  193. typeof parameter1.a == "undefined"
  194. ) {
  195. if (parameter1.hasOwnProperty("x") && parameter2.hasOwnProperty("y")) {
  196. return { x: parameter1.x, y: parameter2.y };
  197. } else if (
  198. parameter1.hasOwnProperty("y") &&
  199. parameter2.hasOwnProperty("x")
  200. ) {
  201. return { x: parameter2.x, y: parameter1.y };
  202. } else {
  203. return null;
  204. }
  205. }
  206. if (parameter1.a == parameter2.a) {
  207. return null;
  208. }
  209. let joinpointx =
  210. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  211. let joinpointy =
  212. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  213. (parameter1.a - parameter2.a);
  214. let point = { x: joinpointx, y: joinpointy };
  215. return point;
  216. }
  217. // 直线的交点
  218. getIntersectionPoint2(a, b, c, d) {
  219. /** 1 解线性方程组, 求线段交点. **/
  220. // 如果分母为0 则平行或共线, 不相交
  221. const denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x) * (c.y - d.y);
  222. if (denominator == 0) {
  223. return null;
  224. }
  225. // 线段所在直线的交点坐标 (x , y)
  226. const x =
  227. ((b.x - a.x) * (d.x - c.x) * (c.y - a.y) +
  228. (b.y - a.y) * (d.x - c.x) * a.x -
  229. (d.y - c.y) * (b.x - a.x) * c.x) /
  230. denominator;
  231. const y =
  232. -(
  233. (b.y - a.y) * (d.y - c.y) * (c.x - a.x) +
  234. (b.x - a.x) * (d.y - c.y) * a.y -
  235. (d.x - c.x) * (b.y - a.y) * c.y
  236. ) / denominator;
  237. return { x: x, y: y };
  238. }
  239. //两条线段交点
  240. getIntersectionPoint3(a, b, c, d) {
  241. const join = this.getIntersectionPoint2(a, b, c, d);
  242. if (join) {
  243. const x = join.x;
  244. const y = join.y; // 交点在线段1上 且交点也在线段2上
  245. /** 2 判断交点是否在两条线段上 **/
  246. if (
  247. (x - a.x) * (x - b.x) <= 0.001 &&
  248. (y - a.y) * (y - b.y) <= 0.001 &&
  249. (x - c.x) * (x - d.x) <= 0.001 &&
  250. (y - c.y) * (y - d.y) <= 0.001
  251. ) {
  252. // 返回交点p
  253. return {
  254. x: x,
  255. y: y,
  256. };
  257. }
  258. return null;
  259. }
  260. return null;
  261. }
  262. // 线段和直线是否相交
  263. getIntersectionPoint4(point1, point2, line) {
  264. const line1 = this.createLine1(point1, point2);
  265. const join = this.getIntersectionPoint(line1, line);
  266. if (join == null) {
  267. return null;
  268. }
  269. if (this.PointInSegment(join, point1, point2)) {
  270. return join; // 相交
  271. } else {
  272. return null;
  273. }
  274. }
  275. //返回true表示平行
  276. isParallel(line1, line2) {
  277. if (typeof line1.a == "undefined" && typeof line2.a == "undefined") {
  278. if (line1.hasOwnProperty("x") && line2.hasOwnProperty("x")) {
  279. return true;
  280. } else if (line1.hasOwnProperty("y") && line2.hasOwnProperty("y")) {
  281. return true;
  282. } else {
  283. return false;
  284. }
  285. } else if (typeof line1.a == "undefined" || typeof line2.a == "undefined") {
  286. return false;
  287. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. }
  293. //两条相交的线段的夹角,永远小于180度
  294. Angle(o, s, e) {
  295. let cosfi = 0,
  296. fi = 0,
  297. norm = 0;
  298. let dsx = s.x - o.x;
  299. let dsy = s.y - o.y;
  300. let dex = e.x - o.x;
  301. let dey = e.y - o.y;
  302. cosfi = dsx * dex + dsy * dey;
  303. norm = (dsx * dsx + dsy * dsy) * (dex * dex + dey * dey);
  304. cosfi /= Math.sqrt(norm);
  305. if (cosfi >= 1.0) return 0;
  306. //if (cosfi <= -1.0) return Math.PI;
  307. if (cosfi <= -1.0) return 180;
  308. fi = Math.acos(cosfi);
  309. if ((180 * fi) / Math.PI < 180) {
  310. //return 180 * fi / Math.PI;
  311. return (fi * 180) / Math.PI;
  312. } else {
  313. //return 360 - 180 * fi / Math.PI;
  314. return ((2 * Math.PI - fi) * 180) / Math.PI;
  315. }
  316. }
  317. //经过point且与line垂直的线
  318. getLineForPoint(line, point) {
  319. let parameter = {};
  320. if (line.a == 0 || typeof line.a == "undefined") {
  321. if (line.hasOwnProperty("x")) {
  322. parameter.x = line.x;
  323. parameter.y = point.y;
  324. } else if (line.hasOwnProperty("y")) {
  325. parameter.x = point.x;
  326. parameter.y = line.y;
  327. }
  328. } else {
  329. parameter.a = -1 / line.a;
  330. parameter.b = point.y - point.x * parameter.a;
  331. }
  332. return parameter;
  333. }
  334. // 经过point且与line垂直的直线,该直线与line的交点
  335. getJoinLinePoint(point, line) {
  336. const verticalLine = this.getVerticalLine(line, point);
  337. const join = this.getIntersectionPoint(line, verticalLine);
  338. return join;
  339. }
  340. // 点到直线的距离
  341. getDisForPoinLine(point, line) {
  342. const join = this.getJoinLinePoint(point, line);
  343. return this.getDistance(point, join);
  344. }
  345. // 垂直线
  346. getVerticalLine(line, point) {
  347. if (typeof line.a === "undefined") {
  348. if (line.hasOwnProperty("x")) {
  349. return { y: point.y };
  350. } else if (line.hasOwnProperty("y")) {
  351. return { x: point.x };
  352. } else {
  353. return null;
  354. }
  355. } else if (line.a == 0) {
  356. return { x: point.x };
  357. } else {
  358. const tl = {};
  359. tl.a = -1 / line.a;
  360. const result = this.createLine3(tl, point);
  361. return result;
  362. }
  363. }
  364. //point在直线上,只是不确定是否在线段上
  365. //方法:point到startPoint和endPoint的距离之和与startPoint和endPoint之间的距离对比
  366. isContainForSegment(point, startPoint, endPoint, minDis) {
  367. if (!minDis) {
  368. minDis = Constant.minLen;
  369. }
  370. let dis1 =
  371. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  372. let dis2 = this.getDistance(startPoint, endPoint);
  373. if (Math.abs(dis1 - dis2) < minDis) {
  374. return true;
  375. } else {
  376. return false;
  377. }
  378. }
  379. /*
  380. //minDis
  381. isPointInPoly(point, points, minDis) {
  382. if (!minDis) {
  383. minDis = Constant.minRealDis
  384. }
  385. const x = point.x
  386. const y = point.y
  387. let inside = false
  388. // 是否在顶点附近
  389. for (let i = 0; i < points.length; ++i) {
  390. let distance = this.getDistance(point, points[i])
  391. if (distance < minDis) {
  392. return true
  393. }
  394. }
  395. // 是否在边沿
  396. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  397. let pt1 = points[i]
  398. let pt2 = points[j]
  399. const flag = this.isContainForSegment(point, pt1, pt2, minDis)
  400. if (flag) {
  401. return true
  402. }
  403. }
  404. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  405. let pt1 = points[i]
  406. let pt2 = points[j]
  407. const xi = pt1.x
  408. const yi = pt1.y
  409. const xj = pt2.x
  410. const yj = pt2.y
  411. const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
  412. if (intersect) inside = !inside
  413. }
  414. return inside
  415. }
  416. */
  417. isPointInPoly(point, points) {
  418. const x = point.x;
  419. const y = point.y;
  420. let inside = false;
  421. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  422. let pt1 = points[i];
  423. let pt2 = points[j];
  424. const xi = pt1.x;
  425. const yi = pt1.y;
  426. const xj = pt2.x;
  427. const yj = pt2.y;
  428. const intersect =
  429. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  430. if (intersect) inside = !inside;
  431. }
  432. return inside;
  433. }
  434. // 点到线段的距离
  435. // 在minDistance范围内,会吸附到point1/point2上
  436. // 返回值:type是1表示吸附在point1,是2表示吸附在point2,是0表示在线段point1-point2上;
  437. getDisForPoinSegment(point, point1, point2, minDistance) {
  438. const line = this.createLine1(point1, point2);
  439. const join = this.getJoinLinePoint(point, line);
  440. const dis = this.getDistance(point1, point2);
  441. const dis1 = this.getDistance(join, point1);
  442. const dis2 = this.getDistance(join, point2);
  443. if (
  444. this.getDistance(join, point1) > dis ||
  445. this.getDistance(join, point2) > dis
  446. ) {
  447. // 在线段外
  448. if (dis1 < dis2 && dis1 < minDistance) {
  449. return { type: 1, join: point1 };
  450. } else if (dis2 < dis1 && dis2 < minDistance) {
  451. return { type: 2, join: point2 };
  452. } else {
  453. return null;
  454. }
  455. } else {
  456. if (dis1 < minDistance) {
  457. return { type: 1, join: point1 };
  458. } else if (dis2 < minDistance) {
  459. return { type: 2, join: point2 };
  460. } else if (this.getDistance(point, join) < minDistance) {
  461. return { type: 0, join: join };
  462. }
  463. }
  464. }
  465. PointInSegment(Q, pi, pj, minDis) {
  466. if (
  467. this.getDistance(Q, pi) < Constant.minAdsorbPix ||
  468. this.getDistance(Q, pj) < Constant.minAdsorbPix
  469. ) {
  470. return true;
  471. }
  472. if (!minDis) {
  473. minDis = 0.1;
  474. }
  475. minDis = minDis / 2;
  476. const offset1 = (Q.x - pi.x) * (pj.y - pi.y) - (pj.x - pi.x) * (Q.y - pi.y);
  477. const offset2 = Math.min(pi.x, pj.x) - Q.x;
  478. const offset3 = Q.x - Math.max(pi.x, pj.x);
  479. const offset4 = Math.min(pi.y, pj.y) - Q.y;
  480. const offset5 = Q.y - Math.max(pi.y, pj.y);
  481. if (
  482. Math.abs(offset1) < minDis &&
  483. (offset2 <= 0 || Math.abs(offset2) < minDis) &&
  484. (offset3 <= 0 || Math.abs(offset3) < minDis) &&
  485. (offset4 <= 0 || Math.abs(offset4) < minDis) &&
  486. (offset5 <= 0 || Math.abs(offset5) < minDis)
  487. ) {
  488. return true;
  489. } else {
  490. return false;
  491. }
  492. }
  493. //点p是否在线段AB上
  494. isPointOnSegment(p, A, B) {
  495. // 计算向量 AP 和 BP
  496. const AP = {
  497. x: p.x - A.x,
  498. y: p.y - A.y,
  499. };
  500. const BP = {
  501. x: p.x - B.x,
  502. y: p.y - B.y,
  503. };
  504. // 计算向量 AB 的长度和方向
  505. const AB = {
  506. x: B.x - A.x,
  507. y: B.y - A.y,
  508. };
  509. const AB_length = this.getDistance(A, B);
  510. const AB_direction = {
  511. x: AB.x / AB_length,
  512. y: AB.y / AB_length,
  513. };
  514. // 检查 AP 和 BP 的方向是否与 AB 相同,并检查它们的长度是否小于等于 AB 的长度
  515. const dot_product_AP = AP.x * AB_direction.x + AP.y * AB_direction.y;
  516. const dot_product_BP = BP.x * AB_direction.x + BP.y * AB_direction.y;
  517. //return dot_product_AP >= 0 && dot_product_BP <= 0 && Math.abs(AP.x * BP.y - AP.y * BP.x) <= AB_length * Number.EPSILON;
  518. return (
  519. dot_product_AP >= 0 &&
  520. dot_product_BP <= 0 &&
  521. Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.01
  522. );
  523. }
  524. clonePoint(p1, p2) {
  525. p1.x = p2.x;
  526. p1.y = p2.y;
  527. }
  528. equalPoint(p1, p2) {
  529. if (p1.x == p2.x && p1.y == p2.y) {
  530. return true;
  531. } else {
  532. return false;
  533. }
  534. }
  535. crossTwoLines(point1, point2, point3, point4, dis) {
  536. if (typeof dis == "undefined") {
  537. dis = Constant.minAdsorbPix;
  538. }
  539. const join = this.getIntersectionPoint2(point1, point2, point3, point4);
  540. if (join != null) {
  541. if (
  542. this.getDistance(point1, join) > dis &&
  543. this.getDistance(point2, join) > dis &&
  544. this.getDistance(point3, join) > dis &&
  545. this.getDistance(point4, join) > dis
  546. ) {
  547. if (
  548. this.getDistance(point1, join) < this.getDistance(point1, point2) &&
  549. this.getDistance(point2, join) < this.getDistance(point1, point2) &&
  550. this.getDistance(point3, join) < this.getDistance(point3, point4) &&
  551. this.getDistance(point4, join) < this.getDistance(point3, point4)
  552. ) {
  553. return true;
  554. } else {
  555. return false;
  556. }
  557. }
  558. } else {
  559. if (
  560. this.PointInSegment(point1, point3, point4) ||
  561. this.PointInSegment(point2, point3, point4)
  562. ) {
  563. return true;
  564. }
  565. }
  566. return false;
  567. }
  568. getDisPointsLine(line, point, distance1, distance2) {
  569. const newpoint1 = {};
  570. const newpoint2 = {};
  571. const result = {};
  572. if (line.hasOwnProperty("x")) {
  573. newpoint1.x = line.x;
  574. newpoint1.y = point.y - distance1;
  575. newpoint2.x = line.x;
  576. newpoint2.y = point.y + distance2;
  577. } else if (line.hasOwnProperty("y")) {
  578. newpoint1.y = line.y;
  579. newpoint1.x = point.x - distance1;
  580. newpoint2.y = line.y;
  581. newpoint2.x = point.x + distance2;
  582. } else {
  583. const a = Math.atan(line.a);
  584. const t_line = { a: -1 / line.a };
  585. const line_ab2 = this.createLine3(t_line, point);
  586. const join = this.getIntersectionPoint(line, line_ab2);
  587. newpoint1.x = join.x - distance1 * Math.cos(a);
  588. newpoint1.y = join.y - distance1 * Math.sin(a);
  589. newpoint2.x = join.x + distance2 * Math.cos(a);
  590. newpoint2.y = join.y + distance2 * Math.sin(a);
  591. }
  592. result.newpoint1 = newpoint1;
  593. result.newpoint2 = newpoint2;
  594. return result;
  595. }
  596. getBoundingBox(points) {
  597. let minX = points[0].x;
  598. let maxX = points[0].x;
  599. let minY = points[0].y;
  600. let maxY = points[0].y;
  601. for (let i = 1; i < points.length; ++i) {
  602. const point = points[i];
  603. if (minX > point.x) {
  604. minX = point.x;
  605. }
  606. if (minY > point.y) {
  607. minY = point.y;
  608. }
  609. if (maxX < point.x) {
  610. maxX = point.x;
  611. }
  612. if (maxY < point.y) {
  613. maxY = point.y;
  614. }
  615. }
  616. const box = {};
  617. box.minX = minX;
  618. box.minY = minY;
  619. box.maxX = maxX;
  620. box.maxY = maxY;
  621. return box;
  622. }
  623. getBoundingBox2(points) {
  624. let minX = null;
  625. let maxX = null;
  626. let minY = null;
  627. let maxY = null;
  628. for (let key in points) {
  629. const point = points[key];
  630. if (minX == null || minX > point.x) {
  631. minX = point.x;
  632. }
  633. if (minY == null || minY > point.y) {
  634. minY = point.y;
  635. }
  636. if (maxX == null || maxX < point.x) {
  637. maxX = point.x;
  638. }
  639. if (maxY == null || maxY < point.y) {
  640. maxY = point.y;
  641. }
  642. }
  643. const box = {};
  644. box.minX = minX;
  645. box.minY = minY;
  646. box.maxX = maxX;
  647. box.maxY = maxY;
  648. return box;
  649. }
  650. ComputePolygonArea(points) {
  651. const point_num = points.length;
  652. if (point_num < 3) {
  653. return 0;
  654. }
  655. let s = points[0].y * (points[point_num - 1].x - points[1].x);
  656. for (let i = 1; i < point_num; ++i)
  657. s += points[i].y * (points[i - 1].x - points[(i + 1) % point_num].x);
  658. return Math.abs(s / 2.0);
  659. }
  660. // 获取多边形重心
  661. getPolygonCore(points) {
  662. function Area(p0, p1, p2) {
  663. let area = 0.0;
  664. area =
  665. p0.x * p1.y +
  666. p1.x * p2.y +
  667. p2.x * p0.y -
  668. p1.x * p0.y -
  669. p2.x * p1.y -
  670. p0.x * p2.y;
  671. return area / 2;
  672. }
  673. let sum_x = 0;
  674. let sum_y = 0;
  675. let sum_area = 0;
  676. let p1 = points[1];
  677. for (let i = 2; i < points.length; i++) {
  678. const p2 = points[i];
  679. const area = Area(points[0], p1, p2);
  680. sum_area += area;
  681. sum_x += (points[0].x + p1.x + p2.x) * area;
  682. sum_y += (points[0].y + p1.y + p2.y) * area;
  683. p1 = p2;
  684. }
  685. const xx = sum_x / sum_area / 3;
  686. const yy = sum_y / sum_area / 3;
  687. return {
  688. x: xx,
  689. y: yy,
  690. };
  691. }
  692. // points1是否在points2里
  693. isPolyInPoly(points1, points2, minDis) {
  694. for (let i = 0; i < points1.length; ++i) {
  695. let flag = false;
  696. for (let j = 0; j < points2.length; ++j) {
  697. if (this.equalPoint(points1[i], points2[j])) {
  698. flag = true;
  699. break;
  700. }
  701. }
  702. if (!flag) {
  703. if (!this.isPointInPoly(points1[i], points2, minDis)) {
  704. return false;
  705. }
  706. } else {
  707. const nextIndex = i == points1.length - 1 ? 0 : i + 1;
  708. const mid = {
  709. x: (points1[i].x + points1[nextIndex].x) / 2,
  710. y: (points1[i].y + points1[nextIndex].y) / 2,
  711. };
  712. if (!this.isPointInPoly(mid, points2, minDis)) {
  713. return false;
  714. }
  715. }
  716. }
  717. return true;
  718. }
  719. dotPoints(pt1, pt2, point1, point2) {
  720. let vt1 = {};
  721. let vt2 = {};
  722. vt1.start = {};
  723. vt1.end = {};
  724. vt1.start.x = 0;
  725. vt1.start.y = 0;
  726. vt1.end.x = pt2.x - pt1.x;
  727. vt1.end.y = pt2.y - pt1.y;
  728. vt2.start = {};
  729. vt2.end = {};
  730. vt2.start.x = 0;
  731. vt2.start.y = 0;
  732. vt2.end.x = point2.x - point1.x;
  733. vt2.end.y = point2.y - point1.y;
  734. let result = vt1.end.x * vt2.end.x + vt1.end.y * vt2.end.y;
  735. return result;
  736. }
  737. //start是起点,target是朝着目标移动,distance是移动的距离
  738. translate(start, target, point, distance) {
  739. let dx = target.x - start.x;
  740. let dy = target.y - start.y;
  741. let dis = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  742. let result = {
  743. x: point.x + (dx * distance) / dis,
  744. y: point.y + (dy * distance) / dis,
  745. };
  746. return result;
  747. }
  748. //射线与线段相交
  749. // intersection(rayStart, rayEnd, segmentStart, segmentEnd) {
  750. // // 计算射线和线段的方向向量
  751. // const rayDirection = {
  752. // x:rayEnd.x - rayStart.x,
  753. // y:rayEnd.y - rayStart.y,
  754. // };
  755. // const segmentDirection = {
  756. // x:segmentEnd.x - segmentStart.x,
  757. // y:segmentEnd.y - segmentStart.y,
  758. // };
  759. // // 计算射线和线段的起点之间的向量
  760. // const startPointVector = {
  761. // x:rayStart.x - segmentStart.x,
  762. // y:rayStart.y - segmentStart.y,
  763. // };
  764. // // 计算射线和线段的叉积
  765. // const crossProduct = rayDirection.x * segmentDirection.y - rayDirection.y * segmentDirection.x;
  766. // // 如果叉积为0,则表示射线和线段平行
  767. // if (crossProduct === 0) {
  768. // return null;
  769. // }
  770. // // 计算线段起点到射线的交点的向量
  771. // const t = (startPointVector.x * segmentDirection.y - startPointVector.y * segmentDirection.x) / crossProduct;
  772. // // 如果t的值小于0,则交点在射线的起点之后
  773. // if (t < 0) {
  774. // return null;
  775. // }
  776. // // 计算交点的坐标
  777. // const intersectionX = rayStart.x + t * rayDirection.x;
  778. // const intersectionY = rayStart.y + t * rayDirection.y;
  779. // // 如果交点在线段的范围内,则返回交点坐标
  780. // if ((intersectionX >= Math.min(segmentStart.x, segmentEnd.x)) &&
  781. // (intersectionX <= Math.max(segmentStart.x, segmentEnd.x)) &&
  782. // (intersectionY >= Math.min(segmentStart.y, segmentEnd.y)) &&
  783. // (intersectionY <= Math.max(segmentStart.y, segmentEnd.y))) {
  784. // //return [intersectionX, intersectionY];
  785. // return {
  786. // x:intersectionX,
  787. // y:intersectionY,
  788. // };
  789. // }
  790. // // 否则返回null
  791. // return null;
  792. // }
  793. // raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  794. // // 计算射线和线段的交点
  795. // const x1 = rayOrigin.x
  796. // const y1 = rayOrigin.y
  797. // const x2 = segmentStart.x
  798. // const y2 = segmentStart.y
  799. // const dx1 = rayDirection.x
  800. // const dy1 = rayDirection.y
  801. // const dx2 = segmentEnd.x - x2
  802. // const dy2 = segmentEnd.y - y2
  803. // const crossProduct = dx1 * dy2 - dx2 * dy1
  804. // if (Math.abs(crossProduct) < 1e-8) {
  805. // // 射线和线段平行或共线
  806. // return null
  807. // }
  808. // const t1 = (dx2 * (y1 - y2) - dy2 * (x1 - x2)) / crossProduct
  809. // const t2 = (dx1 * (y1 - y2) - dy1 * (x1 - x2)) / crossProduct
  810. // if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
  811. // // 有交点,计算交点坐标
  812. // const intersectionX = x1 + t1 * dx1
  813. // const intersectionY = y1 + t1 * dy1
  814. // return {
  815. // x: intersectionX,
  816. // y: intersectionY,
  817. // }
  818. // } else {
  819. // // 没有交点
  820. // return null
  821. // }
  822. // }
  823. raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  824. const end = {
  825. x: rayOrigin.x + rayDirection.x,
  826. y: rayOrigin.y - rayDirection.z,
  827. };
  828. const line = this.createLine1(rayOrigin, end);
  829. const join = this.getIntersectionPoint4(segmentStart, segmentEnd, line);
  830. if (join == null) {
  831. return null;
  832. } else {
  833. const dis = this.getDistance(end, join);
  834. const dis1 = this.getDistance(rayOrigin, join);
  835. const dis2 = this.getDistance(rayOrigin, end);
  836. if (dis - (dis1 + dis2) + 0.01 > 0) {
  837. return null;
  838. } else {
  839. return join;
  840. }
  841. }
  842. }
  843. RectangleVertex(startPoint, endPoint, width) {
  844. let line = this.createLine1(startPoint, endPoint);
  845. let lines = this.getParallelLineForDistance(line, width / 2);
  846. let leftEdgeStart, rightEdgeStart, rightEdgeEnd, leftEdgeEnd;
  847. let point = null;
  848. let points = [];
  849. //先计算start部分
  850. point = startPoint;
  851. points.push(endPoint);
  852. points.push(startPoint);
  853. let point1 = this.getJoinLinePoint(point, lines.line1);
  854. let point2 = this.getJoinLinePoint(point, lines.line2);
  855. points[2] = point1;
  856. if (this.isClockwise(points)) {
  857. rightEdgeStart = point1;
  858. leftEdgeStart = point2;
  859. } else {
  860. rightEdgeStart = point2;
  861. leftEdgeStart = point1;
  862. }
  863. //再计算end部分
  864. points = [];
  865. point = endPoint;
  866. points.push(startPoint);
  867. points.push(endPoint);
  868. point1 = this.getJoinLinePoint(point, lines.line1);
  869. point2 = this.getJoinLinePoint(point, lines.line2);
  870. points[2] = point1;
  871. if (this.isClockwise(points)) {
  872. rightEdgeEnd = point2;
  873. leftEdgeEnd = point1;
  874. } else {
  875. rightEdgeEnd = point1;
  876. leftEdgeEnd = point2;
  877. }
  878. return {
  879. leftEdgeStart: leftEdgeStart,
  880. rightEdgeStart: rightEdgeStart,
  881. rightEdgeEnd: rightEdgeEnd,
  882. leftEdgeEnd: leftEdgeEnd,
  883. };
  884. }
  885. //start到end的射线中取一点point,start-end和end-point的距离相同
  886. getPositionForExtendedLine(start, end) {
  887. const dx = end.x - start.x;
  888. const dy = end.y - start.y;
  889. const point = {
  890. x: end.x + dx,
  891. y: end.y + dy,
  892. };
  893. return point;
  894. }
  895. isOnRay(start, dir, position) {
  896. const v1 = { x: dir.x - start.x, y: dir.y - start.y };
  897. const v2 = { x: position.x - start.x, y: position.y - start.y };
  898. return v1.x * v2.y - v1.y * v2.x;
  899. }
  900. //向量是否同样的方向
  901. isSameDirForVector(point1, point2, p1, p2) {
  902. const v1 = {
  903. x: point2.x - point1.x,
  904. y: point2.y - point1.y,
  905. };
  906. const v2 = {
  907. x: p2.x - p1.x,
  908. y: p2.y - p1.y,
  909. };
  910. const value = this.dot(v1, v2);
  911. if (value > 0) {
  912. return true;
  913. }
  914. {
  915. return false;
  916. }
  917. }
  918. angleTo(v1, v2) {
  919. const denominator = Math.sqrt(this.lengthSq(v1) * this.lengthSq(v2));
  920. if (denominator === 0) return 90;
  921. const theta = this.dot(v1, v2) / denominator;
  922. //return Math.acos(this.clamp(theta, -1, 1));
  923. return (Math.acos(this.clamp(theta, -1, 1)) / Math.PI) * 180;
  924. }
  925. //点乘
  926. dot(v1, v2) {
  927. return v1.x * v2.x + v1.y * v2.y;
  928. }
  929. //叉乘
  930. cross(v1, v2) {
  931. return v1.x * v2.y - v1.y * v2.x;
  932. }
  933. clamp(value, min, max) {
  934. return Math.max(min, Math.min(max, value));
  935. }
  936. lengthSq(v) {
  937. return v.x * v.x + v.y * v.y;
  938. }
  939. getCurvesByPoints(points, scale = 0.2) {
  940. const curves = [];
  941. for (let i = 1; i < points.length; i++) {
  942. const prev1Index = i - 1
  943. const prev2Index = i === 1 ? prev1Index : i - 2;
  944. const nextIndex = i === points.length - 1 ? points.length - 1 : i + 1
  945. const { x: nowX, y: nowY } = points[i];
  946. const { x: last1X, y: last1Y } = points[prev1Index]
  947. const { x: last2X, y: last2Y } = points[prev2Index]
  948. const { x: nextX, y: nextY } = points[nextIndex]
  949. const cAx = last1X + (nowX - last2X) * scale,
  950. cAy = last1Y + (nowY - last2Y) * scale,
  951. cBx = nowX - (nextX - last1X) * scale,
  952. cBy = nowY - (nextY - last1Y) * scale
  953. curves.push({
  954. start: i === 1 ? {x: points[0].x, y: points[0].y} : { x: cAx, y: cAy },
  955. control: { x: nowX, y: nowY },
  956. end: { x: cBx, y: cBy },
  957. })
  958. }
  959. return curves
  960. }
  961. }
  962. const mathUtil = new MathUtil();
  963. export { mathUtil };