2010-10-02 21:52:42 +00:00
|
|
|
// $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
|
|
|
|
*/
|
2010-10-11 18:17:32 +00:00
|
|
|
public class Point {
|
|
|
|
protected final double x, y, z;
|
2010-10-02 21:52:42 +00:00
|
|
|
|
2010-10-03 19:43:30 +00:00
|
|
|
/**
|
|
|
|
* Construct the Point object.
|
2010-10-11 08:22:47 +00:00
|
|
|
*
|
2010-10-03 19:43:30 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
|
|
|
*/
|
2010-10-11 08:22:47 +00:00
|
|
|
public Point(double x, double y, double z) {
|
2010-10-02 21:52:42 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = z;
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Construct the Point object.
|
|
|
|
*
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
|
|
|
*/
|
|
|
|
public Point(int x, int y, int z) {
|
|
|
|
this.x = (double)x;
|
|
|
|
this.y = (double)y;
|
|
|
|
this.z = (double)z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the Point object.
|
|
|
|
*
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
|
|
|
*/
|
|
|
|
public Point(float x, float y, float z) {
|
|
|
|
this.x = (double)x;
|
|
|
|
this.y = (double)y;
|
|
|
|
this.z = (double)z;
|
|
|
|
}
|
|
|
|
|
2010-10-11 18:17:32 +00:00
|
|
|
/**
|
|
|
|
* Construct the Point object.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
*/
|
|
|
|
public Point(Point pt) {
|
|
|
|
this.x = pt.x;
|
|
|
|
this.y = pt.y;
|
|
|
|
this.z = pt.z;
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Construct the Point object.
|
|
|
|
*/
|
|
|
|
public Point() {
|
|
|
|
this.x = 0;
|
|
|
|
this.y = 0;
|
|
|
|
this.z = 0;
|
|
|
|
}
|
|
|
|
|
2010-10-02 21:52:42 +00:00
|
|
|
/**
|
|
|
|
* @return the x
|
|
|
|
*/
|
2010-10-11 08:22:47 +00:00
|
|
|
public double getX() {
|
2010-10-02 21:52:42 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* @return the x
|
|
|
|
*/
|
|
|
|
public int getBlockX() {
|
|
|
|
return (int)x;
|
|
|
|
}
|
2010-10-02 21:52:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the y
|
|
|
|
*/
|
2010-10-11 08:22:47 +00:00
|
|
|
public double getY() {
|
2010-10-02 21:52:42 +00:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* @return the y
|
|
|
|
*/
|
|
|
|
public int getBlockY() {
|
|
|
|
return (int)y;
|
|
|
|
}
|
|
|
|
|
2010-10-02 21:52:42 +00:00
|
|
|
/**
|
|
|
|
* @return the z
|
|
|
|
*/
|
2010-10-11 08:22:47 +00:00
|
|
|
public double getZ() {
|
2010-10-02 21:52:42 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* @return the z
|
|
|
|
*/
|
|
|
|
public int getBlockZ() {
|
|
|
|
return (int)z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds two points.
|
|
|
|
*
|
|
|
|
* @param other
|
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point add(Point other) {
|
|
|
|
return new Point(x + other.x, y + other.y, z + other.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point add(double x, double y, double z) {
|
|
|
|
return new Point(this.x + x, this.y + y, this.z + z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point add(int x, int y, int z) {
|
|
|
|
return new Point(this.x + x, this.y + y, this.z + z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param others
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point add(Point ... 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 Point(newX, newY, newZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts two points.
|
|
|
|
*
|
|
|
|
* @param other
|
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point subtract(Point other) {
|
|
|
|
return new Point(x - other.x, y - other.y, z - other.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point subtract(double x, double y, double z) {
|
|
|
|
return new Point(this.x - x, this.y - y, this.z - z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point subtract(int x, int y, int z) {
|
|
|
|
return new Point(this.x - x, this.y - y, this.z - z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param others
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point subtract(Point ... 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 Point(newX, newY, newZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiplies two points.
|
|
|
|
*
|
|
|
|
* @param other
|
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point multiply(Point other) {
|
|
|
|
return new Point(x * other.x, y * other.y, z * other.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point multiply(double x, double y, double z) {
|
|
|
|
return new Point(this.x * x, this.y * y, this.z * z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point multiply(int x, int y, int z) {
|
|
|
|
return new Point(this.x * x, this.y * y, this.z * z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param others
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point multiply(Point ... 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 Point(newX, newY, newZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide two points.
|
|
|
|
*
|
|
|
|
* @param other
|
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point divide(Point other) {
|
|
|
|
return new Point(x / other.x, y / other.y, z / other.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point divide(double x, double y, double z) {
|
|
|
|
return new Point(this.x / x, this.y / y, this.z / z);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divide two points.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 08:22:47 +00:00
|
|
|
* @return New point
|
|
|
|
*/
|
|
|
|
public Point divide(int x, int y, int z) {
|
|
|
|
return new Point(this.x / x, this.y / y, this.z / z);
|
|
|
|
}
|
|
|
|
|
2010-10-11 18:17:32 +00:00
|
|
|
/**
|
|
|
|
* Get the distance away from a point.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return distance
|
|
|
|
*/
|
|
|
|
public double distance(Point pt) {
|
|
|
|
return Math.sqrt(Math.pow(pt.x - x, 2) +
|
|
|
|
Math.pow(pt.y - y, 2) +
|
|
|
|
Math.pow(pt.z - z, 2));
|
|
|
|
}
|
|
|
|
|
2010-10-11 08:22:47 +00:00
|
|
|
/**
|
|
|
|
* Get a block point from a point.
|
|
|
|
*
|
|
|
|
* @param x
|
|
|
|
* @param y
|
|
|
|
* @param z
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return point
|
2010-10-11 08:22:47 +00:00
|
|
|
*/
|
|
|
|
public static Point toBlockPoint(double x, double y, double z) {
|
|
|
|
return new Point((int)Math.floor(x),
|
|
|
|
(int)Math.floor(y),
|
|
|
|
(int)Math.floor(z));
|
|
|
|
}
|
|
|
|
|
2010-10-03 19:43:30 +00:00
|
|
|
/**
|
|
|
|
* Checks if another object is equivalent.
|
|
|
|
*
|
|
|
|
* @param obj
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return whether the other object is equivalent
|
2010-10-03 19:43:30 +00:00
|
|
|
*/
|
|
|
|
@Override
|
2010-10-02 21:52:42 +00:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (!(obj instanceof Point)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Point other = (Point)obj;
|
2010-10-11 08:22:47 +00:00
|
|
|
return other.x == this.x && other.y == this.y && other.z == this.z;
|
2010-10-04 00:29:17 +00:00
|
|
|
|
2010-10-02 21:52:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 19:43:30 +00:00
|
|
|
/**
|
|
|
|
* Gets the hash code.
|
|
|
|
*
|
2010-10-11 15:56:19 +00:00
|
|
|
* @return hash code
|
2010-10-03 19:43:30 +00:00
|
|
|
*/
|
|
|
|
@Override
|
2010-10-02 21:52:42 +00:00
|
|
|
public int hashCode() {
|
2010-10-11 16:44:22 +00:00
|
|
|
return ((new Double(x)).hashCode() >> 13) ^
|
|
|
|
((new Double(y)).hashCode() >> 7) ^
|
|
|
|
(new Double(z)).hashCode();
|
2010-10-02 21:52:42 +00:00
|
|
|
}
|
2010-10-11 18:17:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns string representation "(x, y, z)".
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "(" + x + ", " + y + ", " + z + ")";
|
|
|
|
}
|
2010-10-02 21:52:42 +00:00
|
|
|
}
|