package com.prestige.util.math; import java.awt.Point; import java.awt.geom.Line2D; public class Vector { private double x, y; /** * @param x The x component of the vector. * @param y The y component of the vector. */ public Vector(final double x, final double y) { set(x, y); } public Vector() { this(0, 0); } public Vector(final Vector vector) { set(vector); } /** * @return The x component of the vector. */ public double getX() { return x; } /** * @return The y component of the vector. */ public double getY() { return y; } /** * @param x The new x factor of the vector. * @param y The new y factor of the vector. * @return The vector with the newly set components. */ public Vector set(final double x, final double y) { this.x = x; this.y = y; return this; } public Vector set(final Vector vector) { return set(vector.x, vector.y); } /** * @param dx The delta x adjustment of the vector. * @param dy The delta y adjustment of the vector. * @return The adjusted vector. */ public Vector adjust(final double dx, final double dy) { return set(getX() + dx, getY() + dy); } /** * @param v Adjusts the vector's components based on the vector v. * @return The adjusted vector. */ public Vector adjust(final Vector v) { return adjust(v.x, v.y); } /** * @param v1 The first vector in the equation. * @param v2 The second vector in the equation. * @return A vector with the quantities of v1 - v2. */ public static Vector subtract(final Vector v1, final Vector v2) { return new Vector(v1.x - v2.x, v1.y - v2.y); } /** * @param delta The delta deviation variable. * @return The vector object with multiplied components. */ public Vector multiply(final double delta) { return adjust(this.x * delta, this.y * delta); } /** * @param v The vector used to yield the dot product. * @return The dot product of two vectors. */ public double dot(final Vector v) { return (this.getX() * v.getX() + this.getY() * v.getY()); } /** * @return The magnitude of the vector. */ public double getMagnitude() { return Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2)); } /** * @return The vector, normalized. */ public Vector normalize() { return set(getX() / getMagnitude(), getY() / getMagnitude()); } /** * @param v The vector in which to yield the angle. * @return The angle between the two vectors in radians. */ public double getAngle(Vector v) { return Math.acos(dot(v) / (getMagnitude() * v.getMagnitude())); } /** * @return The vector object, with negated values. */ public Vector negate() { return set(-getX(), -getY()); } /** * @param line The line in which you want to convert. * @return A vector based on the location and length of the line. */ public static Vector toVector(final Line2D line) { return new Vector(line.getX2() - line.getX1(), line.getY2() - line.getY1()); } /** * @param p The point in which you want to convert. * @return A vector based on the components of point p. */ public static Vector toVector(final Point p) { return new Vector(p.getX(), p.getY()); } @Override public String toString() { return "(" + getX() + ", " + getY() + ")"; } }