MapRoadUtils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. Object.defineProperty(i, "__esModule", {value: !0});
  5. const a = t("MapType"),
  6. n = t("Point"),
  7. r = t("RoadNode");
  8. i.default = class t {
  9. static get instance() {
  10. return null == this._instance && (this._instance = new t()), this._instance;
  11. }
  12. updateMapInfo(t, e, i, s, o) {
  13. switch (
  14. ((this._mapWidth = t),
  15. (this._mapHeight = e),
  16. (this._nodeWidth = i),
  17. (this._nodeHeight = s),
  18. (this._halfNodeWidth = Math.floor(this._nodeWidth / 2)),
  19. (this._halfNodeHeight = Math.floor(this._nodeHeight / 2)),
  20. (this._col = Math.ceil(t / this._nodeWidth)),
  21. (this._row = Math.ceil(e / this._nodeHeight)),
  22. (this._mapType = o),
  23. this._mapType)
  24. ) {
  25. case a.MapType.angle45:
  26. this._mapRoad = new l(
  27. this._row,
  28. this._col,
  29. this._nodeWidth,
  30. this._nodeHeight,
  31. this._halfNodeWidth,
  32. this._halfNodeHeight
  33. );
  34. break;
  35. case a.MapType.angle90:
  36. this._mapRoad = new h(
  37. this._row,
  38. this._col,
  39. this._nodeWidth,
  40. this._nodeHeight,
  41. this._halfNodeWidth,
  42. this._halfNodeHeight
  43. );
  44. break;
  45. case a.MapType.honeycomb:
  46. (this._col = 2 * Math.ceil((this._mapWidth - this._nodeWidth / 4) / ((this._nodeWidth / 4) * 6))),
  47. (this._row = Math.ceil((this._mapHeight - this._nodeHeight / 2) / this._nodeHeight)),
  48. (this._mapRoad = new c(
  49. this._row,
  50. this._col,
  51. this._nodeWidth,
  52. this._nodeHeight,
  53. this._halfNodeWidth,
  54. this._halfNodeHeight
  55. ));
  56. }
  57. }
  58. getNodeByPixel(t, e) {
  59. return this._mapRoad ? this._mapRoad.getNodeByPixel(t, e) : new r.default();
  60. }
  61. getNodeByDerect(t, e) {
  62. return this._mapRoad ? this._mapRoad.getNodeByDerect(t, e) : new r.default();
  63. }
  64. getNodeByWorldPoint(t, e) {
  65. return this._mapRoad ? this._mapRoad.getNodeByWorldPoint(t, e) : new r.default();
  66. }
  67. getWorldPointByPixel(t, e) {
  68. return this._mapRoad ? this._mapRoad.getWorldPointByPixel(t, e) : new n.default();
  69. }
  70. getPixelByWorldPoint(t, e) {
  71. return this._mapRoad ? this._mapRoad.getPixelByWorldPoint(t, e) : new n.default();
  72. }
  73. getDerectByPixel(t, e) {
  74. return this._mapRoad ? this._mapRoad.getDerectByPixel(t, e) : new n.default();
  75. }
  76. getDerectByWorldPoint(t, e) {
  77. return this._mapRoad ? this._mapRoad.getDerectByWorldPoint(t, e) : new n.default();
  78. }
  79. getPixelByDerect(t, e) {
  80. return this._mapRoad ? this._mapRoad.getPixelByDerect(t, e) : new n.default();
  81. }
  82. get mapWidth() {
  83. return this._mapWidth;
  84. }
  85. get mapHeight() {
  86. return this._mapHeight;
  87. }
  88. get nodeWidth() {
  89. return this._nodeWidth;
  90. }
  91. get nodeHeight() {
  92. return this._nodeHeight;
  93. }
  94. get row() {
  95. return this._row;
  96. }
  97. get col() {
  98. return this._col;
  99. }
  100. get halfNodeWidth() {
  101. return this._halfNodeWidth;
  102. }
  103. get halfNodeHeight() {
  104. return this._halfNodeHeight;
  105. }
  106. get mapType() {
  107. return this._mapType;
  108. }
  109. };
  110. class l {
  111. constructor(t, e, i, s, o, a) {
  112. (this._row = t),
  113. (this._col = e),
  114. (this._nodeWidth = i),
  115. (this._nodeHeight = s),
  116. (this._halfNodeWidth = o),
  117. (this._halfNodeHeight = a);
  118. }
  119. getNodeByPixel(t, e) {
  120. var i = this.getWorldPointByPixel(t, e),
  121. s = this.getPixelByWorldPoint(i.x, i.y),
  122. t = this.getDerectByPixel(t, e),
  123. e = new r.default();
  124. return (e.cx = i.x), (e.cy = i.y), (e.px = s.x), (e.py = s.y), (e.dx = t.x), (e.dy = t.y), e;
  125. }
  126. getNodeByDerect(t, e) {
  127. var i = this.getPixelByDerect(t, e),
  128. s = this.getWorldPointByPixel(i.x, i.y),
  129. o = new r.default();
  130. return (o.cx = s.x), (o.cy = s.y), (o.px = i.x), (o.py = i.y), (o.dx = t), (o.dy = e), o;
  131. }
  132. getNodeByWorldPoint(t, e) {
  133. e = this.getPixelByWorldPoint(t, e);
  134. return this.getNodeByPixel(e.x, e.y);
  135. }
  136. getWorldPointByPixel(t, e) {
  137. var i = Math.ceil(t / this._nodeWidth - 0.5 + e / this._nodeHeight) - 1,
  138. e = this._col - 1 - Math.ceil(t / this._nodeWidth - 0.5 - e / this._nodeHeight);
  139. return new n.default(i, e);
  140. }
  141. getPixelByWorldPoint(t, e) {
  142. var i = Math.floor((t + 1 - (e - (this._col - 1))) * this._halfNodeWidth),
  143. e = Math.floor((t + 1 + (e - (this._col - 1))) * this._halfNodeHeight);
  144. return new n.default(i, e);
  145. }
  146. getDerectByPixel(t, e) {
  147. (t = this.getWorldPointByPixel(t, e)),
  148. (e = this.getPixelByWorldPoint(t.x, t.y)),
  149. (t = Math.floor(e.x / this._nodeWidth) - (e.x % this._nodeWidth == 0 ? 1 : 0)),
  150. (e = Math.floor(e.y / this._halfNodeHeight) - 1);
  151. return new n.default(t, e);
  152. }
  153. getDerectByWorldPoint(t, e) {
  154. var i = Math.floor((t - (e - (this._col - 1))) / 2),
  155. e = t + (e - (this._col - 1));
  156. return new n.default(i, e);
  157. }
  158. getPixelByDerect(t, e) {
  159. (t = Math.floor((t + (e % 2)) * this._nodeWidth + (1 - (e % 2)) * this._halfNodeWidth)),
  160. (e = Math.floor((e + 1) * this._halfNodeHeight));
  161. return new n.default(t, e);
  162. }
  163. }
  164. class h {
  165. constructor(t, e, i, s, o, a) {
  166. (this._row = t),
  167. (this._col = e),
  168. (this._nodeWidth = i),
  169. (this._nodeHeight = s),
  170. (this._halfNodeWidth = o),
  171. (this._halfNodeHeight = a);
  172. }
  173. getNodeByPixel(t, e) {
  174. var i = this.getWorldPointByPixel(t, e),
  175. s = this.getPixelByWorldPoint(i.x, i.y),
  176. t = this.getDerectByPixel(t, e),
  177. e = new r.default();
  178. return (e.cx = i.x), (e.cy = i.y), (e.px = s.x), (e.py = s.y), (e.dx = t.x), (e.dy = t.y), e;
  179. }
  180. getNodeByDerect(t, e) {
  181. var i = this.getPixelByDerect(t, e),
  182. s = this.getWorldPointByPixel(i.x, i.y),
  183. o = new r.default();
  184. return (o.cx = s.x), (o.cy = s.y), (o.px = i.x), (o.py = i.y), (o.dx = t), (o.dy = e), o;
  185. }
  186. getNodeByWorldPoint(t, e) {
  187. e = this.getPixelByWorldPoint(t, e);
  188. return this.getNodeByPixel(e.x, e.y);
  189. }
  190. getWorldPointByPixel(t, e) {
  191. (t = Math.floor(t / this._nodeWidth)), (e = Math.floor(e / this._nodeHeight));
  192. return new n.default(t, e);
  193. }
  194. getPixelByWorldPoint(t, e) {
  195. (t = Math.floor((t + 1) * this._nodeWidth - this._halfNodeWidth)),
  196. (e = Math.floor((e + 1) * this._nodeHeight - this._halfNodeHeight));
  197. return new n.default(t, e);
  198. }
  199. getDerectByPixel(t, e) {
  200. (t = Math.floor(t / this._nodeWidth)), (e = Math.floor(e / this._nodeHeight));
  201. return new n.default(t, e);
  202. }
  203. getDerectByWorldPoint(t, e) {
  204. return new n.default(t, e);
  205. }
  206. getPixelByDerect(t, e) {
  207. (t = Math.floor((t + 1) * this._nodeWidth - this._halfNodeWidth)),
  208. (e = Math.floor((e + 1) * this._nodeHeight - this._halfNodeHeight));
  209. return new n.default(t, e);
  210. }
  211. }
  212. class c {
  213. constructor(t, e, i, s, o, a) {
  214. (this._proportion = 1.732),
  215. (this._row = t),
  216. (this._col = e),
  217. (this._nodeWidth = i),
  218. (this._nodeHeight = s),
  219. (this._halfNodeWidth = o),
  220. (this._halfNodeHeight = a),
  221. (this._nwDiv4 = this._nodeWidth / 4),
  222. (this._radius = 4 * this._nwDiv4),
  223. (this._proportion = (2 * this._nodeHeight) / this._nodeWidth);
  224. }
  225. getNodeByPixel(t, e) {
  226. var i = this.getWorldPointByPixel(t, e),
  227. t = this.getPixelByWorldPoint(i.x, i.y),
  228. e = new r.default();
  229. return (e.cx = i.x), (e.cy = i.y), (e.px = t.x), (e.py = t.y), (e.dx = i.x), (e.dy = i.y), e;
  230. }
  231. getNodeByDerect(t, e) {
  232. var i = this.getPixelByDerect(t, e),
  233. s = new r.default();
  234. return (s.cx = t), (s.cy = e), (s.px = i.x), (s.py = i.y), (s.dx = t), (s.dy = e), s;
  235. }
  236. getNodeByWorldPoint(t, e) {
  237. e = this.getPixelByWorldPoint(t, e);
  238. return this.getNodeByPixel(e.x, e.y);
  239. }
  240. getWorldPointByPixel(t, e) {
  241. var i,
  242. s = Math.floor(t / this._nwDiv4),
  243. o = Math.floor(s / 3),
  244. a =
  245. (s - 1) % 6 == 0 || (s - 2) % 6 == 0
  246. ? ((i = o), (a = Math.floor(e / this._nodeHeight)))
  247. : (s - 4) % 6 == 0 || (s - 5) % 6 == 0
  248. ? ((i = o),
  249. (a = e < this._nodeHeight / 2 ? -1 : Math.floor((e - this._nodeHeight / 2) / this._nodeHeight)))
  250. : o % 2 == 0
  251. ? ((a = Math.floor(e / this._nodeHeight)),
  252. this.testPointInHoneycomb(o, a, t, e)
  253. ? ((i = o), a)
  254. : this.testPointInHoneycomb(o - 1, a - 1, t, e)
  255. ? ((i = o - 1), a - 1)
  256. : ((i = o - 1), a))
  257. : ((a = e < this._nodeHeight / 2 ? -1 : Math.floor((e - this._nodeHeight / 2) / this._nodeHeight)),
  258. this.testPointInHoneycomb(o, a, t, e)
  259. ? ((i = o), a)
  260. : this.testPointInHoneycomb(o - 1, a, t, e)
  261. ? ((i = o - 1), a)
  262. : ((i = o - 1), a + 1));
  263. return new n.default(i, a);
  264. }
  265. testPointInHoneycomb(t, e, i, s) {
  266. var o = 2 * this._nwDiv4,
  267. e = this.getPixelByWorldPoint(t, e);
  268. return o - Math.abs(i - e.x) >= Math.abs(s - e.y) / this._proportion;
  269. }
  270. getPixelByWorldPoint(t, e) {
  271. var i = Math.floor(((2 + 3 * t) / 4) * this._nodeWidth),
  272. t = Math.floor((e + 0.5 * (1 + (t % 2))) * this._nodeHeight);
  273. return new n.default(i, t);
  274. }
  275. getDerectByPixel(t, e) {
  276. return this.getWorldPointByPixel(t, e);
  277. }
  278. getDerectByWorldPoint(t, e) {
  279. return new n.default(t, e);
  280. }
  281. getPixelByDerect(t, e) {
  282. var i = ((2 + 3 * t) / 4) * this._nodeWidth,
  283. t = (e + 0.5 * (1 + (t % 2))) * this._nodeHeight;
  284. return new n.default(i, t);
  285. }
  286. }