MathUtil.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 == Math.PI / 2 || angle == 1.5 * Math.PI) {
  42. return { x: point.x };
  43. }
  44. let a = Math.tan(angle);
  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 > Math.PI) {
  121. angle = 2 * Math.PI - 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 Math.PI;
  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.y = point.y;
  323. } else if (line.hasOwnProperty("y")) {
  324. parameter.x = point.x;
  325. }
  326. } else {
  327. parameter.a = -1 / line.a;
  328. parameter.b = point.y - point.x * parameter.a;
  329. }
  330. return parameter;
  331. }
  332. // 经过point且与line垂直的直线,该直线与line的交点
  333. getJoinLinePoint(point, line) {
  334. const verticalLine = this.getVerticalLine(line, point);
  335. const join = this.getIntersectionPoint(line, verticalLine);
  336. return join;
  337. }
  338. // 点到直线的距离
  339. getDisForPoinLine(point, line) {
  340. const join = this.getJoinLinePoint(point, line);
  341. return this.getDistance(point, join);
  342. }
  343. // 垂直线
  344. getVerticalLine(line, point) {
  345. if (typeof line.a === "undefined") {
  346. if (line.hasOwnProperty("x")) {
  347. return { y: point.y };
  348. } else if (line.hasOwnProperty("y")) {
  349. return { x: point.x };
  350. } else {
  351. return null;
  352. }
  353. } else if (line.a == 0) {
  354. return { x: point.x };
  355. } else {
  356. const tl = {};
  357. tl.a = -1 / line.a;
  358. const result = this.createLine3(tl, point);
  359. return result;
  360. }
  361. }
  362. //point在直线上,只是不确定是否在线段上
  363. //方法:point到startPoint和endPoint的距离之和与startPoint和endPoint之间的距离对比
  364. isContainForSegment(point, startPoint, endPoint, minDis) {
  365. if (!minDis) {
  366. minDis = Constant.minLen;
  367. }
  368. let dis1 =
  369. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  370. let dis2 = this.getDistance(startPoint, endPoint);
  371. if (Math.abs(dis1 - dis2) < minDis) {
  372. return true;
  373. } else {
  374. return false;
  375. }
  376. }
  377. /*
  378. //minDis
  379. isPointInPoly(point, points, minDis) {
  380. if (!minDis) {
  381. minDis = Constant.minRealDis
  382. }
  383. const x = point.x
  384. const y = point.y
  385. let inside = false
  386. // 是否在顶点附近
  387. for (let i = 0; i < points.length; ++i) {
  388. let distance = this.getDistance(point, points[i])
  389. if (distance < minDis) {
  390. return true
  391. }
  392. }
  393. // 是否在边沿
  394. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  395. let pt1 = points[i]
  396. let pt2 = points[j]
  397. const flag = this.isContainForSegment(point, pt1, pt2, minDis)
  398. if (flag) {
  399. return true
  400. }
  401. }
  402. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  403. let pt1 = points[i]
  404. let pt2 = points[j]
  405. const xi = pt1.x
  406. const yi = pt1.y
  407. const xj = pt2.x
  408. const yj = pt2.y
  409. const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
  410. if (intersect) inside = !inside
  411. }
  412. return inside
  413. }
  414. */
  415. isPointInPoly(point, points) {
  416. const x = point.x;
  417. const y = point.y;
  418. let inside = false;
  419. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  420. let pt1 = points[i];
  421. let pt2 = points[j];
  422. const xi = pt1.x;
  423. const yi = pt1.y;
  424. const xj = pt2.x;
  425. const yj = pt2.y;
  426. const intersect =
  427. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  428. if (intersect) inside = !inside;
  429. }
  430. return inside;
  431. }
  432. // 点到线段的距离
  433. // 在minDistance范围内,会吸附到point1/point2上
  434. // 返回值:type是1表示吸附在point1,是2表示吸附在point2,是0表示在线段point1-point2上;
  435. getDisForPoinSegment(point, point1, point2, minDistance) {
  436. const line = this.createLine1(point1, point2);
  437. const join = this.getJoinLinePoint(point, line);
  438. const dis = this.getDistance(point1, point2);
  439. const dis1 = this.getDistance(join, point1);
  440. const dis2 = this.getDistance(join, point2);
  441. if (
  442. this.getDistance(join, point1) > dis ||
  443. this.getDistance(join, point2) > dis
  444. ) {
  445. // 在线段外
  446. if (dis1 < dis2 && dis1 < minDistance) {
  447. return { type: 1, join: point1 };
  448. } else if (dis2 < dis1 && dis2 < minDistance) {
  449. return { type: 2, join: point2 };
  450. } else {
  451. return null;
  452. }
  453. } else {
  454. if (dis1 < minDistance) {
  455. return { type: 1, join: point1 };
  456. } else if (dis2 < minDistance) {
  457. return { type: 2, join: point2 };
  458. } else if (this.getDistance(point, join) < minDistance) {
  459. return { type: 0, join: join };
  460. }
  461. }
  462. }
  463. PointInSegment(Q, pi, pj, minDis) {
  464. if (
  465. this.getDistance(Q, pi) < Constant.minAdsorbPix ||
  466. this.getDistance(Q, pj) < Constant.minAdsorbPix
  467. ) {
  468. return true;
  469. }
  470. if (!minDis) {
  471. minDis = 0.1;
  472. }
  473. minDis = minDis / 2;
  474. const offset1 = (Q.x - pi.x) * (pj.y - pi.y) - (pj.x - pi.x) * (Q.y - pi.y);
  475. const offset2 = Math.min(pi.x, pj.x) - Q.x;
  476. const offset3 = Q.x - Math.max(pi.x, pj.x);
  477. const offset4 = Math.min(pi.y, pj.y) - Q.y;
  478. const offset5 = Q.y - Math.max(pi.y, pj.y);
  479. if (
  480. Math.abs(offset1) < minDis &&
  481. (offset2 <= 0 || Math.abs(offset2) < minDis) &&
  482. (offset3 <= 0 || Math.abs(offset3) < minDis) &&
  483. (offset4 <= 0 || Math.abs(offset4) < minDis) &&
  484. (offset5 <= 0 || Math.abs(offset5) < minDis)
  485. ) {
  486. return true;
  487. } else {
  488. return false;
  489. }
  490. }
  491. //点p是否在线段AB上
  492. isPointOnSegment(p, A, B) {
  493. // 计算向量 AP 和 BP
  494. const AP = {
  495. x: p.x - A.x,
  496. y: p.y - A.y,
  497. };
  498. const BP = {
  499. x: p.x - B.x,
  500. y: p.y - B.y,
  501. };
  502. // 计算向量 AB 的长度和方向
  503. const AB = {
  504. x: B.x - A.x,
  505. y: B.y - A.y,
  506. };
  507. const AB_length = this.getDistance(A, B);
  508. const AB_direction = {
  509. x: AB.x / AB_length,
  510. y: AB.y / AB_length,
  511. };
  512. // 检查 AP 和 BP 的方向是否与 AB 相同,并检查它们的长度是否小于等于 AB 的长度
  513. const dot_product_AP = AP.x * AB_direction.x + AP.y * AB_direction.y;
  514. const dot_product_BP = BP.x * AB_direction.x + BP.y * AB_direction.y;
  515. //return dot_product_AP >= 0 && dot_product_BP <= 0 && Math.abs(AP.x * BP.y - AP.y * BP.x) <= AB_length * Number.EPSILON;
  516. return (
  517. dot_product_AP >= 0 &&
  518. dot_product_BP <= 0 &&
  519. Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.01
  520. );
  521. }
  522. clonePoint(p1, p2) {
  523. p1.x = p2.x;
  524. p1.y = p2.y;
  525. }
  526. equalPoint(p1, p2) {
  527. if (p1.x == p2.x && p1.y == p2.y) {
  528. return true;
  529. } else {
  530. return false;
  531. }
  532. }
  533. crossTwoLines(point1, point2, point3, point4, dis) {
  534. if (typeof dis == "undefined") {
  535. dis = Constant.minAdsorbPix;
  536. }
  537. const join = this.getIntersectionPoint2(point1, point2, point3, point4);
  538. if (join != null) {
  539. if (
  540. this.getDistance(point1, join) > dis &&
  541. this.getDistance(point2, join) > dis &&
  542. this.getDistance(point3, join) > dis &&
  543. this.getDistance(point4, join) > dis
  544. ) {
  545. if (
  546. this.getDistance(point1, join) < this.getDistance(point1, point2) &&
  547. this.getDistance(point2, join) < this.getDistance(point1, point2) &&
  548. this.getDistance(point3, join) < this.getDistance(point3, point4) &&
  549. this.getDistance(point4, join) < this.getDistance(point3, point4)
  550. ) {
  551. return true;
  552. } else {
  553. return false;
  554. }
  555. }
  556. } else {
  557. if (
  558. this.PointInSegment(point1, point3, point4) ||
  559. this.PointInSegment(point2, point3, point4)
  560. ) {
  561. return true;
  562. }
  563. }
  564. return false;
  565. }
  566. getDisPointsLine(line, point, distance1, distance2) {
  567. const newpoint1 = {};
  568. const newpoint2 = {};
  569. const result = {};
  570. if (line.hasOwnProperty("x")) {
  571. newpoint1.x = line.x;
  572. newpoint1.y = point.y - distance1;
  573. newpoint2.x = line.x;
  574. newpoint2.y = point.y + distance2;
  575. } else if (line.hasOwnProperty("y")) {
  576. newpoint1.y = line.y;
  577. newpoint1.x = point.x - distance1;
  578. newpoint2.y = line.y;
  579. newpoint2.x = point.x + distance2;
  580. } else {
  581. const a = Math.atan(line.a);
  582. const t_line = { a: -1 / line.a };
  583. const line_ab2 = this.createLine3(t_line, point);
  584. const join = this.getIntersectionPoint(line, line_ab2);
  585. newpoint1.x = join.x - distance1 * Math.cos(a);
  586. newpoint1.y = join.y - distance1 * Math.sin(a);
  587. newpoint2.x = join.x + distance2 * Math.cos(a);
  588. newpoint2.y = join.y + distance2 * Math.sin(a);
  589. }
  590. result.newpoint1 = newpoint1;
  591. result.newpoint2 = newpoint2;
  592. return result;
  593. }
  594. getBoundingBox(points) {
  595. let minX = points[0].x;
  596. let maxX = points[0].x;
  597. let minY = points[0].y;
  598. let maxY = points[0].y;
  599. for (let i = 1; i < points.length; ++i) {
  600. const point = points[i];
  601. if (minX > point.x) {
  602. minX = point.x;
  603. }
  604. if (minY > point.y) {
  605. minY = point.y;
  606. }
  607. if (maxX < point.x) {
  608. maxX = point.x;
  609. }
  610. if (maxY < point.y) {
  611. maxY = point.y;
  612. }
  613. }
  614. const box = {};
  615. box.minX = minX;
  616. box.minY = minY;
  617. box.maxX = maxX;
  618. box.maxY = maxY;
  619. return box;
  620. }
  621. getBoundingBox2(points) {
  622. let minX = null;
  623. let maxX = null;
  624. let minY = null;
  625. let maxY = null;
  626. for (let key in points) {
  627. const point = points[key];
  628. if (minX == null || minX > point.x) {
  629. minX = point.x;
  630. }
  631. if (minY == null || minY > point.y) {
  632. minY = point.y;
  633. }
  634. if (maxX == null || maxX < point.x) {
  635. maxX = point.x;
  636. }
  637. if (maxY == null || maxY < point.y) {
  638. maxY = point.y;
  639. }
  640. }
  641. const box = {};
  642. box.minX = minX;
  643. box.minY = minY;
  644. box.maxX = maxX;
  645. box.maxY = maxY;
  646. return box;
  647. }
  648. ComputePolygonArea(points) {
  649. const point_num = points.length;
  650. if (point_num < 3) {
  651. return 0;
  652. }
  653. let s = points[0].y * (points[point_num - 1].x - points[1].x);
  654. for (let i = 1; i < point_num; ++i)
  655. s += points[i].y * (points[i - 1].x - points[(i + 1) % point_num].x);
  656. return Math.abs(s / 2.0);
  657. }
  658. // 获取多边形重心
  659. getPolygonCore(points) {
  660. function Area(p0, p1, p2) {
  661. let area = 0.0;
  662. area =
  663. p0.x * p1.y +
  664. p1.x * p2.y +
  665. p2.x * p0.y -
  666. p1.x * p0.y -
  667. p2.x * p1.y -
  668. p0.x * p2.y;
  669. return area / 2;
  670. }
  671. let sum_x = 0;
  672. let sum_y = 0;
  673. let sum_area = 0;
  674. let p1 = points[1];
  675. for (let i = 2; i < points.length; i++) {
  676. const p2 = points[i];
  677. const area = Area(points[0], p1, p2);
  678. sum_area += area;
  679. sum_x += (points[0].x + p1.x + p2.x) * area;
  680. sum_y += (points[0].y + p1.y + p2.y) * area;
  681. p1 = p2;
  682. }
  683. const xx = sum_x / sum_area / 3;
  684. const yy = sum_y / sum_area / 3;
  685. return {
  686. x: xx,
  687. y: yy,
  688. };
  689. }
  690. // points1是否在points2里
  691. isPolyInPoly(points1, points2, minDis) {
  692. for (let i = 0; i < points1.length; ++i) {
  693. let flag = false;
  694. for (let j = 0; j < points2.length; ++j) {
  695. if (this.equalPoint(points1[i], points2[j])) {
  696. flag = true;
  697. break;
  698. }
  699. }
  700. if (!flag) {
  701. if (!this.isPointInPoly(points1[i], points2, minDis)) {
  702. return false;
  703. }
  704. } else {
  705. const nextIndex = i == points1.length - 1 ? 0 : i + 1;
  706. const mid = {
  707. x: (points1[i].x + points1[nextIndex].x) / 2,
  708. y: (points1[i].y + points1[nextIndex].y) / 2,
  709. };
  710. if (!this.isPointInPoly(mid, points2, minDis)) {
  711. return false;
  712. }
  713. }
  714. }
  715. return true;
  716. }
  717. dotPoints(pt1, pt2, point1, point2) {
  718. let vt1 = {};
  719. let vt2 = {};
  720. vt1.start = {};
  721. vt1.end = {};
  722. vt1.start.x = 0;
  723. vt1.start.y = 0;
  724. vt1.end.x = pt2.x - pt1.x;
  725. vt1.end.y = pt2.y - pt1.y;
  726. vt2.start = {};
  727. vt2.end = {};
  728. vt2.start.x = 0;
  729. vt2.start.y = 0;
  730. vt2.end.x = point2.x - point1.x;
  731. vt2.end.y = point2.y - point1.y;
  732. let result = vt1.end.x * vt2.end.x + vt1.end.y * vt2.end.y;
  733. return result;
  734. }
  735. //start是起点,target是朝着目标移动,distance是移动的距离
  736. translate(start, target, point, distance) {
  737. let dx = target.x - start.x;
  738. let dy = target.y - start.y;
  739. let dis = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  740. let result = {
  741. x: point.x + (dx * distance) / dis,
  742. y: point.y + (dy * distance) / dis,
  743. };
  744. return result;
  745. }
  746. //射线与线段相交
  747. // intersection(rayStart, rayEnd, segmentStart, segmentEnd) {
  748. // // 计算射线和线段的方向向量
  749. // const rayDirection = {
  750. // x:rayEnd.x - rayStart.x,
  751. // y:rayEnd.y - rayStart.y,
  752. // };
  753. // const segmentDirection = {
  754. // x:segmentEnd.x - segmentStart.x,
  755. // y:segmentEnd.y - segmentStart.y,
  756. // };
  757. // // 计算射线和线段的起点之间的向量
  758. // const startPointVector = {
  759. // x:rayStart.x - segmentStart.x,
  760. // y:rayStart.y - segmentStart.y,
  761. // };
  762. // // 计算射线和线段的叉积
  763. // const crossProduct = rayDirection.x * segmentDirection.y - rayDirection.y * segmentDirection.x;
  764. // // 如果叉积为0,则表示射线和线段平行
  765. // if (crossProduct === 0) {
  766. // return null;
  767. // }
  768. // // 计算线段起点到射线的交点的向量
  769. // const t = (startPointVector.x * segmentDirection.y - startPointVector.y * segmentDirection.x) / crossProduct;
  770. // // 如果t的值小于0,则交点在射线的起点之后
  771. // if (t < 0) {
  772. // return null;
  773. // }
  774. // // 计算交点的坐标
  775. // const intersectionX = rayStart.x + t * rayDirection.x;
  776. // const intersectionY = rayStart.y + t * rayDirection.y;
  777. // // 如果交点在线段的范围内,则返回交点坐标
  778. // if ((intersectionX >= Math.min(segmentStart.x, segmentEnd.x)) &&
  779. // (intersectionX <= Math.max(segmentStart.x, segmentEnd.x)) &&
  780. // (intersectionY >= Math.min(segmentStart.y, segmentEnd.y)) &&
  781. // (intersectionY <= Math.max(segmentStart.y, segmentEnd.y))) {
  782. // //return [intersectionX, intersectionY];
  783. // return {
  784. // x:intersectionX,
  785. // y:intersectionY,
  786. // };
  787. // }
  788. // // 否则返回null
  789. // return null;
  790. // }
  791. // raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  792. // // 计算射线和线段的交点
  793. // const x1 = rayOrigin.x
  794. // const y1 = rayOrigin.y
  795. // const x2 = segmentStart.x
  796. // const y2 = segmentStart.y
  797. // const dx1 = rayDirection.x
  798. // const dy1 = rayDirection.y
  799. // const dx2 = segmentEnd.x - x2
  800. // const dy2 = segmentEnd.y - y2
  801. // const crossProduct = dx1 * dy2 - dx2 * dy1
  802. // if (Math.abs(crossProduct) < 1e-8) {
  803. // // 射线和线段平行或共线
  804. // return null
  805. // }
  806. // const t1 = (dx2 * (y1 - y2) - dy2 * (x1 - x2)) / crossProduct
  807. // const t2 = (dx1 * (y1 - y2) - dy1 * (x1 - x2)) / crossProduct
  808. // if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
  809. // // 有交点,计算交点坐标
  810. // const intersectionX = x1 + t1 * dx1
  811. // const intersectionY = y1 + t1 * dy1
  812. // return {
  813. // x: intersectionX,
  814. // y: intersectionY,
  815. // }
  816. // } else {
  817. // // 没有交点
  818. // return null
  819. // }
  820. // }
  821. raySegmentIntersection(rayOrigin, rayDirection, segmentStart, segmentEnd) {
  822. const end = {
  823. x: rayOrigin.x + rayDirection.x,
  824. y: rayOrigin.y - rayDirection.z,
  825. };
  826. const line = this.createLine1(rayOrigin, end);
  827. const join = this.getIntersectionPoint4(segmentStart, segmentEnd, line);
  828. if (join == null) {
  829. return null;
  830. } else {
  831. const dis = this.getDistance(end, join);
  832. const dis1 = this.getDistance(rayOrigin, join);
  833. const dis2 = this.getDistance(rayOrigin, end);
  834. if (dis - (dis1 + dis2) + 0.01 > 0) {
  835. return null;
  836. } else {
  837. return join;
  838. }
  839. }
  840. }
  841. RectangleVertex(startPoint, endPoint, width) {
  842. let line = this.createLine1(startPoint, endPoint);
  843. let lines = this.getParallelLineForDistance(line, width / 2);
  844. let leftEdgeStart, rightEdgeStart, rightEdgeEnd, leftEdgeEnd;
  845. let point = null;
  846. let points = [];
  847. //先计算start部分
  848. point = startPoint;
  849. points.push(endPoint);
  850. points.push(startPoint);
  851. let point1 = this.getJoinLinePoint(point, lines.line1);
  852. let point2 = this.getJoinLinePoint(point, lines.line2);
  853. points[2] = point1;
  854. if (this.isClockwise(points)) {
  855. rightEdgeStart = point1;
  856. leftEdgeStart = point2;
  857. } else {
  858. rightEdgeStart = point2;
  859. leftEdgeStart = point1;
  860. }
  861. //再计算end部分
  862. points = [];
  863. point = endPoint;
  864. points.push(startPoint);
  865. points.push(endPoint);
  866. point1 = this.getJoinLinePoint(point, lines.line1);
  867. point2 = this.getJoinLinePoint(point, lines.line2);
  868. points[2] = point1;
  869. if (this.isClockwise(points)) {
  870. rightEdgeEnd = point2;
  871. leftEdgeEnd = point1;
  872. } else {
  873. rightEdgeEnd = point1;
  874. leftEdgeEnd = point2;
  875. }
  876. return {
  877. leftEdgeStart: leftEdgeStart,
  878. rightEdgeStart: rightEdgeStart,
  879. rightEdgeEnd: rightEdgeEnd,
  880. leftEdgeEnd: leftEdgeEnd,
  881. };
  882. }
  883. //start到end的射线中取一点point,start-end和end-point的距离相同
  884. getPositionForExtendedLine(start, end) {
  885. const dx = end.x - start.x;
  886. const dy = end.y - start.y;
  887. const point = {
  888. x: end.x + dx,
  889. y: end.y + dy,
  890. };
  891. return point;
  892. }
  893. isOnRay(start, dir, position) {
  894. const v1 = { x: dir.x - start.x, y: dir.y - start.y };
  895. const v2 = { x: position.x - start.x, y: position.y - start.y };
  896. return v1.x * v2.y - v1.y * v2.x;
  897. }
  898. //向量是否同样的方向
  899. isSameDirForVector(point1, point2, p1, p2) {
  900. const v1 = {
  901. x: point2.x - point1.x,
  902. y: point2.y - point1.y,
  903. };
  904. const v2 = {
  905. x: p2.x - p1.x,
  906. y: p2.y - p1.y,
  907. };
  908. const value = this.dot(v1, v2);
  909. if (value > 0) {
  910. return true;
  911. }
  912. {
  913. return false;
  914. }
  915. }
  916. angleTo(v1, v2) {
  917. const denominator = Math.sqrt(this.lengthSq(v1) * this.lengthSq(v2));
  918. if (denominator === 0) return Math.PI / 2;
  919. const theta = this.dot(v1, v2) / denominator;
  920. //return Math.acos(this.clamp(theta, -1, 1));
  921. return (Math.acos(this.clamp(theta, -1, 1)) / Math.PI) * 180;
  922. }
  923. //点乘
  924. dot(v1, v2) {
  925. return v1.x * v2.x + v1.y * v2.y;
  926. }
  927. //叉乘
  928. cross(v1, v2) {
  929. return v1.x * v2.y - v1.y * v2.x;
  930. }
  931. clamp(value, min, max) {
  932. return Math.max(min, Math.min(max, value));
  933. }
  934. lengthSq(v) {
  935. return v.x * v.x + v.y * v.y;
  936. }
  937. }
  938. const mathUtil = new MathUtil();
  939. export { mathUtil };