Point3D.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * $Header: Point3D.java, 19/07/2005 21:33:07 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 19/07/2005 21:33:07 $
  6. * $Revision: 1 $
  7. * $Log: Point3D.java,v $
  8. * Revision 1 19/07/2005 21:33:07 luisantonioa
  9. * Added copyright notice
  10. *
  11. *
  12. * This program is free software: you can redistribute it and/or modify it under
  13. * the terms of the GNU General Public License as published by the Free Software
  14. * Foundation, either version 3 of the License, or (at your option) any later
  15. * version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. package com.l2jserver.gameserver.util;
  26. import java.io.Serializable;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  31. */
  32. public class Point3D implements Serializable
  33. {
  34. /**
  35. * Comment for <code>serialVersionUID</code>
  36. */
  37. private static final long serialVersionUID = 4638345252031872576L;
  38. /* since using volatile vars, removing all synchronizations */
  39. private volatile int _x, _y, _z;
  40. public Point3D(int pX, int pY, int pZ)
  41. {
  42. _x = pX;
  43. _y = pY;
  44. _z = pZ;
  45. }
  46. public Point3D(int pX, int pY)
  47. {
  48. _x = pX;
  49. _y = pY;
  50. _z = 0;
  51. }
  52. /**
  53. * @param worldPosition
  54. */
  55. public Point3D(Point3D worldPosition)
  56. {
  57. _x = worldPosition._x;
  58. _y = worldPosition._y;
  59. _z = worldPosition._z;
  60. }
  61. public synchronized void setTo(Point3D point)
  62. {
  63. _x = point._x;
  64. _y = point._y;
  65. _z = point._z;
  66. }
  67. @Override
  68. public String toString()
  69. {
  70. return "(" + _x + ", " + _y + ", " + _z + ")";
  71. }
  72. @Override
  73. public int hashCode()
  74. {
  75. return _x ^ _y ^ _z;
  76. }
  77. @Override
  78. public boolean equals(Object o)
  79. {
  80. if (o instanceof Point3D)
  81. {
  82. Point3D point3D = (Point3D) o;
  83. boolean ret = point3D._x == _x && point3D._y == _y && point3D._z == _z;
  84. return ret;
  85. }
  86. return false;
  87. }
  88. public boolean equals(int pX, int pY, int pZ)
  89. {
  90. return _x == pX && _y == pY && _z == pZ;
  91. }
  92. public long distanceSquaredTo(Point3D point)
  93. {
  94. long dx, dy;
  95. dx = _x - point._x;
  96. dy = _y - point._y;
  97. return (dx * dx) + (dy * dy);
  98. }
  99. public static long distanceSquared(Point3D point1, Point3D point2)
  100. {
  101. long dx, dy;
  102. dx = point1._x - point2._x;
  103. dy = point1._y - point2._y;
  104. return (dx * dx) + (dy * dy);
  105. }
  106. public static boolean distanceLessThan(Point3D point1, Point3D point2, double distance)
  107. {
  108. return distanceSquared(point1, point2) < distance * distance;
  109. }
  110. public int getX()
  111. {
  112. return _x;
  113. }
  114. public synchronized void setX(int pX)
  115. {
  116. _x = pX;
  117. }
  118. public int getY()
  119. {
  120. return _y;
  121. }
  122. public synchronized void setY(int pY)
  123. {
  124. _y = pY;
  125. }
  126. public int getZ()
  127. {
  128. return _z;
  129. }
  130. public synchronized void setZ(int pZ)
  131. {
  132. _z = pZ;
  133. }
  134. public synchronized void setXYZ(int pX, int pY, int pZ)
  135. {
  136. _x = pX;
  137. _y = pY;
  138. _z = pZ;
  139. }
  140. /* note by Deedlit: we are using volatile variable types here. We dont need to additionally
  141. * use synchronized, cause volatile vars are synced vars. Removed all, please test if all is ok. Need to
  142. * look through code and remove similar pointless lockings if any more there. */
  143. }