DrawPincerArrow.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. import {
  2. createTooltip
  3. } from "../../../common/common.js";
  4. import {
  5. isRuntimeApp,
  6. isRuntimeWeb,
  7. createOperationMainDom,
  8. showTooltipMessage
  9. } from "../../../common/RuntimeEnvironment.js";
  10. /*
  11. 九、绘制钳击箭头
  12. */
  13. class DrawPincerArrow {
  14. constructor(arg) {
  15. this.viewer = arg.viewer;
  16. this.Cesium = arg.Cesium;
  17. // this.callback=arg.callback;
  18. this.floatingPoint = null; //标识点
  19. this._PincerArrow = null; //活动箭头
  20. this._PincerArrowLast = null; //最后一个箭头
  21. this._positions = []; //活动点
  22. this._entities_point = []; //脏数据
  23. this._entities_PincerArrow = []; //脏数据
  24. this._PincerArrowData = null; //用于构造箭头数据
  25. this.DrawStartEvent = new Cesium.Event(); //开始绘制事件
  26. this.DrawEndEvent = new Cesium.Event(); //结束绘制事件
  27. this._tooltip = createTooltip(this.viewer.container);
  28. /* 通用参数集合 */
  29. this._param = {
  30. id: "DrawStraightArrow",
  31. polygonColor: 'rgba(0,255,0,0.5)', //面填充颜色
  32. outlineColor: 'rgba(255, 255, 255, 1)', //边框颜色
  33. outlineWidth: 1, //边框宽度
  34. }
  35. /* 创建面材质 */
  36. this.polygonMaterial = Cesium.Color.fromCssColorString(this._param.polygonColor);
  37. /* 创建线材质 */
  38. // this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({//曲线
  39. // dashLength: 16,
  40. // color: Cesium.Color.fromCssColorString(this._param.outlineColor)
  41. // });
  42. this.outlineMaterial = Cesium.Color.fromCssColorString(this._param.outlineColor);
  43. }
  44. //返回箭头
  45. get PincerArrow() {
  46. return this._PincerArrowLast;
  47. }
  48. //返回箭头数据用于加载箭头
  49. getData() {
  50. return this._PincerArrowData;
  51. }
  52. //加载箭头
  53. addload(data) {
  54. var $this = this;
  55. if (data.length < 3) {
  56. return;
  57. }
  58. //计算面
  59. var res = $this.doubleArrow(data);
  60. var returnData = res.polygonalPoint;
  61. var arrowEntity = $this.viewer.entities.add({
  62. Type: 'DrawPincerArrow',
  63. Position: data,
  64. id: data.id || $this.objId,
  65. polygon: {
  66. hierarchy: new $this.Cesium.PolygonHierarchy(returnData),
  67. show: true,
  68. fill: true,
  69. clampToGround: true,
  70. material: $this.polygonMaterial
  71. }
  72. })
  73. return arrowEntity
  74. }
  75. // 修改编辑调用计算
  76. computePosition(data) {
  77. let $this = this
  78. var lnglatArr = [];
  79. for (var i = 0; i < data.length; i++) {
  80. var lnglat = $this.cartesianToLatlng(data[i]);
  81. lnglatArr.push(lnglat)
  82. }
  83. $this._PincerArrowData = lnglatArr;
  84. var res = $this.doubleArrow(lnglatArr);
  85. var returnData = res.polygonalPoint;
  86. return new $this.Cesium.PolygonHierarchy(returnData)
  87. }
  88. //开始创建
  89. startCreate(drawType) {
  90. if (isRuntimeApp()) {
  91. showTooltipMessage("点击开始绘制");
  92. }
  93. var $this = this;
  94. this.drawType = drawType;
  95. this.handler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
  96. //单击开始绘制
  97. this.handler.setInputAction(function(evt) {
  98. if (isRuntimeApp()) {
  99. //屏幕坐标转地形上坐标
  100. var cartesian = $this.getCatesian3FromPX(evt.position);
  101. if (!cartesian) {
  102. return;
  103. }
  104. $this.createPoint(cartesian); // 绘制点
  105. $this._positions.push(cartesian);
  106. if ($this._positions.length < 3 || $this._positions.length === 4) {
  107. showTooltipMessage("点击添加点");
  108. }
  109. if ($this._positions.length === 2) {
  110. if (!$this.Cesium.defined($this._PincerArrow)) {
  111. $this._PincerArrow = $this.createPincerArrow();
  112. }
  113. }
  114. if ($this._positions.length === 3) {
  115. $this._positions.pop();
  116. $this._positions.push(cartesian);
  117. showTooltipMessage("点击添加点,点击完成按钮,结束绘制");
  118. //创建按钮
  119. createOperationMainDom();
  120. //隐藏回退按钮
  121. document.getElementById("btnDrawBackout").style.display = 'none';
  122. //完成绘制
  123. document.getElementById("btnDrawComplete").onclick = () => {
  124. $this._PincerArrowData = $this._positions.concat();
  125. $this.viewer.entities.remove($this._PincerArrow); //移除
  126. $this._PincerArrow = null;
  127. $this._positions = [];
  128. var lnglatArr = [];
  129. for (var i = 0; i < $this._PincerArrowData.length; i++) {
  130. var lnglat = $this.cartesianToLatlng($this._PincerArrowData[i]);
  131. lnglatArr.push(lnglat)
  132. }
  133. $this._PincerArrowData = lnglatArr;
  134. var pincerArrow = $this.addload(lnglatArr); //加载
  135. $this._entities_PincerArrow.push(pincerArrow);
  136. $this._PincerArrowLast = pincerArrow;
  137. $this.clearPoint();
  138. $this.destroy();
  139. let buttonDiv = document.getElementById("drawButtonDiv");
  140. if (buttonDiv) {
  141. //从页面移除
  142. document.body.removeChild(buttonDiv);
  143. }
  144. }
  145. }
  146. if ($this._positions.length === 5) {
  147. showTooltipMessage("点击完成按钮,结束绘制");
  148. $this.destroy();
  149. }
  150. } else {
  151. console.log('监听鼠标事件', '单击')
  152. /* 锁定点击事件 以免和双击事件冲突 */
  153. clearTimeout($this._timer);
  154. $this._timer = setTimeout(function() {
  155. var cartesian = $this.getCatesian3FromPX(evt.position);
  156. if (!cartesian) {
  157. return;
  158. }
  159. if ($this._positions.length == 0) {
  160. $this.floatingPoint = $this.createPoint(cartesian);
  161. }
  162. if ($this._positions.length < 5) {
  163. $this._positions.push(cartesian);
  164. $this.createPoint(cartesian); // 绘制点
  165. }
  166. if ($this._positions.length == 5) {
  167. $this._positions[4] = cartesian;
  168. $this.floatingPoint.position.setValue(cartesian);
  169. }
  170. }, 200);
  171. }
  172. }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK);
  173. //移动时绘制面
  174. this.handler.setInputAction(function(evt) {
  175. /* 如果运行环境是App 则禁止使用鼠标移动事件 */
  176. if (isRuntimeApp()) return;
  177. if ($this._positions.length == 0) {
  178. $this._tooltip.showAt(evt.endPosition, "点击开始绘制");
  179. } else {
  180. $this._tooltip.showAt(evt.endPosition, "点击添加点");
  181. }
  182. var cartesian = $this.getCatesian3FromPX(evt.endPosition);
  183. if (!cartesian) return;
  184. if ($this._positions.length < 2) return;
  185. if (!$this.Cesium.defined($this._PincerArrow)) {
  186. if ($this._positions.length == 2) {
  187. $this._positions.push(cartesian);
  188. }
  189. $this._PincerArrow = $this.createPincerArrow();
  190. }
  191. $this.floatingPoint.position.setValue(cartesian);
  192. if ($this._positions.length == 3) {
  193. $this._positions[2] = cartesian;
  194. // $this._tooltip.showAt(evt.endPosition, "点击添加点,右键删除点,双击结束绘制");
  195. $this._tooltip.showAt(evt.endPosition, "点击添加点,双击结束绘制");
  196. }
  197. if ($this._positions.length > 3) {
  198. $this._positions.pop();
  199. $this._positions.push(cartesian);
  200. if ($this._positions === 4) {
  201. $this._tooltip.showAt(evt.endPosition, "点击添加点");
  202. }
  203. if ($this._positions.length == 5) {
  204. $this._tooltip.showAt(evt.endPosition, "双击结束绘制");
  205. }
  206. }
  207. console.log($this._positions.length);
  208. }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  209. //右键结束改为双击结束
  210. this.handler.setInputAction(function(evt) {
  211. // if (!$this._PincerArrow) {
  212. // return
  213. // } else {
  214. // // if(typeof $this.callback=="function"){
  215. // // $this.callback();
  216. // // }
  217. // };
  218. // var cartesian = $this.getCatesian3FromPX(evt.position);
  219. // if (!cartesian) return;
  220. // $this._positions.pop();
  221. // $this._positions.push(cartesian);
  222. // $this._PincerArrowData = $this._positions.concat();
  223. // $this.viewer.entities.remove($this._PincerArrow); //移除
  224. // $this._PincerArrow = null;
  225. // $this._positions = [];
  226. // $this.floatingPoint.position.setValue(cartesian);
  227. // var lnglatArr = [];
  228. // for (var i = 0; i < $this._PincerArrowData.length; i++) {
  229. // var lnglat = $this.cartesianToLatlng($this._PincerArrowData[i]);
  230. // lnglatArr.push(lnglat)
  231. // }
  232. // $this._PincerArrowData = lnglatArr;
  233. // var pincerArrow = $this.addload(lnglatArr); //加载
  234. // $this._entities_PincerArrow.push(pincerArrow);
  235. // $this._PincerArrowLast = pincerArrow;
  236. // $this.clearPoint();
  237. // $this.destroy();
  238. }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  239. //双击结束
  240. this.handler.setInputAction(function(evt) {
  241. /* 如果运行环境是App 则禁止使用鼠标双击事件 */
  242. if (isRuntimeApp()) return;
  243. console.log('监听鼠标事件', '双击')
  244. /* 解除锁定 */
  245. clearTimeout($this._timer);
  246. if (!$this._PincerArrow) {
  247. return
  248. } else {
  249. // if(typeof $this.callback=="function"){
  250. // $this.callback();
  251. // }
  252. };
  253. var cartesian = $this.getCatesian3FromPX(evt.position);
  254. if (!cartesian) return;
  255. $this._positions.pop();
  256. // $this._positions.push(cartesian);
  257. $this._PincerArrowData = $this._positions.concat();
  258. $this.viewer.entities.remove($this._PincerArrow); //移除
  259. $this._PincerArrow = null;
  260. $this._positions = [];
  261. $this.floatingPoint.position.setValue(cartesian);
  262. var lnglatArr = [];
  263. for (var i = 0; i < $this._PincerArrowData.length; i++) {
  264. var lnglat = $this.cartesianToLatlng($this._PincerArrowData[i]);
  265. lnglatArr.push(lnglat)
  266. }
  267. $this._PincerArrowData = lnglatArr;
  268. var pincerArrow = $this.addload(lnglatArr); //加载
  269. $this._entities_PincerArrow.push(pincerArrow);
  270. $this._PincerArrowLast = pincerArrow;
  271. $this.clearPoint();
  272. $this.destroy();
  273. $this._tooltip.setVisible(false);
  274. }, $this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  275. }
  276. //创建钳击箭头
  277. createPincerArrow() {
  278. var $this = this;
  279. $this._computeTempPositions();
  280. var arrowEntity = $this.viewer.entities.add({
  281. polygon: {
  282. hierarchy: new $this.Cesium.CallbackProperty(
  283. function() {
  284. //计算面
  285. var lnglatArr = [];
  286. for (var i = 0; i < $this._positions.length; i++) {
  287. var lnglat = $this.cartesianToLatlng($this._positions[i]);
  288. lnglatArr.push(lnglat)
  289. }
  290. var res = $this.doubleArrow(lnglatArr);
  291. var returnData = res.polygonalPoint;
  292. return new $this.Cesium.PolygonHierarchy(returnData);
  293. }, false),
  294. show: true,
  295. fill: true,
  296. clampToGround: true,
  297. material: $this.polygonMaterial
  298. }
  299. })
  300. $this._entities_PincerArrow.push(arrowEntity);
  301. return arrowEntity
  302. }
  303. //创建点
  304. createPoint(cartesian) {
  305. var $this = this;
  306. var point = this.viewer.entities.add({
  307. position: cartesian,
  308. point: {
  309. pixelSize: 10,
  310. color: $this.Cesium.Color.RED,
  311. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  312. }
  313. });
  314. $this._entities_point.push(point);
  315. return point;
  316. }
  317. cartesianToLatlng(cartesian) {
  318. let cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  319. let lat = this.Cesium.Math.toDegrees(cartographic.latitude);
  320. let lng = this.Cesium.Math.toDegrees(cartographic.longitude);
  321. let alt = cartographic.height;
  322. return [lng, lat, alt];
  323. }
  324. //销毁
  325. destroy() {
  326. if (this.handler) {
  327. this.handler.destroy();
  328. this.handler = null;
  329. }
  330. }
  331. clearPoint() {
  332. this.DrawEndEvent.raiseEvent(this._PincerArrowLast, this._PincerArrowData, this.drawType);
  333. for (var i = 0; i < this._entities_point.length; i++) {
  334. this.viewer.entities.remove(this._entities_point[i]);
  335. }
  336. this._entities_point = []; //脏数据
  337. }
  338. //清空实体对象
  339. clear() {
  340. for (var i = 0; i < this._entities_point.length; i++) {
  341. this.viewer.entities.remove(this._entities_point[i]);
  342. }
  343. for (var i = 0; i < this._entities_PincerArrow.length; i++) {
  344. this.viewer.entities.remove(this._entities_PincerArrow[i]);
  345. }
  346. this.floatingPoint = null; //标识点
  347. this._PincerArrow = null; //活动箭头
  348. this._PincerArrowLast = null; //最后一个箭头
  349. this._positions = []; //活动点
  350. this._entities_point = []; //脏数据
  351. this._entities_PincerArrow = []; //脏数据
  352. this._PincerArrowData = null; //用于构造箭头数据
  353. }
  354. getCatesian3FromPX(px) {
  355. var cartesian;
  356. var ray = this.viewer.camera.getPickRay(px);
  357. if (!ray) return null;
  358. cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
  359. return cartesian;
  360. }
  361. _computeTempPositions() {
  362. var _this = this;
  363. var pnts = [].concat(_this._positions);
  364. var num = pnts.length;
  365. var first = pnts[0];
  366. var last = pnts[num - 1];
  367. if (_this._isSimpleXYZ(first, last) == false) {
  368. pnts.push(first);
  369. num += 1;
  370. }
  371. _this.tempPositions = [];
  372. for (var i = 1; i < num; i++) {
  373. var p1 = pnts[i - 1];
  374. var p2 = pnts[i];
  375. var cp = _this._computeCenterPotition(p1, p2);
  376. _this.tempPositions.push(p1);
  377. _this.tempPositions.push(cp);
  378. }
  379. }
  380. _isSimpleXYZ(p1, p2) {
  381. if (p1.x == p2.x && p1.y == p2.y && p1.z == p2.z) {
  382. return true;
  383. }
  384. return false;
  385. }
  386. _computeCenterPotition(p1, p2) {
  387. var _this = this;
  388. var c1 = _this.viewer.scene.globe.ellipsoid.cartesianToCartographic(p1);
  389. var c2 = _this.viewer.scene.globe.ellipsoid.cartesianToCartographic(p2);
  390. var cm = new _this.Cesium.EllipsoidGeodesic(c1, c2).interpolateUsingFraction(0.5);
  391. var cp = _this.viewer.scene.globe.ellipsoid.cartographicToCartesian(cm);
  392. return cp;
  393. }
  394. ////////////////////////////////////////求取箭头坐标函数/////////////////////////////////////////////////////
  395. //箭头配置函数
  396. doubleArrow(inputPoint) {
  397. var $this = this;
  398. this.connPoint = null;
  399. this.tempPoint4 = null;
  400. this.points = inputPoint;
  401. var result = {
  402. controlPoint: null,
  403. polygonalPoint: null
  404. };
  405. //获取已经点击的坐标数
  406. var t = inputPoint.length;
  407. if (!(2 > t)) {
  408. if (2 == t) return inputPoint;
  409. var o = this.points[0], //第一个点
  410. e = this.points[1], //第二个点
  411. r = this.points[2], //第三个点
  412. t = inputPoint.length; //获取已经点击的坐标数
  413. //下面的是移动点位后的坐标
  414. 3 == t ? this.tempPoint4 = $this.getTempPoint4(o, e, r) : this.tempPoint4 = this.points[3],
  415. 3 == t || 4 == t ? this.connPoint = $this.mid(o, e) : this.connPoint = this.points[4];
  416. var n, g;
  417. $this.isClockWise(o, e, r) ? (n = $this.getArrowPoints(o, this.connPoint, this.tempPoint4, !1), g = $this.getArrowPoints(this.connPoint, e, r, !0)) : (n = $this.getArrowPoints(e, this.connPoint, r, !1), g = $this.getArrowPoints(this.connPoint, o, this.tempPoint4, !0));
  418. var i = n.length,
  419. s = (i - 5) / 2,
  420. a = n.slice(0, s),
  421. l = n.slice(s, s + 5),
  422. u = n.slice(s + 5, i),
  423. c = g.slice(0, s),
  424. p = g.slice(s, s + 5),
  425. h = g.slice(s + 5, i);
  426. c = $this.getBezierPoints(c);
  427. var d = $this.getBezierPoints(h.concat(a.slice(1)));
  428. u = $this.getBezierPoints(u);
  429. var f = c.concat(p, d, l, u);
  430. var newArray = $this.array2Dto1D(f);
  431. result.controlPoint = [o, e, r, this.tempPoint4, this.connPoint];
  432. result.polygonalPoint = $this.Cesium.Cartesian3.fromDegreesArray(newArray);
  433. }
  434. return result;
  435. }
  436. getTempPoint4(t, o, e) {
  437. var $this = this;
  438. var r, n, g, i, s = $this.mid(t, o),
  439. a = $this.distance(s, e),
  440. l = $this.getAngleOfThreePoints(t, s, e);
  441. return l < Math.PI / 2 ? (n = a * Math.sin(l), g = a * Math.cos(l), i = $this.getThirdPoint(t, s, Math.PI / 2, n, !1), r = $this.getThirdPoint(s, i, Math.PI / 2, g, !0)) : l >= Math.PI / 2 && l < Math.PI ? (n = a * Math.sin(Math.PI - l), g = a * Math.cos(Math.PI - l), i = $this.getThirdPoint(t, s, Math.PI / 2, n, !1), r = $this.getThirdPoint(s, i, Math.PI / 2, g, !1)) : l >= Math.PI && l < 1.5 * Math.PI ? (n = a * Math.sin(l - Math.PI), g = a * Math.cos(l - Math.PI), i = $this.getThirdPoint(t, s, Math.PI / 2, n, !0), r = $this.getThirdPoint(s, i, Math.PI / 2, g, !0)) : (n = a * Math.sin(2 * Math.PI - l), g = a * Math.cos(2 * Math.PI - l), i = $this.getThirdPoint(t, s, Math.PI / 2, n, !0), r = $this.getThirdPoint(s, i, Math.PI / 2, g, !1)),
  442. r
  443. }
  444. mid(t, o) {
  445. return [(t[0] + o[0]) / 2, (t[1] + o[1]) / 2]
  446. }
  447. isClockWise(t, o, e) {
  448. return (e[1] - t[1]) * (o[0] - t[0]) > (o[1] - t[1]) * (e[0] - t[0])
  449. }
  450. getArrowPoints(t, o, e, r) {
  451. var $this = this;
  452. var doubleArrowDefualParam = {
  453. type: "doublearrow",
  454. headHeightFactor: .25,
  455. headWidthFactor: .3,
  456. neckHeightFactor: .85,
  457. fixPointCount: 4,
  458. neckWidthFactor: .15
  459. }
  460. this.type = doubleArrowDefualParam.type,
  461. this.headHeightFactor = doubleArrowDefualParam.headHeightFactor,
  462. this.headWidthFactor = doubleArrowDefualParam.headWidthFactor,
  463. this.neckHeightFactor = doubleArrowDefualParam.neckHeightFactor,
  464. this.neckWidthFactor = doubleArrowDefualParam.neckWidthFactor;
  465. var n = $this.mid(t, o),
  466. g = $this.distance(n, e),
  467. i = $this.getThirdPoint(e, n, 0, .3 * g, !0),
  468. s = $this.getThirdPoint(e, n, 0, .5 * g, !0);
  469. i = $this.getThirdPoint(n, i, Math.PI / 2, g / 5, r),
  470. s = $this.getThirdPoint(n, s, Math.PI / 2, g / 4, r);
  471. var a = [n, i, s, e],
  472. l = $this.getArrowHeadPoints(a, this.headHeightFactor, this.headWidthFactor, this.neckHeightFactor, this.neckWidthFactor),
  473. u = l[0],
  474. c = l[4],
  475. p = $this.distance(t, o) / $this.getBaseLength(a) / 2,
  476. h = $this.getArrowBodyPoints(a, u, c, p),
  477. d = h.length,
  478. f = h.slice(0, d / 2),
  479. E = h.slice(d / 2, d);
  480. return f.push(u),
  481. E.push(c),
  482. f = f.reverse(),
  483. f.push(o),
  484. E = E.reverse(),
  485. E.push(t),
  486. f.reverse().concat(l, E)
  487. }
  488. getArrowHeadPoints(t, o, e) {
  489. var $this = this;
  490. var doubleArrowDefualParam = {
  491. type: "doublearrow",
  492. headHeightFactor: .25,
  493. headWidthFactor: .3,
  494. neckHeightFactor: .85,
  495. fixPointCount: 4,
  496. neckWidthFactor: .15
  497. }
  498. this.type = doubleArrowDefualParam.type,
  499. this.headHeightFactor = doubleArrowDefualParam.headHeightFactor,
  500. this.headWidthFactor = doubleArrowDefualParam.headWidthFactor,
  501. this.neckHeightFactor = doubleArrowDefualParam.neckHeightFactor,
  502. this.neckWidthFactor = doubleArrowDefualParam.neckWidthFactor;
  503. var r = $this.getBaseLength(t),
  504. n = r * this.headHeightFactor,
  505. g = t[t.length - 1],
  506. i = ($this.distance(o, e), n * this.headWidthFactor),
  507. s = n * this.neckWidthFactor,
  508. a = n * this.neckHeightFactor,
  509. l = $this.getThirdPoint(t[t.length - 2], g, 0, n, !0),
  510. u = $this.getThirdPoint(t[t.length - 2], g, 0, a, !0),
  511. c = $this.getThirdPoint(g, l, Math.PI / 2, i, !1),
  512. p = $this.getThirdPoint(g, l, Math.PI / 2, i, !0),
  513. h = $this.getThirdPoint(g, u, Math.PI / 2, s, !1),
  514. d = $this.getThirdPoint(g, u, Math.PI / 2, s, !0);
  515. return [h, c, g, p, d];
  516. }
  517. getArrowBodyPoints(t, o, e, r) {
  518. var $this = this;
  519. for (var n = $this.wholeDistance(t), g = $this.getBaseLength(t), i = g * r, s = $this.distance(o, e), a = (i - s) / 2, l = 0, u = [], c = [], p = 1; p < t.length - 1; p++) {
  520. var h = $this.getAngleOfThreePoints(t[p - 1], t[p], t[p + 1]) / 2;
  521. l += $this.distance(t[p - 1], t[p]);
  522. var d = (i / 2 - l / n * a) / Math.sin(h),
  523. f = $this.getThirdPoint(t[p - 1], t[p], Math.PI - h, d, !0),
  524. E = $this.getThirdPoint(t[p - 1], t[p], h, d, !1);
  525. u.push(f), c.push(E)
  526. }
  527. return u.concat(c)
  528. }
  529. getBezierPoints(t) {
  530. if (t.length <= 2) return t;
  531. for (var o = [], e = t.length - 1, r = 0; 1 >= r; r += .01) {
  532. for (var n = 0, y = 0, g = 0; e >= g; g++) {
  533. var i = this.getBinomialFactor(e, g),
  534. s = Math.pow(r, g),
  535. a = Math.pow(1 - r, e - g);
  536. n += i * s * a * t[g][0], y += i * s * a * t[g][1]
  537. }
  538. o.push([n, y])
  539. }
  540. return o.push(t[e]), o
  541. }
  542. getBaseLength(t) {
  543. return Math.pow(this.wholeDistance(t), .99)
  544. }
  545. wholeDistance(t) {
  546. for (var o = 0, e = 0; e < t.length - 1; e++) o += this.distance(t[e], t[e + 1]);
  547. return o
  548. }
  549. getBinomialFactor(t, o) {
  550. return this.getFactorial(t) / (this.getFactorial(o) * this.getFactorial(t - o))
  551. }
  552. getFactorial(t) {
  553. if (1 >= t) return 1;
  554. if (2 == t) return 2;
  555. if (3 == t) return 6;
  556. if (4 == t) return 24;
  557. if (5 == t) return 120;
  558. for (var o = 1, e = 1; t >= e; e++) o *= e;
  559. return o
  560. }
  561. array2Dto1D(array) {
  562. var newArray = [];
  563. array.forEach(function(elt) {
  564. newArray.push(elt[0]);
  565. newArray.push(elt[1]);
  566. });
  567. return newArray;
  568. }
  569. distance(t, o) {
  570. return Math.sqrt(Math.pow(t[0] - o[0], 2) + Math.pow(t[1] - o[1], 2))
  571. }
  572. getAngleOfThreePoints(t, o, e) {
  573. var r = this.getAzimuth(o, t) - this.getAzimuth(o, e);
  574. return 0 > r ? r + 2 * Math.PI : r
  575. }
  576. getAzimuth(t, o) {
  577. var e, r = Math.asin(Math.abs(o[1] - t[1]) / this.distance(t, o));
  578. return o[1] >= t[1] && o[0] >= t[0] ? e = r + Math.PI : o[1] >= t[1] && o[0] < t[0] ? e = 2 * Math.PI - r : o[1] < t[1] && o[0] < t[0] ? e = r : o[1] < t[1] && o[0] >= t[0] && (e = Math.PI - r), e
  579. }
  580. getThirdPoint(t, o, e, r, n) {
  581. var g = this.getAzimuth(t, o),
  582. i = n ? g + e : g - e,
  583. s = r * Math.cos(i),
  584. a = r * Math.sin(i);
  585. return [o[0] + s, o[1] + a]
  586. }
  587. }
  588. export default DrawPincerArrow