/*
* Vector2.java
*
* A 2d vector math libaray.
*
* 2012 Brandon Reiss. All Rights Reserved. Do not duplicate or distribute
* without the author's consent.
*/
package com.brandonreiss.math;
/// A 3d vector.
public class Vector2 implements Cloneable {
public double x, y;
/*
* Constructors.
*/
public Vector2() {}
public Vector2(Vector2 rhs) {
x = rhs.x; y = rhs.y;
}
public Vector2(double x_, double y_) {
x = x_; y = y_;
}
/*
* Static methods.
*/
/// The zero vector.
public static Vector2 vectorZero() {
return new Vector2(0, 0);
}
/// Create a vector with the same value for each coordinate.
public static Vector2 vectorReplicate(double v) {
return new Vector2(v, v);
}
/// Vector addition.
public static Vector2 vectorAdd(Vector2 lhs, Vector2 rhs) {
return new Vector2(lhs.x + rhs.x, lhs.y + rhs.y);
}
/// Add scalar to all coordinates.
public static Vector2 vectorAdd(Vector2 lhs, double rhs) {
return new Vector2(lhs.x + rhs, lhs.y + rhs);
}
/// Vector subtraction.
public static Vector2 vectorSub(Vector2 lhs, Vector2 rhs) {
return new Vector2(lhs.x - rhs.x, lhs.y - rhs.y);
}
/// Subtract scalar from all coordinates.
public static Vector2 vectorSub(Vector2 lhs, double rhs) {
return new Vector2(lhs.x - rhs, lhs.y - rhs);
}
/// Element-wise multiplication.
public static Vector2 vectorMul(Vector2 lhs, Vector2 rhs) {
return new Vector2(lhs.x * rhs.x, lhs.y * rhs.y);
}
/// Scalar multiplication.
public static Vector2 vectorMul(Vector2 lhs, double rhs) {
return new Vector2(lhs.x * rhs, lhs.y * rhs);
}
/// Element-wise division.
public static Vector2 vectorDiv(Vector2 lhs, Vector2 rhs) {
return new Vector2(lhs.x / rhs.x, lhs.y / rhs.y);
}
/// Scalar division.
public static Vector2 vectorDiv(Vector2 lhs, double rhs) {
return new Vector2(lhs.x / rhs, lhs.y / rhs);
}
/// Return vector with the min coordinates from both inputs.
public static Vector2 vectorMin(Vector2 lhs, Vector2 rhs) {
return new Vector2(Math.min(lhs.x, rhs.x),
Math.min(lhs.y, rhs.y));
}
/// Return vector with the max coordinates from both inputs.
public static Vector2 max(Vector2 lhs, Vector2 rhs) {
return new Vector2(Math.max(lhs.x, rhs.x),
Math.max(lhs.y, rhs.y));
}
/// Return the minimum coordinate.
public static double vectorMin(Vector2 v) {
return Math.min(v.x, v.y);
}
/// Return the maximum coordinate.
public static double vectorMax(Vector2 v) {
return Math.max(v.x, v.y);
}
/// Vector dot product.
public static double vectorDot(Vector2 lhs, Vector2 rhs) {
return lhs.x * rhs.x + lhs.y * rhs.y;
}
/// Vector length squared.
public static double vectorLengthSq(Vector2 v) {
return vectorDot(v, v);
}
/// Vector length.
public static double vectorLength(Vector2 v) {
return Math.sqrt(vectorLengthSq(v));
}
/// Normalize vector such that its length is 1.
public static Vector2 vectorNormalize(Vector2 v) {
return vectorDiv(v, vectorLength(v));
}
/*
* Local methods.
*/
/// Set to zero vector.
public Vector2 zero() {
x = 0; y = 0;
return this;
}
/// Set the same value for each coordinate.
public Vector2 replicate(double v) {
x = v; y = v;
return this;
}
/// Set the value of each coordinate.
public Vector2 set(double x_, double y_) {
x = x_; y = y_;
return this;
}
/// Assign values from another vector.
public Vector2 set(Vector2 rhs) {
x = rhs.x; y = rhs.y;
return this;
}
/// Vector addition.
public Vector2 add(Vector2 rhs) {
x += rhs.x; y += rhs.y;
return this;
}
/// Add scalar to all coordinates.
public Vector2 add(double rhs) {
x += rhs; y += rhs;
return this;
}
/// Vector subtraction.
public Vector2 sub(Vector2 rhs) {
x -= rhs.x; y -= rhs.y;
return this;
}
/// Subtract scalar from all coordinates.
public Vector2 sub(double rhs) {
x -= rhs; y -= rhs;
return this;
}
/// Element-wise multiplication.
public Vector2 mul(Vector2 rhs) {
x *= rhs.x; y *= rhs.y;
return this;
}
/// Scalar multiplication.
public Vector2 mul(double rhs) {
x *= rhs; y *= rhs;
return this;
}
/// Element-wise division.
public Vector2 div(Vector2 rhs) {
x /= rhs.x; y /= rhs.y;
return this;
}
/// Scalar division.
public Vector2 div(double rhs) {
x /= rhs; y /= rhs;
return this;
}
/// Set to min coordinates from this and rhs.
public Vector2 min(Vector2 rhs) {
x = Math.min(x, rhs.x);
y = Math.min(y, rhs.y);
return this;
}
/// Set to max coordinates from this and rhs.
public Vector2 max(Vector2 rhs) {
x = Math.max(x, rhs.x);
y = Math.max(y, rhs.y);
return this;
}
/// Return the minimum coordinate.
public double min() {
return Math.min(x, y);
}
/// Return the maximum coordinate.
public double max() {
return Math.max(x, y);
}
/// Vector dot product.
public double dot(Vector2 rhs) {
return x * rhs.x + y * rhs.y;
}
/// Vector length squared.
public double lengthSq() {
return dot(this);
}
/// Vector length.
public double length() {
return Math.sqrt(lengthSq());
}
/// Normalize vector such that its length is 1.
public Vector2 normalize() {
return div(length());
}
/// Vector string conversion.
public String toString() {
return "[ " + x + ", " + y + " ]";
}
@Override public Vector2 clone() { return new Vector2(this); }
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Vector2)) {
return false;
}
Vector2 rhs = (Vector2)o;
return (x == rhs.x) && (y == rhs.y);
}
}