GeoUtils.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. import com.l2jserver.gameserver.geoengine.Direction;
  21. /**
  22. * @author FBIagent
  23. */
  24. public final class GeoUtils
  25. {
  26. public interface PointListener
  27. {
  28. /**
  29. * @param x
  30. * @param y
  31. * @return true proceed, false abort
  32. */
  33. boolean onPoint(int x, int y);
  34. }
  35. public static boolean forEachLinePoint(int srcX, int srcY, int dstX, int dstY, PointListener listener)
  36. {
  37. int dx = Math.abs(dstX - srcX), sx = srcX < dstX ? 1 : -1;
  38. int dy = -Math.abs(dstY - srcY), sy = srcY < dstY ? 1 : -1;
  39. int err = dx + dy, e2;
  40. for (;;)
  41. {
  42. if (!listener.onPoint(srcX, srcY))
  43. {
  44. return false;
  45. }
  46. if ((srcX == dstX) && (srcY == dstY))
  47. {
  48. break;
  49. }
  50. e2 = 2 * err;
  51. if (e2 > dy)
  52. {
  53. err += dy;
  54. srcX += sx;
  55. }
  56. if (e2 < dx)
  57. {
  58. err += dx;
  59. srcY += sy;
  60. }
  61. }
  62. return true;
  63. }
  64. /**
  65. * difference between x values: never abover 1<br>
  66. * difference between y values: never above 1
  67. * @param lastX
  68. * @param lastY
  69. * @param x
  70. * @param y
  71. * @return
  72. */
  73. public static Direction computeDirection(int lastX, int lastY, int x, int y)
  74. {
  75. if (x > lastX) // east
  76. {
  77. if (y > lastY)
  78. {
  79. return Direction.SOUTH_EAST;
  80. }
  81. else if (y < lastY)
  82. {
  83. return Direction.NORTH_EAST;
  84. }
  85. else
  86. {
  87. return Direction.EAST;
  88. }
  89. }
  90. else if (x < lastX) // west
  91. {
  92. if (y > lastY)
  93. {
  94. return Direction.SOUTH_WEST;
  95. }
  96. else if (y < lastY)
  97. {
  98. return Direction.NORTH_WEST;
  99. }
  100. else
  101. {
  102. return Direction.WEST;
  103. }
  104. }
  105. else
  106. // unchanged x
  107. {
  108. if (y > lastY)
  109. {
  110. return Direction.SOUTH;
  111. }
  112. else if (y < lastY)
  113. {
  114. return Direction.NORTH;
  115. }
  116. else
  117. {
  118. return null;// error, should never happen, TODO: Logging
  119. }
  120. }
  121. }
  122. }