/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.math;

/**
 * A two dimensional vector
 * 
 * @author Matthias Betz
 *
 */
public class Vector2d {

	private double x;
	private double y;

	public Vector2d() {
		this(0, 0);
	}

	public Vector2d(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public Vector2d minus(Vector2d other) {
		return new Vector2d(x - other.x, y - other.y);
	}

	public Vector2d mult(double scalar) {
		return new Vector2d(scalar * x, scalar * y);
	}

	public Vector2d plus(Vector2d other) {
		return new Vector2d(x + other.x, y + other.y);
	}

	public double length() {
		return Math.sqrt(lengthSquare());
	}

	public double lengthSquare() {
		return x * x + y * y;
	}

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	/**
	 * Checks whether each coordinate is within epsilon distance, does NOT use
	 * euclidean distance.
	 * 
	 * @param other   the other vector
	 * @param epsilon the tolerance
	 * @return true if the coordinates are within epsilon distance, false otherwise
	 */
	public boolean equalsWithinEpsilon(Vector2d other, double epsilon) {
		if (Math.abs(other.x - x) > epsilon) {
			return false;
		}
		return Math.abs(other.y - y) <= epsilon;
	}

	@Override
	public String toString() {
		return "Vector2d [x=" + x + ", y=" + y + "]";
	}

	public double dot(Vector2d other) {
		return x * other.x + y * other.y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(x);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(y);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Vector2d other = (Vector2d) obj;
		if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
			return false;
		}
		return Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y);
	}

	public void normalize() {
		double l = length();
		x = x / l;
		y = y / l;
	}

	public double cross(Vector2d other) {
		return x * other.getY() - other.getX() * y;
	}

}
