Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
Vector.java
Go to the documentation of this file.
1 package org.bukkit.util;
2 
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5 import java.util.Random;
6 import org.bukkit.Location;
7 import org.bukkit.World;
8 import org.bukkit.configuration.serialization.ConfigurationSerializable;
9 import org.bukkit.configuration.serialization.SerializableAs;
10 
17 @SerializableAs("Vector")
18 public class Vector implements Cloneable, ConfigurationSerializable {
19  private static final long serialVersionUID = -2657651106777219169L;
20 
21  private static Random random = new Random();
22 
26  private static final double epsilon = 0.000001;
27 
28  protected double x;
29  protected double y;
30  protected double z;
31 
35  public Vector() {
36  this.x = 0;
37  this.y = 0;
38  this.z = 0;
39  }
40 
48  public Vector(int x, int y, int z) {
49  this.x = x;
50  this.y = y;
51  this.z = z;
52  }
53 
61  public Vector(double x, double y, double z) {
62  this.x = x;
63  this.y = y;
64  this.z = z;
65  }
66 
74  public Vector(float x, float y, float z) {
75  this.x = x;
76  this.y = y;
77  this.z = z;
78  }
79 
86  public Vector add(Vector vec) {
87  x += vec.x;
88  y += vec.y;
89  z += vec.z;
90  return this;
91  }
92 
99  public Vector subtract(Vector vec) {
100  x -= vec.x;
101  y -= vec.y;
102  z -= vec.z;
103  return this;
104  }
105 
112  public Vector multiply(Vector vec) {
113  x *= vec.x;
114  y *= vec.y;
115  z *= vec.z;
116  return this;
117  }
118 
125  public Vector divide(Vector vec) {
126  x /= vec.x;
127  y /= vec.y;
128  z /= vec.z;
129  return this;
130  }
131 
138  public Vector copy(Vector vec) {
139  x = vec.x;
140  y = vec.y;
141  z = vec.z;
142  return this;
143  }
144 
154  public double length() {
155  return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
156  }
157 
163  public double lengthSquared() {
164  return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
165  }
166 
177  public double distance(Vector o) {
178  return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2));
179  }
180 
187  public double distanceSquared(Vector o) {
188  return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2);
189  }
190 
197  public float angle(Vector other) {
198  double dot = dot(other) / (length() * other.length());
199 
200  return (float) Math.acos(dot);
201  }
202 
209  public Vector midpoint(Vector other) {
210  x = (x + other.x) / 2;
211  y = (y + other.y) / 2;
212  z = (z + other.z) / 2;
213  return this;
214  }
215 
222  public Vector getMidpoint(Vector other) {
223  double x = (this.x + other.x) / 2;
224  double y = (this.y + other.y) / 2;
225  double z = (this.z + other.z) / 2;
226  return new Vector(x, y, z);
227  }
228 
235  public Vector multiply(int m) {
236  x *= m;
237  y *= m;
238  z *= m;
239  return this;
240  }
241 
248  public Vector multiply(double m) {
249  x *= m;
250  y *= m;
251  z *= m;
252  return this;
253  }
254 
261  public Vector multiply(float m) {
262  x *= m;
263  y *= m;
264  z *= m;
265  return this;
266  }
267 
275  public double dot(Vector other) {
276  return x * other.x + y * other.y + z * other.z;
277  }
278 
291  double newX = y * o.z - o.y * z;
292  double newY = z * o.x - o.z * x;
293  double newZ = x * o.y - o.x * y;
294 
295  x = newX;
296  y = newY;
297  z = newZ;
298  return this;
299  }
300 
306  public Vector normalize() {
307  double length = length();
308 
309  x /= length;
310  y /= length;
311  z /= length;
312 
313  return this;
314  }
315 
321  public Vector zero() {
322  x = 0;
323  y = 0;
324  z = 0;
325  return this;
326  }
327 
337  public boolean isInAABB(Vector min, Vector max) {
338  return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
339  }
340 
348  public boolean isInSphere(Vector origin, double radius) {
349  return (Math.pow(origin.x - x, 2) + Math.pow(origin.y - y, 2) + Math.pow(origin.z - z, 2)) <= Math.pow(radius, 2);
350  }
351 
357  public double getX() {
358  return x;
359  }
360 
367  public int getBlockX() {
368  return NumberConversions.floor(x);
369  }
370 
376  public double getY() {
377  return y;
378  }
379 
386  public int getBlockY() {
387  return NumberConversions.floor(y);
388  }
389 
395  public double getZ() {
396  return z;
397  }
398 
405  public int getBlockZ() {
406  return NumberConversions.floor(z);
407  }
408 
415  public Vector setX(int x) {
416  this.x = x;
417  return this;
418  }
419 
426  public Vector setX(double x) {
427  this.x = x;
428  return this;
429  }
430 
437  public Vector setX(float x) {
438  this.x = x;
439  return this;
440  }
441 
448  public Vector setY(int y) {
449  this.y = y;
450  return this;
451  }
452 
459  public Vector setY(double y) {
460  this.y = y;
461  return this;
462  }
463 
470  public Vector setY(float y) {
471  this.y = y;
472  return this;
473  }
474 
481  public Vector setZ(int z) {
482  this.z = z;
483  return this;
484  }
485 
492  public Vector setZ(double z) {
493  this.z = z;
494  return this;
495  }
496 
503  public Vector setZ(float z) {
504  this.z = z;
505  return this;
506  }
507 
515  @Override
516  public boolean equals(Object obj) {
517  if (!(obj instanceof Vector)) {
518  return false;
519  }
520 
521  Vector other = (Vector) obj;
522 
523  return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
524  }
525 
531  @Override
532  public int hashCode() {
533  int hash = 7;
534 
535  hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
536  hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
537  hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
538  return hash;
539  }
540 
546  @Override
547  public Vector clone() {
548  try {
549  return (Vector) super.clone();
550  } catch (CloneNotSupportedException e) {
551  throw new Error(e);
552  }
553  }
554 
558  @Override
559  public String toString() {
560  return x + "," + y + "," + z;
561  }
562 
569  public Location toLocation(World world) {
570  return new Location(world, x, y, z);
571  }
572 
581  public Location toLocation(World world, float yaw, float pitch) {
582  return new Location(world, x, y, z, yaw, pitch);
583  }
584 
591  return new BlockVector(x, y, z);
592  }
593 
599  public static double getEpsilon() {
600  return epsilon;
601  }
602 
610  public static Vector getMinimum(Vector v1, Vector v2) {
611  return new Vector(Math.min(v1.x, v2.x), Math.min(v1.y, v2.y), Math.min(v1.z, v2.z));
612  }
613 
621  public static Vector getMaximum(Vector v1, Vector v2) {
622  return new Vector(Math.max(v1.x, v2.x), Math.max(v1.y, v2.y), Math.max(v1.z, v2.z));
623  }
624 
631  public static Vector getRandom() {
632  return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble());
633  }
634 
635  public Map<String, Object> serialize() {
636  Map<String, Object> result = new LinkedHashMap<String, Object>();
637 
638  result.put("x", getX());
639  result.put("y", getY());
640  result.put("z", getZ());
641 
642  return result;
643  }
644 
645  public static Vector deserialize(Map<String, Object> args) {
646  double x = 0;
647  double y = 0;
648  double z = 0;
649 
650  if (args.containsKey("x")) {
651  x = (Double) args.get("x");
652  }
653  if (args.containsKey("y")) {
654  y = (Double) args.get("y");
655  }
656  if (args.containsKey("z")) {
657  z = (Double) args.get("z");
658  }
659 
660  return new Vector(x, y, z);
661  }
662 }