Plex-FAWE/src/main/java/com/sk89q/worldedit/Vector2D.java

617 lines
12 KiB
Java
Raw Normal View History

// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
*
* @author sk89q
*/
public class Vector2D {
protected final double x, z;
/**
* Construct the Vector2D object.
*
* @param x
* @param z
*/
public Vector2D(double x, double z) {
this.x = x;
this.z = z;
}
/**
* Construct the Vector2D object.
*
* @param x
* @param z
*/
public Vector2D(int x, int z) {
2011-11-23 01:29:48 +00:00
this.x = (double) x;
this.z = (double) z;
}
/**
* Construct the Vector2D object.
*
* @param x
* @param z
*/
public Vector2D(float x, float z) {
2011-11-23 01:29:48 +00:00
this.x = (double) x;
this.z = (double) z;
}
/**
* Construct the Vector2D object.
*
* @param pt
*/
public Vector2D(Vector2D pt) {
this.x = pt.x;
this.z = pt.z;
}
/**
* Construct the Vector2D object.
*/
public Vector2D() {
this.x = 0;
this.z = 0;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @return the x
*/
public int getBlockX() {
2011-09-24 19:24:10 +00:00
return (int) Math.round(x);
}
/**
* Set X.
*
* @param x
* @return new vector
*/
public Vector2D setX(double x) {
return new Vector2D(x, z);
}
/**
* Set X.
*
* @param x
* @return new vector
*/
public Vector2D setX(int x) {
return new Vector2D(x, z);
}
/**
* @return the z
*/
public double getZ() {
return z;
}
/**
* @return the z
*/
public int getBlockZ() {
2011-09-24 19:24:10 +00:00
return (int) Math.round(z);
}
/**
* Set Z.
*
* @param z
* @return new vector
*/
public Vector2D setZ(double z) {
return new Vector2D(x, z);
}
/**
* Set Z.
*
* @param z
* @return new vector
*/
public Vector2D setZ(int z) {
return new Vector2D(x, z);
}
/**
* Adds two points.
*
* @param other
* @return New point
*/
public Vector2D add(Vector2D other) {
return new Vector2D(x + other.x, z + other.z);
}
/**
* Adds two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D add(double x, double z) {
return new Vector2D(this.x + x, this.z + z);
}
/**
* Adds two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D add(int x, int z) {
return new Vector2D(this.x + x, this.z + z);
}
/**
* Adds points.
*
* @param others
* @return New point
*/
public Vector2D add(Vector2D... others) {
double newX = x, newZ = z;
for (int i = 0; i < others.length; ++i) {
newX += others[i].x;
newZ += others[i].z;
}
return new Vector2D(newX, newZ);
}
/**
* Subtracts two points.
*
* @param other
* @return New point
*/
public Vector2D subtract(Vector2D other) {
return new Vector2D(x - other.x, z - other.z);
}
/**
* Subtract two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D subtract(double x, double z) {
return new Vector2D(this.x - x, this.z - z);
}
/**
* Subtract two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D subtract(int x, int z) {
return new Vector2D(this.x - x, this.z - z);
}
/**
* Subtract points.
*
* @param others
* @return New point
*/
public Vector2D subtract(Vector2D... others) {
double newX = x, newZ = z;
for (int i = 0; i < others.length; ++i) {
newX -= others[i].x;
newZ -= others[i].z;
}
return new Vector2D(newX, newZ);
}
/**
* Multiplies two points.
*
* @param other
* @return New point
*/
public Vector2D multiply(Vector2D other) {
return new Vector2D(x * other.x, z * other.z);
}
/**
* Multiply two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D multiply(double x, double z) {
return new Vector2D(this.x * x, this.z * z);
}
/**
* Multiply two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D multiply(int x, int z) {
return new Vector2D(this.x * x, this.z * z);
}
/**
* Multiply points.
*
* @param others
* @return New point
*/
public Vector2D multiply(Vector2D... others) {
double newX = x, newZ = z;
for (int i = 0; i < others.length; ++i) {
newX *= others[i].x;
newZ *= others[i].z;
}
return new Vector2D(newX, newZ);
}
/**
* Scalar multiplication.
*
* @param n
* @return New point
*/
public Vector2D multiply(double n) {
return new Vector2D(this.x * n, this.z * n);
}
/**
* Scalar multiplication.
*
* @param n
* @return New point
*/
public Vector2D multiply(float n) {
return new Vector2D(this.x * n, this.z * n);
}
/**
* Scalar multiplication.
*
* @param n
* @return New point
*/
public Vector2D multiply(int n) {
return new Vector2D(this.x * n, this.z * n);
}
/**
* Divide two points.
*
* @param other
* @return New point
*/
public Vector2D divide(Vector2D other) {
return new Vector2D(x / other.x, z / other.z);
}
/**
* Divide two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D divide(double x, double z) {
return new Vector2D(this.x / x, this.z / z);
}
/**
* Divide two points.
*
* @param x
* @param y
* @param z
* @return New point
*/
public Vector2D divide(int x, int z) {
return new Vector2D(this.x / x, this.z / z);
}
/**
* Scalar division.
*
* @param n
* @return new point
*/
public Vector2D divide(int n) {
return new Vector2D(x / n, z / n);
}
/**
* Scalar division.
*
* @param n
* @return new point
*/
public Vector2D divide(double n) {
return new Vector2D(x / n, z / n);
}
/**
* Scalar division.
*
* @param n
* @return new point
*/
public Vector2D divide(float n) {
return new Vector2D(x / n, z / n);
}
/**
* Get the length of the vector.
*
* @return length
*/
public double length() {
return Math.sqrt(x * x + z * z);
}
/**
* Get the length^2 of the vector.
*
* @return length^2
*/
public double lengthSq() {
return x * x + z * z;
}
/**
* Get the distance away from a point.
*
* @param pt
* @return distance
*/
public double distance(Vector2D pt) {
return Math.sqrt(Math.pow(pt.x - x, 2) +
Math.pow(pt.z - z, 2));
}
/**
* Get the distance away from a point, squared.
*
* @param pt
* @return distance
*/
public double distanceSq(Vector2D pt) {
return Math.pow(pt.x - x, 2) +
Math.pow(pt.z - z, 2);
}
/**
* Get the normalized vector.
*
* @return vector
*/
public Vector2D normalize() {
return divide(length());
}
/**
* Gets the dot product of this and another vector.
*
* @param other
* @return the dot product of this and the other vector
*/
public double dot(Vector2D other) {
return x * other.x + z * other.z;
}
/**
* Checks to see if a vector is contained with another.
*
* @param min
* @param max
* @return
*/
public boolean containedWithin(Vector2D min, Vector2D max) {
return x >= min.getX() && x <= max.getX()
&& z >= min.getZ() && z <= max.getZ();
}
/**
* Checks to see if a vector is contained with another.
*
* @param min
* @param max
* @return
*/
public boolean containedWithinBlock(Vector2D min, Vector2D max) {
return getBlockX() >= min.getBlockX() && getBlockX() <= max.getBlockX()
&& getBlockZ() >= min.getBlockZ() && getBlockZ() <= max.getBlockZ();
}
/**
* Rounds all components down.
*
* @return
*/
public Vector2D floor() {
return new Vector2D(Math.floor(x), Math.floor(z));
}
/**
* Rounds all components up.
*
* @return
*/
public Vector2D ceil() {
return new Vector2D(Math.ceil(x), Math.ceil(z));
}
/**
* Rounds all components to the closest integer.
*
* @return
*/
public Vector2D round() {
return new Vector2D(Math.floor(x + 0.5), Math.floor(z + 0.5));
}
/**
* 2D transformation.
*
* @param angle in degrees
* @param aboutX about which x coordinate to rotate
* @param aboutZ about which z coordinate to rotate
* @param translateX what to add after rotation
* @param translateZ what to add after rotation
* @return
*/
public Vector2D transform2D(double angle,
double aboutX, double aboutZ, double translateX, double translateZ) {
angle = Math.toRadians(angle);
double x = this.x - aboutX;
double z = this.z - aboutZ;
double x2 = x * Math.cos(angle) - z * Math.sin(angle);
double z2 = x * Math.sin(angle) + z * Math.cos(angle);
return new Vector2D(
x2 + aboutX + translateX,
z2 + aboutZ + translateZ
);
}
2011-02-20 01:44:39 +00:00
/**
* Gets a BlockVector version.
2011-09-24 19:32:03 +00:00
*
2011-02-20 01:44:39 +00:00
* @return BlockVector
*/
public BlockVector2D toBlockVector2D() {
return new BlockVector2D(this);
}
/**
* Checks if another object is equivalent.
*
* @param obj
* @return whether the other object is equivalent
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector2D)) {
return false;
}
Vector2D other = (Vector2D) obj;
return other.x == this.x && other.z == this.z;
}
/**
* Gets the hash code.
*
* @return hash code
*/
@Override
public int hashCode() {
return ((new Double(x)).hashCode() >> 13) ^
(new Double(z)).hashCode();
}
/**
* Returns string representation "(x, y, z)".
*
* @return string
*/
@Override
public String toString() {
return "(" + x + ", " + z + ")";
}
/**
* Creates a 3D vector by adding a zero Y component to this vector.
*
* @return Vector
*/
public Vector toVector() {
return new Vector(x, 0, z);
}
2012-01-03 13:11:55 +00:00
/**
* Creates a 3D vector by adding the specified Y component to this vector.
*
* @return Vector
*/
public Vector toVector(double y) {
return new Vector(x, y, z);
}
/**
* Gets the minimum components of two vectors.
*
* @param v1
* @param v2
* @return minimum
*/
public static Vector2D getMinimum(Vector2D v1, Vector2D v2) {
return new Vector2D(
Math.min(v1.x, v2.x),
Math.min(v1.z, v2.z)
);
}
/**
* Gets the maximum components of two vectors.
*
* @param v1
* @param v2
* @return maximum
*/
public static Vector2D getMaximum(Vector2D v1, Vector2D v2) {
return new Vector2D(
Math.max(v1.x, v2.x),
Math.max(v1.z, v2.z)
);
}
}