2
0

LinePointIterator.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.util;
  20. /**
  21. * @author FBIagent
  22. */
  23. public final class LinePointIterator
  24. {
  25. // src is moved towards dst in next()
  26. private int _srcX;
  27. private int _srcY;
  28. private final int _dstX;
  29. private final int _dstY;
  30. private final int _dx;
  31. private final int _dy;
  32. private final int _sx;
  33. private final int _sy;
  34. private int _err;
  35. private int _e2;
  36. private boolean _first;
  37. public LinePointIterator(int srcX, int srcY, int dstX, int dstY)
  38. {
  39. _srcX = srcX;
  40. _srcY = srcY;
  41. _dstX = dstX;
  42. _dstY = dstY;
  43. _dx = Math.abs(dstX - srcX);
  44. _sx = srcX < dstX ? 1 : -1;
  45. _dy = -Math.abs(dstY - srcY);
  46. _sy = srcY < dstY ? 1 : -1;
  47. _err = _dx + _dy;
  48. _e2 = 0;
  49. _first = true;
  50. }
  51. public boolean next()
  52. {
  53. if (_first)
  54. {
  55. _first = false;
  56. return true;
  57. }
  58. else if ((_srcX != _dstX) || (_srcY != _dstY))
  59. {
  60. _e2 = 2 * _err;
  61. if (_e2 > _dy)
  62. {
  63. _err += _dy;
  64. _srcX += _sx;
  65. }
  66. if (_e2 < _dx)
  67. {
  68. _err += _dx;
  69. _srcY += _sy;
  70. }
  71. return true;
  72. }
  73. return false;
  74. }
  75. public int x()
  76. {
  77. return _srcX;
  78. }
  79. public int y()
  80. {
  81. return _srcY;
  82. }
  83. }