package org.bukkit.util;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
/**
* 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.
*/
@SerializableAs("Vector")
public class Vector implements Cloneable, ConfigurationSerializable {
private static final long serialVersionUID = -2657651106777219169L;
private static Random random = new Random();
/**
* Threshold for fuzzy equals().
*/
private static final double epsilon = 0.000001;
protected double x;
protected double y;
protected double z;
/**
* Construct the vector with all components as 0.
*/
public Vector() {
this.x = 0;
this.y = 0;
this.z = 0;
}
/**
* Construct the vector with provided integer components.
*
* @param x X component
* @param y Y component
* @param z Z component
*/
public Vector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Construct the vector with provided double components.
*
* @param x X component
* @param y Y component
* @param z Z component
*/
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Construct the vector with provided float components.
*
* @param x X component
* @param y Y component
* @param z Z component
*/
public Vector(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Adds a vector to this one
*
* @param vec The other vector
* @return the same vector
*/
public Vector add(Vector vec) {
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
/**
* Subtracts a vector from this one.
*
* @param vec The other vector
* @return the same vector
*/
public Vector subtract(Vector vec) {
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
/**
* Multiplies the vector by another.
*
* @param vec The other vector
* @return the same vector
*/
public Vector multiply(Vector vec) {
x *= vec.x;
y *= vec.y;
z *= vec.z;
return this;
}
/**
* Divides the vector by another.
*
* @param vec The other vector
* @return the same vector
*/
public Vector divide(Vector vec) {
x /= vec.x;
y /= vec.y;
z /= vec.z;
return this;
}
/**
* Copies another vector
*
* @param vec The other vector
* @return the same vector
*/
public Vector copy(Vector vec) {
x = vec.x;
y = vec.y;
z = vec.z;
return this;
}
/**
* 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.
*
* @return the magnitude
*/
public double length() {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
}
/**
* Gets the magnitude of the vector squared.
*
* @return the magnitude
*/
public double lengthSquared() {
return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
}
/**
* 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.
*
* @param o The other vector
* @return the distance
*/
public double distance(Vector o) {
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2));
}
/**
* Get the squared distance between this vector and another.
*
* @param o The other vector
* @return the distance
*/
public double distanceSquared(Vector o) {
return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2);
}
/**
* Gets the angle between this vector and another in radians.
*
* @param other The other vector
* @return angle in radians
*/
public float angle(Vector other) {
double dot = dot(other) / (length() * other.length());
return (float) Math.acos(dot);
}
/**
* Sets this vector to the midpoint between this vector and another.
*
* @param other The other vector
* @return this same vector (now a midpoint)
*/
public Vector midpoint(Vector other) {
x = (x + other.x) / 2;
y = (y + other.y) / 2;
z = (z + other.z) / 2;
return this;
}
/**
* Gets a new midpoint vector between this vector and another.
*
* @param other The other vector
* @return a new midpoint vector
*/
public Vector getMidpoint(Vector other) {
double x = (this.x + other.x) / 2;
double y = (this.y + other.y) / 2;
double z = (this.z + other.z) / 2;
return new Vector(x, y, z);
}
/**
* Performs scalar multiplication, multiplying all components with a scalar.
*
* @param m The factor
* @return the same vector
*/
public Vector multiply(int m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Performs scalar multiplication, multiplying all components with a scalar.
*
* @param m The factor
* @return the same vector
*/
public Vector multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* Performs scalar multiplication, multiplying all components with a scalar.
*
* @param m The factor
* @return the same vector
*/
public Vector multiply(float m) {
x *= m;
y *= m;
z *= m;
return this;
}
/**
* 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.
*
* @param other The other vector
* @return dot product
*/
public double dot(Vector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Calculates the cross product of this vector with another. The cross
* product is defined as:
*