LinePointIterator3D.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2004-2015 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 HorridoJoho
  22. */
  23. public final class LinePointIterator3D
  24. {
  25. private int _srcX;
  26. private int _srcY;
  27. private int _srcZ;
  28. private final int _dstX;
  29. private final int _dstY;
  30. private final int _dstZ;
  31. private final long _dx;
  32. private final long _dy;
  33. private final long _dz;
  34. private final long _sx;
  35. private final long _sy;
  36. private final long _sz;
  37. private long _error;
  38. private long _error2;
  39. private boolean _first;
  40. public LinePointIterator3D(int srcX, int srcY, int srcZ, int dstX, int dstY, int dstZ)
  41. {
  42. _srcX = srcX;
  43. _srcY = srcY;
  44. _srcZ = srcZ;
  45. _dstX = dstX;
  46. _dstY = dstY;
  47. _dstZ = dstZ;
  48. _dx = Math.abs((long) dstX - srcX);
  49. _dy = Math.abs((long) dstY - srcY);
  50. _dz = Math.abs((long) dstZ - srcZ);
  51. _sx = srcX < dstX ? 1 : -1;
  52. _sy = srcY < dstY ? 1 : -1;
  53. _sz = srcZ < dstZ ? 1 : -1;
  54. if ((_dx >= _dy) && (_dx >= _dz))
  55. {
  56. _error = _error2 = _dx / 2;
  57. }
  58. else if ((_dy >= _dx) && (_dy >= _dz))
  59. {
  60. _error = _error2 = _dy / 2;
  61. }
  62. else
  63. {
  64. _error = _error2 = _dz / 2;
  65. }
  66. _first = true;
  67. }
  68. public boolean next()
  69. {
  70. if (_first)
  71. {
  72. _first = false;
  73. return true;
  74. }
  75. else if ((_dx >= _dy) && (_dx >= _dz))
  76. {
  77. if (_srcX != _dstX)
  78. {
  79. _srcX += _sx;
  80. _error += _dy;
  81. if (_error >= _dx)
  82. {
  83. _srcY += _sy;
  84. _error -= _dx;
  85. }
  86. _error2 += _dz;
  87. if (_error2 >= _dx)
  88. {
  89. _srcZ += _sz;
  90. _error2 -= _dx;
  91. }
  92. return true;
  93. }
  94. }
  95. else if ((_dy >= _dx) && (_dy >= _dz))
  96. {
  97. if (_srcY != _dstY)
  98. {
  99. _srcY += _sy;
  100. _error += _dx;
  101. if (_error >= _dy)
  102. {
  103. _srcX += _sx;
  104. _error -= _dy;
  105. }
  106. _error2 += _dz;
  107. if (_error2 >= _dy)
  108. {
  109. _srcZ += _sz;
  110. _error2 -= _dy;
  111. }
  112. return true;
  113. }
  114. }
  115. else
  116. {
  117. if (_srcZ != _dstZ)
  118. {
  119. _srcZ += _sz;
  120. _error += _dx;
  121. if (_error >= _dz)
  122. {
  123. _srcX += _sx;
  124. _error -= _dz;
  125. }
  126. _error2 += _dy;
  127. if (_error2 >= _dz)
  128. {
  129. _srcY += _sy;
  130. _error2 -= _dz;
  131. }
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. public int x()
  138. {
  139. return _srcX;
  140. }
  141. public int y()
  142. {
  143. return _srcY;
  144. }
  145. public int z()
  146. {
  147. return _srcZ;
  148. }
  149. }