mxPerimeter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxPerimeter =
  6. {
  7. /**
  8. * Class: mxPerimeter
  9. *
  10. * Provides various perimeter functions to be used in a style
  11. * as the value of <mxConstants.STYLE_PERIMETER>. Perimeters for
  12. * rectangle, circle, rhombus and triangle are available.
  13. *
  14. * Example:
  15. *
  16. * (code)
  17. * <add as="perimeter">mxPerimeter.RectanglePerimeter</add>
  18. * (end)
  19. *
  20. * Or programmatically:
  21. *
  22. * (code)
  23. * style[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter;
  24. * (end)
  25. *
  26. * When adding new perimeter functions, it is recommended to use the
  27. * mxPerimeter-namespace as follows:
  28. *
  29. * (code)
  30. * mxPerimeter.CustomPerimeter = function (bounds, vertex, next, orthogonal)
  31. * {
  32. * var x = 0; // Calculate x-coordinate
  33. * var y = 0; // Calculate y-coordainte
  34. *
  35. * return new mxPoint(x, y);
  36. * }
  37. * (end)
  38. *
  39. * The new perimeter should then be registered in the <mxStyleRegistry> as follows:
  40. * (code)
  41. * mxStyleRegistry.putValue('customPerimeter', mxPerimeter.CustomPerimeter);
  42. * (end)
  43. *
  44. * The custom perimeter above can now be used in a specific vertex as follows:
  45. *
  46. * (code)
  47. * model.setStyle(vertex, 'perimeter=customPerimeter');
  48. * (end)
  49. *
  50. * Note that the key of the <mxStyleRegistry> entry for the function should
  51. * be used in string values, unless <mxGraphView.allowEval> is true, in
  52. * which case you can also use mxPerimeter.CustomPerimeter for the value in
  53. * the cell style above.
  54. *
  55. * Or it can be used for all vertices in the graph as follows:
  56. *
  57. * (code)
  58. * var style = graph.getStylesheet().getDefaultVertexStyle();
  59. * style[mxConstants.STYLE_PERIMETER] = mxPerimeter.CustomPerimeter;
  60. * (end)
  61. *
  62. * Note that the object can be used directly when programmatically setting
  63. * the value, but the key in the <mxStyleRegistry> should be used when
  64. * setting the value via a key, value pair in a cell style.
  65. *
  66. * The parameters are explained in <RectanglePerimeter>.
  67. *
  68. * Function: RectanglePerimeter
  69. *
  70. * Describes a rectangular perimeter for the given bounds.
  71. *
  72. * Parameters:
  73. *
  74. * bounds - <mxRectangle> that represents the absolute bounds of the
  75. * vertex.
  76. * vertex - <mxCellState> that represents the vertex.
  77. * next - <mxPoint> that represents the nearest neighbour point on the
  78. * given edge.
  79. * orthogonal - Boolean that specifies if the orthogonal projection onto
  80. * the perimeter should be returned. If this is false then the intersection
  81. * of the perimeter and the line between the next and the center point is
  82. * returned.
  83. */
  84. RectanglePerimeter: function (bounds, vertex, next, orthogonal)
  85. {
  86. var cx = bounds.getCenterX();
  87. var cy = bounds.getCenterY();
  88. var dx = next.x - cx;
  89. var dy = next.y - cy;
  90. var alpha = Math.atan2(dy, dx);
  91. var p = new mxPoint(0, 0);
  92. var pi = Math.PI;
  93. var pi2 = Math.PI/2;
  94. var beta = pi2 - alpha;
  95. var t = Math.atan2(bounds.height, bounds.width);
  96. if (alpha < -pi + t || alpha > pi - t)
  97. {
  98. // Left edge
  99. p.x = bounds.x;
  100. p.y = cy - bounds.width * Math.tan(alpha) / 2;
  101. }
  102. else if (alpha < -t)
  103. {
  104. // Top Edge
  105. p.y = bounds.y;
  106. p.x = cx - bounds.height * Math.tan(beta) / 2;
  107. }
  108. else if (alpha < t)
  109. {
  110. // Right Edge
  111. p.x = bounds.x + bounds.width;
  112. p.y = cy + bounds.width * Math.tan(alpha) / 2;
  113. }
  114. else
  115. {
  116. // Bottom Edge
  117. p.y = bounds.y + bounds.height;
  118. p.x = cx + bounds.height * Math.tan(beta) / 2;
  119. }
  120. if (orthogonal)
  121. {
  122. if (next.x >= bounds.x &&
  123. next.x <= bounds.x + bounds.width)
  124. {
  125. p.x = next.x;
  126. }
  127. else if (next.y >= bounds.y &&
  128. next.y <= bounds.y + bounds.height)
  129. {
  130. p.y = next.y;
  131. }
  132. if (next.x < bounds.x)
  133. {
  134. p.x = bounds.x;
  135. }
  136. else if (next.x > bounds.x + bounds.width)
  137. {
  138. p.x = bounds.x + bounds.width;
  139. }
  140. if (next.y < bounds.y)
  141. {
  142. p.y = bounds.y;
  143. }
  144. else if (next.y > bounds.y + bounds.height)
  145. {
  146. p.y = bounds.y + bounds.height;
  147. }
  148. }
  149. return p;
  150. },
  151. /**
  152. * Function: EllipsePerimeter
  153. *
  154. * Describes an elliptic perimeter. See <RectanglePerimeter>
  155. * for a description of the parameters.
  156. */
  157. EllipsePerimeter: function (bounds, vertex, next, orthogonal)
  158. {
  159. var x = bounds.x;
  160. var y = bounds.y;
  161. var a = bounds.width / 2;
  162. var b = bounds.height / 2;
  163. var cx = x + a;
  164. var cy = y + b;
  165. var px = next.x;
  166. var py = next.y;
  167. // Calculates straight line equation through
  168. // point and ellipse center y = d * x + h
  169. var dx = parseInt(px - cx);
  170. var dy = parseInt(py - cy);
  171. if (dx == 0 && dy != 0)
  172. {
  173. return new mxPoint(cx, cy + b * dy / Math.abs(dy));
  174. }
  175. else if (dx == 0 && dy == 0)
  176. {
  177. return new mxPoint(px, py);
  178. }
  179. if (orthogonal)
  180. {
  181. if (py >= y && py <= y + bounds.height)
  182. {
  183. var ty = py - cy;
  184. var tx = Math.sqrt(a*a*(1-(ty*ty)/(b*b))) || 0;
  185. if (px <= x)
  186. {
  187. tx = -tx;
  188. }
  189. return new mxPoint(cx+tx, py);
  190. }
  191. if (px >= x && px <= x + bounds.width)
  192. {
  193. var tx = px - cx;
  194. var ty = Math.sqrt(b*b*(1-(tx*tx)/(a*a))) || 0;
  195. if (py <= y)
  196. {
  197. ty = -ty;
  198. }
  199. return new mxPoint(px, cy+ty);
  200. }
  201. }
  202. // Calculates intersection
  203. var d = dy / dx;
  204. var h = cy - d * cx;
  205. var e = a * a * d * d + b * b;
  206. var f = -2 * cx * e;
  207. var g = a * a * d * d * cx * cx +
  208. b * b * cx * cx -
  209. a * a * b * b;
  210. var det = Math.sqrt(f * f - 4 * e * g);
  211. // Two solutions (perimeter points)
  212. var xout1 = (-f + det) / (2 * e);
  213. var xout2 = (-f - det) / (2 * e);
  214. var yout1 = d * xout1 + h;
  215. var yout2 = d * xout2 + h;
  216. var dist1 = Math.sqrt(Math.pow((xout1 - px), 2)
  217. + Math.pow((yout1 - py), 2));
  218. var dist2 = Math.sqrt(Math.pow((xout2 - px), 2)
  219. + Math.pow((yout2 - py), 2));
  220. // Correct solution
  221. var xout = 0;
  222. var yout = 0;
  223. if (dist1 < dist2)
  224. {
  225. xout = xout1;
  226. yout = yout1;
  227. }
  228. else
  229. {
  230. xout = xout2;
  231. yout = yout2;
  232. }
  233. return new mxPoint(xout, yout);
  234. },
  235. /**
  236. * Function: RhombusPerimeter
  237. *
  238. * Describes a rhombus (aka diamond) perimeter. See <RectanglePerimeter>
  239. * for a description of the parameters.
  240. */
  241. RhombusPerimeter: function (bounds, vertex, next, orthogonal)
  242. {
  243. var x = bounds.x;
  244. var y = bounds.y;
  245. var w = bounds.width;
  246. var h = bounds.height;
  247. var cx = x + w / 2;
  248. var cy = y + h / 2;
  249. var px = next.x;
  250. var py = next.y;
  251. // Special case for intersecting the diamond's corners
  252. if (cx == px)
  253. {
  254. if (cy > py)
  255. {
  256. return new mxPoint(cx, y); // top
  257. }
  258. else
  259. {
  260. return new mxPoint(cx, y + h); // bottom
  261. }
  262. }
  263. else if (cy == py)
  264. {
  265. if (cx > px)
  266. {
  267. return new mxPoint(x, cy); // left
  268. }
  269. else
  270. {
  271. return new mxPoint(x + w, cy); // right
  272. }
  273. }
  274. var tx = cx;
  275. var ty = cy;
  276. if (orthogonal)
  277. {
  278. if (px >= x && px <= x + w)
  279. {
  280. tx = px;
  281. }
  282. else if (py >= y && py <= y + h)
  283. {
  284. ty = py;
  285. }
  286. }
  287. // In which quadrant will the intersection be?
  288. // set the slope and offset of the border line accordingly
  289. if (px < cx)
  290. {
  291. if (py < cy)
  292. {
  293. return mxUtils.intersection(px, py, tx, ty, cx, y, x, cy);
  294. }
  295. else
  296. {
  297. return mxUtils.intersection(px, py, tx, ty, cx, y + h, x, cy);
  298. }
  299. }
  300. else if (py < cy)
  301. {
  302. return mxUtils.intersection(px, py, tx, ty, cx, y, x + w, cy);
  303. }
  304. else
  305. {
  306. return mxUtils.intersection(px, py, tx, ty, cx, y + h, x + w, cy);
  307. }
  308. },
  309. /**
  310. * Function: TrianglePerimeter
  311. *
  312. * Describes a triangle perimeter. See <RectanglePerimeter>
  313. * for a description of the parameters.
  314. */
  315. TrianglePerimeter: function (bounds, vertex, next, orthogonal)
  316. {
  317. var direction = (vertex != null) ?
  318. vertex.style[mxConstants.STYLE_DIRECTION] : null;
  319. var vertical = direction == mxConstants.DIRECTION_NORTH ||
  320. direction == mxConstants.DIRECTION_SOUTH;
  321. var x = bounds.x;
  322. var y = bounds.y;
  323. var w = bounds.width;
  324. var h = bounds.height;
  325. var cx = x + w / 2;
  326. var cy = y + h / 2;
  327. var start = new mxPoint(x, y);
  328. var corner = new mxPoint(x + w, cy);
  329. var end = new mxPoint(x, y + h);
  330. if (direction == mxConstants.DIRECTION_NORTH)
  331. {
  332. start = end;
  333. corner = new mxPoint(cx, y);
  334. end = new mxPoint(x + w, y + h);
  335. }
  336. else if (direction == mxConstants.DIRECTION_SOUTH)
  337. {
  338. corner = new mxPoint(cx, y + h);
  339. end = new mxPoint(x + w, y);
  340. }
  341. else if (direction == mxConstants.DIRECTION_WEST)
  342. {
  343. start = new mxPoint(x + w, y);
  344. corner = new mxPoint(x, cy);
  345. end = new mxPoint(x + w, y + h);
  346. }
  347. var dx = next.x - cx;
  348. var dy = next.y - cy;
  349. var alpha = (vertical) ? Math.atan2(dx, dy) : Math.atan2(dy, dx);
  350. var t = (vertical) ? Math.atan2(w, h) : Math.atan2(h, w);
  351. var base = false;
  352. if (direction == mxConstants.DIRECTION_NORTH ||
  353. direction == mxConstants.DIRECTION_WEST)
  354. {
  355. base = alpha > -t && alpha < t;
  356. }
  357. else
  358. {
  359. base = alpha < -Math.PI + t || alpha > Math.PI - t;
  360. }
  361. var result = null;
  362. if (base)
  363. {
  364. if (orthogonal && ((vertical && next.x >= start.x && next.x <= end.x) ||
  365. (!vertical && next.y >= start.y && next.y <= end.y)))
  366. {
  367. if (vertical)
  368. {
  369. result = new mxPoint(next.x, start.y);
  370. }
  371. else
  372. {
  373. result = new mxPoint(start.x, next.y);
  374. }
  375. }
  376. else
  377. {
  378. if (direction == mxConstants.DIRECTION_NORTH)
  379. {
  380. result = new mxPoint(x + w / 2 + h * Math.tan(alpha) / 2,
  381. y + h);
  382. }
  383. else if (direction == mxConstants.DIRECTION_SOUTH)
  384. {
  385. result = new mxPoint(x + w / 2 - h * Math.tan(alpha) / 2,
  386. y);
  387. }
  388. else if (direction == mxConstants.DIRECTION_WEST)
  389. {
  390. result = new mxPoint(x + w, y + h / 2 +
  391. w * Math.tan(alpha) / 2);
  392. }
  393. else
  394. {
  395. result = new mxPoint(x, y + h / 2 -
  396. w * Math.tan(alpha) / 2);
  397. }
  398. }
  399. }
  400. else
  401. {
  402. if (orthogonal)
  403. {
  404. var pt = new mxPoint(cx, cy);
  405. if (next.y >= y && next.y <= y + h)
  406. {
  407. pt.x = (vertical) ? cx : (
  408. (direction == mxConstants.DIRECTION_WEST) ?
  409. x + w : x);
  410. pt.y = next.y;
  411. }
  412. else if (next.x >= x && next.x <= x + w)
  413. {
  414. pt.x = next.x;
  415. pt.y = (!vertical) ? cy : (
  416. (direction == mxConstants.DIRECTION_NORTH) ?
  417. y + h : y);
  418. }
  419. // Compute angle
  420. dx = next.x - pt.x;
  421. dy = next.y - pt.y;
  422. cx = pt.x;
  423. cy = pt.y;
  424. }
  425. if ((vertical && next.x <= x + w / 2) ||
  426. (!vertical && next.y <= y + h / 2))
  427. {
  428. result = mxUtils.intersection(next.x, next.y, cx, cy,
  429. start.x, start.y, corner.x, corner.y);
  430. }
  431. else
  432. {
  433. result = mxUtils.intersection(next.x, next.y, cx, cy,
  434. corner.x, corner.y, end.x, end.y);
  435. }
  436. }
  437. if (result == null)
  438. {
  439. result = new mxPoint(cx, cy);
  440. }
  441. return result;
  442. },
  443. /**
  444. * Function: HexagonPerimeter
  445. *
  446. * Describes a hexagon perimeter. See <RectanglePerimeter>
  447. * for a description of the parameters.
  448. */
  449. HexagonPerimeter: function (bounds, vertex, next, orthogonal)
  450. {
  451. var x = bounds.x;
  452. var y = bounds.y;
  453. var w = bounds.width;
  454. var h = bounds.height;
  455. var cx = bounds.getCenterX();
  456. var cy = bounds.getCenterY();
  457. var px = next.x;
  458. var py = next.y;
  459. var dx = px - cx;
  460. var dy = py - cy;
  461. var alpha = -Math.atan2(dy, dx);
  462. var pi = Math.PI;
  463. var pi2 = Math.PI / 2;
  464. var result = new mxPoint(cx, cy);
  465. var direction = (vertex != null) ? mxUtils.getValue(
  466. vertex.style, mxConstants.STYLE_DIRECTION,
  467. mxConstants.DIRECTION_EAST) : mxConstants.DIRECTION_EAST;
  468. var vertical = direction == mxConstants.DIRECTION_NORTH
  469. || direction == mxConstants.DIRECTION_SOUTH;
  470. var a = new mxPoint();
  471. var b = new mxPoint();
  472. //Only consider corrects quadrants for the orthogonal case.
  473. if ((px < x) && (py < y) || (px < x) && (py > y + h)
  474. || (px > x + w) && (py < y) || (px > x + w) && (py > y + h))
  475. {
  476. orthogonal = false;
  477. }
  478. if (orthogonal)
  479. {
  480. if (vertical)
  481. {
  482. //Special cases where intersects with hexagon corners
  483. if (px == cx)
  484. {
  485. if (py <= y)
  486. {
  487. return new mxPoint(cx, y);
  488. }
  489. else if (py >= y + h)
  490. {
  491. return new mxPoint(cx, y + h);
  492. }
  493. }
  494. else if (px < x)
  495. {
  496. if (py == y + h / 4)
  497. {
  498. return new mxPoint(x, y + h / 4);
  499. }
  500. else if (py == y + 3 * h / 4)
  501. {
  502. return new mxPoint(x, y + 3 * h / 4);
  503. }
  504. }
  505. else if (px > x + w)
  506. {
  507. if (py == y + h / 4)
  508. {
  509. return new mxPoint(x + w, y + h / 4);
  510. }
  511. else if (py == y + 3 * h / 4)
  512. {
  513. return new mxPoint(x + w, y + 3 * h / 4);
  514. }
  515. }
  516. else if (px == x)
  517. {
  518. if (py < cy)
  519. {
  520. return new mxPoint(x, y + h / 4);
  521. }
  522. else if (py > cy)
  523. {
  524. return new mxPoint(x, y + 3 * h / 4);
  525. }
  526. }
  527. else if (px == x + w)
  528. {
  529. if (py < cy)
  530. {
  531. return new mxPoint(x + w, y + h / 4);
  532. }
  533. else if (py > cy)
  534. {
  535. return new mxPoint(x + w, y + 3 * h / 4);
  536. }
  537. }
  538. if (py == y)
  539. {
  540. return new mxPoint(cx, y);
  541. }
  542. else if (py == y + h)
  543. {
  544. return new mxPoint(cx, y + h);
  545. }
  546. if (px < cx)
  547. {
  548. if ((py > y + h / 4) && (py < y + 3 * h / 4))
  549. {
  550. a = new mxPoint(x, y);
  551. b = new mxPoint(x, y + h);
  552. }
  553. else if (py < y + h / 4)
  554. {
  555. a = new mxPoint(x - Math.floor(0.5 * w), y
  556. + Math.floor(0.5 * h));
  557. b = new mxPoint(x + w, y - Math.floor(0.25 * h));
  558. }
  559. else if (py > y + 3 * h / 4)
  560. {
  561. a = new mxPoint(x - Math.floor(0.5 * w), y
  562. + Math.floor(0.5 * h));
  563. b = new mxPoint(x + w, y + Math.floor(1.25 * h));
  564. }
  565. }
  566. else if (px > cx)
  567. {
  568. if ((py > y + h / 4) && (py < y + 3 * h / 4))
  569. {
  570. a = new mxPoint(x + w, y);
  571. b = new mxPoint(x + w, y + h);
  572. }
  573. else if (py < y + h / 4)
  574. {
  575. a = new mxPoint(x, y - Math.floor(0.25 * h));
  576. b = new mxPoint(x + Math.floor(1.5 * w), y
  577. + Math.floor(0.5 * h));
  578. }
  579. else if (py > y + 3 * h / 4)
  580. {
  581. a = new mxPoint(x + Math.floor(1.5 * w), y
  582. + Math.floor(0.5 * h));
  583. b = new mxPoint(x, y + Math.floor(1.25 * h));
  584. }
  585. }
  586. }
  587. else
  588. {
  589. //Special cases where intersects with hexagon corners
  590. if (py == cy)
  591. {
  592. if (px <= x)
  593. {
  594. return new mxPoint(x, y + h / 2);
  595. }
  596. else if (px >= x + w)
  597. {
  598. return new mxPoint(x + w, y + h / 2);
  599. }
  600. }
  601. else if (py < y)
  602. {
  603. if (px == x + w / 4)
  604. {
  605. return new mxPoint(x + w / 4, y);
  606. }
  607. else if (px == x + 3 * w / 4)
  608. {
  609. return new mxPoint(x + 3 * w / 4, y);
  610. }
  611. }
  612. else if (py > y + h)
  613. {
  614. if (px == x + w / 4)
  615. {
  616. return new mxPoint(x + w / 4, y + h);
  617. }
  618. else if (px == x + 3 * w / 4)
  619. {
  620. return new mxPoint(x + 3 * w / 4, y + h);
  621. }
  622. }
  623. else if (py == y)
  624. {
  625. if (px < cx)
  626. {
  627. return new mxPoint(x + w / 4, y);
  628. }
  629. else if (px > cx)
  630. {
  631. return new mxPoint(x + 3 * w / 4, y);
  632. }
  633. }
  634. else if (py == y + h)
  635. {
  636. if (px < cx)
  637. {
  638. return new mxPoint(x + w / 4, y + h);
  639. }
  640. else if (py > cy)
  641. {
  642. return new mxPoint(x + 3 * w / 4, y + h);
  643. }
  644. }
  645. if (px == x)
  646. {
  647. return new mxPoint(x, cy);
  648. }
  649. else if (px == x + w)
  650. {
  651. return new mxPoint(x + w, cy);
  652. }
  653. if (py < cy)
  654. {
  655. if ((px > x + w / 4) && (px < x + 3 * w / 4))
  656. {
  657. a = new mxPoint(x, y);
  658. b = new mxPoint(x + w, y);
  659. }
  660. else if (px < x + w / 4)
  661. {
  662. a = new mxPoint(x - Math.floor(0.25 * w), y + h);
  663. b = new mxPoint(x + Math.floor(0.5 * w), y
  664. - Math.floor(0.5 * h));
  665. }
  666. else if (px > x + 3 * w / 4)
  667. {
  668. a = new mxPoint(x + Math.floor(0.5 * w), y
  669. - Math.floor(0.5 * h));
  670. b = new mxPoint(x + Math.floor(1.25 * w), y + h);
  671. }
  672. }
  673. else if (py > cy)
  674. {
  675. if ((px > x + w / 4) && (px < x + 3 * w / 4))
  676. {
  677. a = new mxPoint(x, y + h);
  678. b = new mxPoint(x + w, y + h);
  679. }
  680. else if (px < x + w / 4)
  681. {
  682. a = new mxPoint(x - Math.floor(0.25 * w), y);
  683. b = new mxPoint(x + Math.floor(0.5 * w), y
  684. + Math.floor(1.5 * h));
  685. }
  686. else if (px > x + 3 * w / 4)
  687. {
  688. a = new mxPoint(x + Math.floor(0.5 * w), y
  689. + Math.floor(1.5 * h));
  690. b = new mxPoint(x + Math.floor(1.25 * w), y);
  691. }
  692. }
  693. }
  694. var tx = cx;
  695. var ty = cy;
  696. if (px >= x && px <= x + w)
  697. {
  698. tx = px;
  699. if (py < cy)
  700. {
  701. ty = y + h;
  702. }
  703. else
  704. {
  705. ty = y;
  706. }
  707. }
  708. else if (py >= y && py <= y + h)
  709. {
  710. ty = py;
  711. if (px < cx)
  712. {
  713. tx = x + w;
  714. }
  715. else
  716. {
  717. tx = x;
  718. }
  719. }
  720. result = mxUtils.intersection(tx, ty, next.x, next.y, a.x, a.y, b.x, b.y);
  721. }
  722. else
  723. {
  724. if (vertical)
  725. {
  726. var beta = Math.atan2(h / 4, w / 2);
  727. //Special cases where intersects with hexagon corners
  728. if (alpha == beta)
  729. {
  730. return new mxPoint(x + w, y + Math.floor(0.25 * h));
  731. }
  732. else if (alpha == pi2)
  733. {
  734. return new mxPoint(x + Math.floor(0.5 * w), y);
  735. }
  736. else if (alpha == (pi - beta))
  737. {
  738. return new mxPoint(x, y + Math.floor(0.25 * h));
  739. }
  740. else if (alpha == -beta)
  741. {
  742. return new mxPoint(x + w, y + Math.floor(0.75 * h));
  743. }
  744. else if (alpha == (-pi2))
  745. {
  746. return new mxPoint(x + Math.floor(0.5 * w), y + h);
  747. }
  748. else if (alpha == (-pi + beta))
  749. {
  750. return new mxPoint(x, y + Math.floor(0.75 * h));
  751. }
  752. if ((alpha < beta) && (alpha > -beta))
  753. {
  754. a = new mxPoint(x + w, y);
  755. b = new mxPoint(x + w, y + h);
  756. }
  757. else if ((alpha > beta) && (alpha < pi2))
  758. {
  759. a = new mxPoint(x, y - Math.floor(0.25 * h));
  760. b = new mxPoint(x + Math.floor(1.5 * w), y
  761. + Math.floor(0.5 * h));
  762. }
  763. else if ((alpha > pi2) && (alpha < (pi - beta)))
  764. {
  765. a = new mxPoint(x - Math.floor(0.5 * w), y
  766. + Math.floor(0.5 * h));
  767. b = new mxPoint(x + w, y - Math.floor(0.25 * h));
  768. }
  769. else if (((alpha > (pi - beta)) && (alpha <= pi))
  770. || ((alpha < (-pi + beta)) && (alpha >= -pi)))
  771. {
  772. a = new mxPoint(x, y);
  773. b = new mxPoint(x, y + h);
  774. }
  775. else if ((alpha < -beta) && (alpha > -pi2))
  776. {
  777. a = new mxPoint(x + Math.floor(1.5 * w), y
  778. + Math.floor(0.5 * h));
  779. b = new mxPoint(x, y + Math.floor(1.25 * h));
  780. }
  781. else if ((alpha < -pi2) && (alpha > (-pi + beta)))
  782. {
  783. a = new mxPoint(x - Math.floor(0.5 * w), y
  784. + Math.floor(0.5 * h));
  785. b = new mxPoint(x + w, y + Math.floor(1.25 * h));
  786. }
  787. }
  788. else
  789. {
  790. var beta = Math.atan2(h / 2, w / 4);
  791. //Special cases where intersects with hexagon corners
  792. if (alpha == beta)
  793. {
  794. return new mxPoint(x + Math.floor(0.75 * w), y);
  795. }
  796. else if (alpha == (pi - beta))
  797. {
  798. return new mxPoint(x + Math.floor(0.25 * w), y);
  799. }
  800. else if ((alpha == pi) || (alpha == -pi))
  801. {
  802. return new mxPoint(x, y + Math.floor(0.5 * h));
  803. }
  804. else if (alpha == 0)
  805. {
  806. return new mxPoint(x + w, y + Math.floor(0.5 * h));
  807. }
  808. else if (alpha == -beta)
  809. {
  810. return new mxPoint(x + Math.floor(0.75 * w), y + h);
  811. }
  812. else if (alpha == (-pi + beta))
  813. {
  814. return new mxPoint(x + Math.floor(0.25 * w), y + h);
  815. }
  816. if ((alpha > 0) && (alpha < beta))
  817. {
  818. a = new mxPoint(x + Math.floor(0.5 * w), y
  819. - Math.floor(0.5 * h));
  820. b = new mxPoint(x + Math.floor(1.25 * w), y + h);
  821. }
  822. else if ((alpha > beta) && (alpha < (pi - beta)))
  823. {
  824. a = new mxPoint(x, y);
  825. b = new mxPoint(x + w, y);
  826. }
  827. else if ((alpha > (pi - beta)) && (alpha < pi))
  828. {
  829. a = new mxPoint(x - Math.floor(0.25 * w), y + h);
  830. b = new mxPoint(x + Math.floor(0.5 * w), y
  831. - Math.floor(0.5 * h));
  832. }
  833. else if ((alpha < 0) && (alpha > -beta))
  834. {
  835. a = new mxPoint(x + Math.floor(0.5 * w), y
  836. + Math.floor(1.5 * h));
  837. b = new mxPoint(x + Math.floor(1.25 * w), y);
  838. }
  839. else if ((alpha < -beta) && (alpha > (-pi + beta)))
  840. {
  841. a = new mxPoint(x, y + h);
  842. b = new mxPoint(x + w, y + h);
  843. }
  844. else if ((alpha < (-pi + beta)) && (alpha > -pi))
  845. {
  846. a = new mxPoint(x - Math.floor(0.25 * w), y);
  847. b = new mxPoint(x + Math.floor(0.5 * w), y
  848. + Math.floor(1.5 * h));
  849. }
  850. }
  851. result = mxUtils.intersection(cx, cy, next.x, next.y, a.x, a.y, b.x, b.y);
  852. }
  853. if (result == null)
  854. {
  855. return new mxPoint(cx, cy);
  856. }
  857. return result;
  858. }
  859. };