Removed Point/BlockPoint to Vector/BlockVector.
This commit is contained in:
389
src/com/sk89q/worldedit/Vector.java
Normal file
389
src/com/sk89q/worldedit/Vector.java
Normal file
@@ -0,0 +1,389 @@
|
||||
// $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 Albert
|
||||
*/
|
||||
public class Vector {
|
||||
protected final double x, y, z;
|
||||
|
||||
/**
|
||||
* Construct the Vector object.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
*/
|
||||
public Vector(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the Vector object.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
*/
|
||||
public Vector(int x, int y, int z) {
|
||||
this.x = (double)x;
|
||||
this.y = (double)y;
|
||||
this.z = (double)z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the Vector object.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
*/
|
||||
public Vector(float x, float y, float z) {
|
||||
this.x = (double)x;
|
||||
this.y = (double)y;
|
||||
this.z = (double)z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the Vector object.
|
||||
*
|
||||
* @param pt
|
||||
*/
|
||||
public Vector(Vector pt) {
|
||||
this.x = pt.x;
|
||||
this.y = pt.y;
|
||||
this.z = pt.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the Vector object.
|
||||
*/
|
||||
public Vector() {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.z = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the x
|
||||
*/
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the x
|
||||
*/
|
||||
public int getBlockX() {
|
||||
return (int)x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the y
|
||||
*/
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the y
|
||||
*/
|
||||
public int getBlockY() {
|
||||
return (int)y;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the z
|
||||
*/
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the z
|
||||
*/
|
||||
public int getBlockZ() {
|
||||
return (int)z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two points.
|
||||
*
|
||||
* @param other
|
||||
* @return New point
|
||||
*/
|
||||
public Vector add(Vector other) {
|
||||
return new Vector(x + other.x, y + other.y, z + other.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector add(double x, double y, double z) {
|
||||
return new Vector(this.x + x, this.y + y, this.z + z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector add(int x, int y, int z) {
|
||||
return new Vector(this.x + x, this.y + y, this.z + z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds points.
|
||||
*
|
||||
* @param others
|
||||
* @return New point
|
||||
*/
|
||||
public Vector add(Vector ... others) {
|
||||
double newX = x, newY = y, newZ = z;
|
||||
|
||||
for (int i = 0; i < others.length; i++) {
|
||||
newX += others[i].x;
|
||||
newY += others[i].y;
|
||||
newZ += others[i].z;
|
||||
}
|
||||
return new Vector(newX, newY, newZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts two points.
|
||||
*
|
||||
* @param other
|
||||
* @return New point
|
||||
*/
|
||||
public Vector subtract(Vector other) {
|
||||
return new Vector(x - other.x, y - other.y, z - other.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector subtract(double x, double y, double z) {
|
||||
return new Vector(this.x - x, this.y - y, this.z - z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector subtract(int x, int y, int z) {
|
||||
return new Vector(this.x - x, this.y - y, this.z - z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract points.
|
||||
*
|
||||
* @param others
|
||||
* @return New point
|
||||
*/
|
||||
public Vector subtract(Vector ... others) {
|
||||
double newX = x, newY = y, newZ = z;
|
||||
|
||||
for (int i = 0; i < others.length; i++) {
|
||||
newX -= others[i].x;
|
||||
newY -= others[i].y;
|
||||
newZ -= others[i].z;
|
||||
}
|
||||
return new Vector(newX, newY, newZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies two points.
|
||||
*
|
||||
* @param other
|
||||
* @return New point
|
||||
*/
|
||||
public Vector multiply(Vector other) {
|
||||
return new Vector(x * other.x, y * other.y, z * other.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector multiply(double x, double y, double z) {
|
||||
return new Vector(this.x * x, this.y * y, this.z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector multiply(int x, int y, int z) {
|
||||
return new Vector(this.x * x, this.y * y, this.z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply points.
|
||||
*
|
||||
* @param others
|
||||
* @return New point
|
||||
*/
|
||||
public Vector multiply(Vector ... others) {
|
||||
double newX = x, newY = y, newZ = z;
|
||||
|
||||
for (int i = 0; i < others.length; i++) {
|
||||
newX *= others[i].x;
|
||||
newY *= others[i].y;
|
||||
newZ *= others[i].z;
|
||||
}
|
||||
return new Vector(newX, newY, newZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide two points.
|
||||
*
|
||||
* @param other
|
||||
* @return New point
|
||||
*/
|
||||
public Vector divide(Vector other) {
|
||||
return new Vector(x / other.x, y / other.y, z / other.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector divide(double x, double y, double z) {
|
||||
return new Vector(this.x / x, this.y / y, this.z / z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide two points.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return New point
|
||||
*/
|
||||
public Vector divide(int x, int y, int z) {
|
||||
return new Vector(this.x / x, this.y / y, this.z / z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the distance away from a point.
|
||||
*
|
||||
* @param pt
|
||||
* @return distance
|
||||
*/
|
||||
public double distance(Vector pt) {
|
||||
return Math.sqrt(Math.pow(pt.x - x, 2) +
|
||||
Math.pow(pt.y - y, 2) +
|
||||
Math.pow(pt.z - z, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a block point from a point.
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return point
|
||||
*/
|
||||
public static Vector toBlockPoint(double x, double y, double z) {
|
||||
return new Vector((int)Math.floor(x),
|
||||
(int)Math.floor(y),
|
||||
(int)Math.floor(z));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if another object is equivalent.
|
||||
*
|
||||
* @param obj
|
||||
* @return whether the other object is equivalent
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Vector)) {
|
||||
return false;
|
||||
}
|
||||
Vector other = (Vector)obj;
|
||||
return other.x == this.x && other.y == this.y && other.z == this.z;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the hash code.
|
||||
*
|
||||
* @return hash code
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return ((new Double(x)).hashCode() >> 13) ^
|
||||
((new Double(y)).hashCode() >> 7) ^
|
||||
(new Double(z)).hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string representation "(x, y, z)".
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + x + ", " + y + ", " + z + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a BlockVector version.
|
||||
*
|
||||
* @return BlockVector
|
||||
*/
|
||||
public BlockVector toBlockPoint() {
|
||||
return new BlockVector(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user