Point3D.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.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. private volatile int _x, _y, _z;
  39. public Point3D(int pX, int pY, int pZ)
  40. {
  41. _x = pX;
  42. _y = pY;
  43. _z = pZ;
  44. }
  45. public Point3D(int pX, int pY)
  46. {
  47. _x = pX;
  48. _y = pY;
  49. _z = 0;
  50. }
  51. /**
  52. * @param worldPosition
  53. */
  54. public Point3D(Point3D worldPosition)
  55. {
  56. synchronized (worldPosition)
  57. {
  58. _x = worldPosition._x;
  59. _y = worldPosition._y;
  60. _z = worldPosition._z;
  61. }
  62. }
  63. public synchronized void setTo(Point3D point)
  64. {
  65. synchronized (point)
  66. {
  67. _x = point._x;
  68. _y = point._y;
  69. _z = point._z;
  70. }
  71. }
  72. @Override
  73. public String toString()
  74. {
  75. return "(" + _x + ", " + _y + ", " + _z + ")";
  76. }
  77. @Override
  78. public int hashCode()
  79. {
  80. return _x ^ _y ^ _z;
  81. }
  82. @Override
  83. public synchronized boolean equals(Object o)
  84. {
  85. if (o instanceof Point3D)
  86. {
  87. Point3D point3D = (Point3D) o;
  88. boolean ret;
  89. synchronized (point3D)
  90. {
  91. ret = point3D._x == _x && point3D._y == _y && point3D._z == _z;
  92. }
  93. return ret;
  94. }
  95. return false;
  96. }
  97. public synchronized boolean equals(int pX, int pY, int pZ)
  98. {
  99. return _x == pX && _y == pY && _z == pZ;
  100. }
  101. public synchronized long distanceSquaredTo(Point3D point)
  102. {
  103. long dx, dy;
  104. synchronized (point)
  105. {
  106. dx = _x - point._x;
  107. dy = _y - point._y;
  108. }
  109. return (dx * dx) + (dy * dy);
  110. }
  111. public static long distanceSquared(Point3D point1, Point3D point2)
  112. {
  113. long dx, dy;
  114. synchronized (point1)
  115. {
  116. synchronized (point2)
  117. {
  118. dx = point1._x - point2._x;
  119. dy = point1._y - point2._y;
  120. }
  121. }
  122. return (dx * dx) + (dy * dy);
  123. }
  124. public static boolean distanceLessThan(Point3D point1, Point3D point2,
  125. double distance)
  126. {
  127. return distanceSquared(point1, point2) < distance * distance;
  128. }
  129. public synchronized int getX()
  130. {
  131. return _x;
  132. }
  133. public synchronized void setX(int pX)
  134. {
  135. _x = pX;
  136. }
  137. public synchronized int getY()
  138. {
  139. return _y;
  140. }
  141. public synchronized void setY(int pY)
  142. {
  143. _y = pY;
  144. }
  145. public synchronized int getZ()
  146. {
  147. return _z;
  148. }
  149. public synchronized void setZ(int pZ)
  150. {
  151. _z = pZ;
  152. }
  153. public synchronized void setXYZ(int pX, int pY, int pZ)
  154. {
  155. _x = pX;
  156. _y = pY;
  157. _z = pZ;
  158. }
  159. }