// $Id$ /* * WorldEdit * Copyright (C) 2010 sk89q and contributors * * 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 . */ package com.sk89q.worldedit; /** * * @author sk89q */ public class Vector2D { public static final Vector2D ZERO = new Vector2D(0, 0); public static final Vector2D UNIT_X = new Vector2D(1, 0); public static final Vector2D UNIT_Z = new Vector2D(0, 1); public static final Vector2D ONE = new Vector2D(1, 1); 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) { this.x = (double) x; this.z = (double) z; } /** * Construct the Vector2D object. * * @param x * @param z */ public Vector2D(float x, float z) { 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() { 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() { 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 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 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 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 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); } /** * Component-wise multiplication * * @param other * @return New point */ public Vector2D multiply(Vector2D other) { return new Vector2D(x * other.x, z * other.z); } /** * Component-wise multiplication * * @param x * @param z * @return New point */ public Vector2D multiply(double x, double z) { return new Vector2D(this.x * x, this.z * z); } /** * Component-wise multiplication * * @param x * @param z * @return New point */ public Vector2D multiply(int x, int z) { return new Vector2D(this.x * x, this.z * z); } /** * Component-wise multiplication * * @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); } /** * Component-wise division * * @param other * @return New point */ public Vector2D divide(Vector2D other) { return new Vector2D(x / other.x, z / other.z); } /** * Component-wise division * * @param x * @param z * @return New point */ public Vector2D divide(double x, double z) { return new Vector2D(this.x / x, this.z / z); } /** * Component-wise division * * @param x * @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.x && x <= max.x && z >= min.z && z <= max.z; } /** * 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.
*
* Components < 0.5 are rounded down, otherwise up * * @return */ public Vector2D round() { return new Vector2D(Math.floor(x + 0.5), Math.floor(z + 0.5)); } /** * Returns a vector with the absolute values of the components of this vector. * * @return */ public Vector2D positive() { return new Vector2D(Math.abs(x), Math.abs(z)); } /** * 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 ); } public boolean isCollinearWith(Vector2D other) { if (x == 0 && z == 0) { // this is a zero vector return true; } final double otherX = other.x; final double otherZ = other.z; if (otherX == 0 && otherZ == 0) { // other is a zero vector return true; } if ((x == 0) != (otherX == 0)) return false; if ((z == 0) != (otherZ == 0)) return false; final double quotientX = otherX / x; if (!Double.isNaN(quotientX)) { return other.equals(multiply(quotientX)); } final double quotientZ = otherZ / z; if (!Double.isNaN(quotientZ)) { return other.equals(multiply(quotientZ)); } throw new RuntimeException("This should not happen"); } /** * Gets a BlockVector version. * * @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); } /** * 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) ); } }