Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.util.Vector Class Reference
Inheritance diagram for org.bukkit.util.Vector:
org.bukkit.configuration.serialization.ConfigurationSerializable org.bukkit.util.BlockVector

Public Member Functions

 Vector ()
 
 Vector (int x, int y, int z)
 
 Vector (double x, double y, double z)
 
 Vector (float x, float y, float z)
 
Vector add (Vector vec)
 
Vector subtract (Vector vec)
 
Vector multiply (Vector vec)
 
Vector divide (Vector vec)
 
Vector copy (Vector vec)
 
double length ()
 
double lengthSquared ()
 
double distance (Vector o)
 
double distanceSquared (Vector o)
 
float angle (Vector other)
 
Vector midpoint (Vector other)
 
Vector getMidpoint (Vector other)
 
Vector multiply (int m)
 
Vector multiply (double m)
 
Vector multiply (float m)
 
double dot (Vector other)
 
Vector crossProduct (Vector o)
 
Vector normalize ()
 
Vector zero ()
 
boolean isInAABB (Vector min, Vector max)
 
boolean isInSphere (Vector origin, double radius)
 
double getX ()
 
int getBlockX ()
 
double getY ()
 
int getBlockY ()
 
double getZ ()
 
int getBlockZ ()
 
Vector setX (int x)
 
Vector setX (double x)
 
Vector setX (float x)
 
Vector setY (int y)
 
Vector setY (double y)
 
Vector setY (float y)
 
Vector setZ (int z)
 
Vector setZ (double z)
 
Vector setZ (float z)
 
boolean equals (Object obj)
 
int hashCode ()
 
Vector clone ()
 
String toString ()
 
Location toLocation (World world)
 
Location toLocation (World world, float yaw, float pitch)
 
BlockVector toBlockVector ()
 
Map< String, Object > serialize ()
 

Static Public Member Functions

static double getEpsilon ()
 
static Vector getMinimum (Vector v1, Vector v2)
 
static Vector getMaximum (Vector v1, Vector v2)
 
static Vector getRandom ()
 
static Vector deserialize (Map< String, Object > args)
 

Protected Attributes

double x
 
double y
 
double z
 

Detailed Description

Represents a mutable vector. Because the components of Vectors are mutable, storing Vectors long term may be dangerous if passing code modifies the Vector later. If you want to keep around a Vector, it may be wise to call clone() in order to get a copy.

Definition at line 18 of file Vector.java.

Constructor & Destructor Documentation

org.bukkit.util.Vector.Vector ( )

Construct the vector with all components as 0.

Definition at line 35 of file Vector.java.

35  {
36  this.x = 0;
37  this.y = 0;
38  this.z = 0;
39  }
org.bukkit.util.Vector.Vector ( int  x,
int  y,
int  z 
)

Construct the vector with provided integer components.

Parameters
xX component
yY component
zZ component

Definition at line 48 of file Vector.java.

48  {
49  this.x = x;
50  this.y = y;
51  this.z = z;
52  }
org.bukkit.util.Vector.Vector ( double  x,
double  y,
double  z 
)

Construct the vector with provided double components.

Parameters
xX component
yY component
zZ component

Definition at line 61 of file Vector.java.

61  {
62  this.x = x;
63  this.y = y;
64  this.z = z;
65  }
org.bukkit.util.Vector.Vector ( float  x,
float  y,
float  z 
)

Construct the vector with provided float components.

Parameters
xX component
yY component
zZ component

Definition at line 74 of file Vector.java.

74  {
75  this.x = x;
76  this.y = y;
77  this.z = z;
78  }

Member Function Documentation

Vector org.bukkit.util.Vector.add ( Vector  vec)

Adds a vector to this one

Parameters
vecThe other vector
Returns
the same vector

Definition at line 86 of file Vector.java.

86  {
87  x += vec.x;
88  y += vec.y;
89  z += vec.z;
90  return this;
91  }
float org.bukkit.util.Vector.angle ( Vector  other)

Gets the angle between this vector and another in radians.

Parameters
otherThe other vector
Returns
angle in radians

Definition at line 197 of file Vector.java.

References org.bukkit.util.Vector.length().

197  {
198  double dot = dot(other) / (length() * other.length());
199 
200  return (float) Math.acos(dot);
201  }
double dot(Vector other)
Definition: Vector.java:278
Vector org.bukkit.util.Vector.clone ( )

Get a new vector.

Returns
vector

Definition at line 552 of file Vector.java.

References org.bukkit.util.Vector.clone().

Referenced by org.bukkit.util.BlockIterator.BlockIterator(), org.bukkit.util.Vector.clone(), org.bukkit.event.entity.EntityPortalExitEvent.getAfter(), org.bukkit.event.entity.EntityPortalExitEvent.getBefore(), org.bukkit.event.block.BlockDispenseEvent.getVelocity(), and org.bukkit.event.entity.EntityPortalExitEvent.setAfter().

552  {
553  try {
554  return (Vector) super.clone();
555  } catch (CloneNotSupportedException e) {
556  throw new Error(e);
557  }
558  }
Vector org.bukkit.util.Vector.copy ( Vector  vec)

Copies another vector

Parameters
vecThe other vector
Returns
the same vector

Definition at line 138 of file Vector.java.

138  {
139  x = vec.x;
140  y = vec.y;
141  z = vec.z;
142  return this;
143  }
Vector org.bukkit.util.Vector.crossProduct ( Vector  o)

Calculates the cross product of this vector with another. The cross product is defined as:

  • x = y1 * z2 - y2 * z1
  • y = z1 * x2 - z2 * x1
  • z = x1 * y2 - x2 * y1
Parameters
oThe other vector
Returns
the same vector

Definition at line 294 of file Vector.java.

294  {
295  double newX = y * o.z - o.y * z;
296  double newY = z * o.x - o.z * x;
297  double newZ = x * o.y - o.x * y;
298 
299  x = newX;
300  y = newY;
301  z = newZ;
302  return this;
303  }
double org.bukkit.util.Vector.distance ( Vector  o)

Get the distance between this vector and another. The value of this method is not cached and uses a costly square-root function, so do not repeatedly call this method to get the vector's magnitude. NaN will be returned if the inner result of the sqrt() function overflows, which will be caused if the distance is too long.

Parameters
oThe other vector
Returns
the distance

Definition at line 177 of file Vector.java.

177  {
178  return Math.sqrt(NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z));
179  }
double org.bukkit.util.Vector.distanceSquared ( Vector  o)

Get the squared distance between this vector and another.

Parameters
oThe other vector
Returns
the distance

Definition at line 187 of file Vector.java.

187  {
188  return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z);
189  }
Vector org.bukkit.util.Vector.divide ( Vector  vec)

Divides the vector by another.

Parameters
vecThe other vector
Returns
the same vector

Definition at line 125 of file Vector.java.

125  {
126  x /= vec.x;
127  y /= vec.y;
128  z /= vec.z;
129  return this;
130  }
double org.bukkit.util.Vector.dot ( Vector  other)

Calculates the dot product of this vector with another. The dot product is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.

Parameters
otherThe other vector
Returns
dot product

Definition at line 278 of file Vector.java.

278  {
279  return x * other.x + y * other.y + z * other.z;
280  }
boolean org.bukkit.util.Vector.equals ( Object  obj)

Checks to see if two objects are equal.

Only two Vectors can ever return true. This method uses a fuzzy match to account for floating point errors. The epsilon can be retrieved with epsilon.

Definition at line 521 of file Vector.java.

521  {
522  if (!(obj instanceof Vector)) {
523  return false;
524  }
525 
526  Vector other = (Vector) obj;
527 
528  return Math.abs(x - other.x) < epsilon && Math.abs(y - other.y) < epsilon && Math.abs(z - other.z) < epsilon && (this.getClass().equals(obj.getClass()));
529  }
int org.bukkit.util.Vector.getBlockX ( )

Gets the floored value of the X component, indicating the block that this vector is contained with.

Returns
block X

Definition at line 372 of file Vector.java.

372  {
373  return NumberConversions.floor(x);
374  }
int org.bukkit.util.Vector.getBlockY ( )

Gets the floored value of the Y component, indicating the block that this vector is contained with.

Returns
block y

Definition at line 391 of file Vector.java.

391  {
392  return NumberConversions.floor(y);
393  }
int org.bukkit.util.Vector.getBlockZ ( )

Gets the floored value of the Z component, indicating the block that this vector is contained with.

Returns
block z

Definition at line 410 of file Vector.java.

410  {
411  return NumberConversions.floor(z);
412  }
static double org.bukkit.util.Vector.getEpsilon ( )
static

Get the threshold used for equals().

Returns
The epsilon.

Definition at line 604 of file Vector.java.

604  {
605  return epsilon;
606  }
static Vector org.bukkit.util.Vector.getMaximum ( Vector  v1,
Vector  v2 
)
static

Gets the maximum components of two vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
maximum

Definition at line 626 of file Vector.java.

626  {
627  return new Vector(Math.max(v1.x, v2.x), Math.max(v1.y, v2.y), Math.max(v1.z, v2.z));
628  }
Vector org.bukkit.util.Vector.getMidpoint ( Vector  other)

Gets a new midpoint vector between this vector and another.

Parameters
otherThe other vector
Returns
a new midpoint vector

Definition at line 222 of file Vector.java.

222  {
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  }
static Vector org.bukkit.util.Vector.getMinimum ( Vector  v1,
Vector  v2 
)
static

Gets the minimum components of two vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
minimum

Definition at line 615 of file Vector.java.

615  {
616  return new Vector(Math.min(v1.x, v2.x), Math.min(v1.y, v2.y), Math.min(v1.z, v2.z));
617  }
static Vector org.bukkit.util.Vector.getRandom ( )
static

Gets a random vector with components having a random value between 0 and 1.

Returns
A random vector.

Definition at line 636 of file Vector.java.

636  {
637  return new Vector(random.nextDouble(), random.nextDouble(), random.nextDouble());
638  }
double org.bukkit.util.Vector.getX ( )
double org.bukkit.util.Vector.getY ( )
double org.bukkit.util.Vector.getZ ( )
int org.bukkit.util.Vector.hashCode ( )

Returns a hash code for this vector

Returns
hash code

Definition at line 537 of file Vector.java.

537  {
538  int hash = 7;
539 
540  hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
541  hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
542  hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
543  return hash;
544  }
boolean org.bukkit.util.Vector.isInAABB ( Vector  min,
Vector  max 
)

Returns whether this vector is in an axis-aligned bounding box.

The minimum and maximum vectors given must be truly the minimum and maximum X, Y and Z components.

Parameters
minMinimum vector
maxMaximum vector
Returns
whether this vector is in the AABB

Definition at line 342 of file Vector.java.

342  {
343  return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
344  }
boolean org.bukkit.util.Vector.isInSphere ( Vector  origin,
double  radius 
)

Returns whether this vector is within a sphere.

Parameters
originSphere origin.
radiusSphere radius
Returns
whether this vector is in the sphere

Definition at line 353 of file Vector.java.

353  {
354  return (NumberConversions.square(origin.x - x) + NumberConversions.square(origin.y - y) + NumberConversions.square(origin.z - z)) <= NumberConversions.square(radius);
355  }
double org.bukkit.util.Vector.length ( )

Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The value of this method is not cached and uses a costly square-root function, so do not repeatedly call this method to get the vector's magnitude. NaN will be returned if the inner result of the sqrt() function overflows, which will be caused if the length is too long.

Returns
the magnitude

Definition at line 154 of file Vector.java.

Referenced by org.bukkit.util.Vector.angle().

154  {
155  return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z));
156  }
double org.bukkit.util.Vector.lengthSquared ( )

Gets the magnitude of the vector squared.

Returns
the magnitude

Definition at line 163 of file Vector.java.

163  {
164  return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z);
165  }
Vector org.bukkit.util.Vector.midpoint ( Vector  other)

Sets this vector to the midpoint between this vector and another.

Parameters
otherThe other vector
Returns
this same vector (now a midpoint)

Definition at line 209 of file Vector.java.

209  {
210  x = (x + other.x) / 2;
211  y = (y + other.y) / 2;
212  z = (z + other.z) / 2;
213  return this;
214  }
Vector org.bukkit.util.Vector.multiply ( Vector  vec)

Multiplies the vector by another.

Parameters
vecThe other vector
Returns
the same vector

Definition at line 112 of file Vector.java.

112  {
113  x *= vec.x;
114  y *= vec.y;
115  z *= vec.z;
116  return this;
117  }
Vector org.bukkit.util.Vector.multiply ( int  m)

Performs scalar multiplication, multiplying all components with a scalar.

Parameters
mThe factor
Returns
the same vector

Definition at line 236 of file Vector.java.

236  {
237  x *= m;
238  y *= m;
239  z *= m;
240  return this;
241  }
Vector org.bukkit.util.Vector.multiply ( double  m)

Performs scalar multiplication, multiplying all components with a scalar.

Parameters
mThe factor
Returns
the same vector

Definition at line 250 of file Vector.java.

250  {
251  x *= m;
252  y *= m;
253  z *= m;
254  return this;
255  }
Vector org.bukkit.util.Vector.multiply ( float  m)

Performs scalar multiplication, multiplying all components with a scalar.

Parameters
mThe factor
Returns
the same vector

Definition at line 264 of file Vector.java.

264  {
265  x *= m;
266  y *= m;
267  z *= m;
268  return this;
269  }
Vector org.bukkit.util.Vector.normalize ( )

Converts this vector to a unit vector (a vector with length of 1).

Returns
the same vector

Definition at line 310 of file Vector.java.

310  {
311  double length = length();
312 
313  x /= length;
314  y /= length;
315  z /= length;
316 
317  return this;
318  }
Map<String, Object> org.bukkit.util.Vector.serialize ( )

Creates a Map representation of this class.

This class must provide a method to restore this class, as defined in the ConfigurationSerializable interface javadocs.

Returns
Map containing the current state of this class

Implements org.bukkit.configuration.serialization.ConfigurationSerializable.

Definition at line 640 of file Vector.java.

640  {
641  Map<String, Object> result = new LinkedHashMap<String, Object>();
642 
643  result.put("x", getX());
644  result.put("y", getY());
645  result.put("z", getZ());
646 
647  return result;
648  }
Vector org.bukkit.util.Vector.setX ( int  x)

Set the X component.

Parameters
xThe new X component.
Returns
This vector.

Definition at line 420 of file Vector.java.

Referenced by org.bukkit.Location.getDirection().

420  {
421  this.x = x;
422  return this;
423  }
Vector org.bukkit.util.Vector.setX ( double  x)

Set the X component.

Parameters
xThe new X component.
Returns
This vector.

Definition at line 431 of file Vector.java.

431  {
432  this.x = x;
433  return this;
434  }
Vector org.bukkit.util.Vector.setX ( float  x)

Set the X component.

Parameters
xThe new X component.
Returns
This vector.

Definition at line 442 of file Vector.java.

442  {
443  this.x = x;
444  return this;
445  }
Vector org.bukkit.util.Vector.setY ( int  y)

Set the Y component.

Parameters
yThe new Y component.
Returns
This vector.

Definition at line 453 of file Vector.java.

Referenced by org.bukkit.util.BlockIterator.BlockIterator(), and org.bukkit.Location.getDirection().

453  {
454  this.y = y;
455  return this;
456  }
Vector org.bukkit.util.Vector.setY ( double  y)

Set the Y component.

Parameters
yThe new Y component.
Returns
This vector.

Definition at line 464 of file Vector.java.

464  {
465  this.y = y;
466  return this;
467  }
Vector org.bukkit.util.Vector.setY ( float  y)

Set the Y component.

Parameters
yThe new Y component.
Returns
This vector.

Definition at line 475 of file Vector.java.

475  {
476  this.y = y;
477  return this;
478  }
Vector org.bukkit.util.Vector.setZ ( int  z)

Set the Z component.

Parameters
zThe new Z component.
Returns
This vector.

Definition at line 486 of file Vector.java.

Referenced by org.bukkit.Location.getDirection().

486  {
487  this.z = z;
488  return this;
489  }
Vector org.bukkit.util.Vector.setZ ( double  z)

Set the Z component.

Parameters
zThe new Z component.
Returns
This vector.

Definition at line 497 of file Vector.java.

497  {
498  this.z = z;
499  return this;
500  }
Vector org.bukkit.util.Vector.setZ ( float  z)

Set the Z component.

Parameters
zThe new Z component.
Returns
This vector.

Definition at line 508 of file Vector.java.

508  {
509  this.z = z;
510  return this;
511  }
Vector org.bukkit.util.Vector.subtract ( Vector  vec)

Subtracts a vector from this one.

Parameters
vecThe other vector
Returns
the same vector

Definition at line 99 of file Vector.java.

99  {
100  x -= vec.x;
101  y -= vec.y;
102  z -= vec.z;
103  return this;
104  }
BlockVector org.bukkit.util.Vector.toBlockVector ( )

Get the block vector of this vector.

Returns
A block vector.

Definition at line 595 of file Vector.java.

595  {
596  return new BlockVector(x, y, z);
597  }
Location org.bukkit.util.Vector.toLocation ( World  world)

Gets a Location version of this vector with yaw and pitch being 0.

Parameters
worldThe world to link the location to.
Returns
the location

Definition at line 574 of file Vector.java.

574  {
575  return new Location(world, x, y, z);
576  }
Location org.bukkit.util.Vector.toLocation ( World  world,
float  yaw,
float  pitch 
)

Gets a Location version of this vector.

Parameters
worldThe world to link the location to.
yawThe desired yaw.
pitchThe desired pitch.
Returns
the location

Definition at line 586 of file Vector.java.

586  {
587  return new Location(world, x, y, z, yaw, pitch);
588  }
String org.bukkit.util.Vector.toString ( )

Returns this vector's components as x,y,z.

Definition at line 564 of file Vector.java.

564  {
565  return x + "," + y + "," + z;
566  }
Vector org.bukkit.util.Vector.zero ( )

Zero this vector's components.

Returns
the same vector

Definition at line 325 of file Vector.java.

325  {
326  x = 0;
327  y = 0;
328  z = 0;
329  return this;
330  }

The documentation for this class was generated from the following file: