/*-
 *  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.edge;

public class Point2d {

	private double x;
	private double y;

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

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

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	public Point2d plus(Vector2d dir) {
		return new Point2d(x + dir.getX(), y + dir.getY());
	}

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

	public boolean isAlmostEqual(Point2d other) {
		return isAlmostEqual(other, Global.getHighAccuracyTolerance());
	}

	public boolean isAlmostEqual(Point2d other, double eps) {
		Vector2d vec = minus(other);

		double localEps = eps * eps;
		// prevent the epsilon of getting to small
		if (localEps < Global.getHighAccuracyTolerance()) {
			localEps = Global.getHighAccuracyTolerance();
		}

		// not using sqrt, should speed this a little bit up
		return vec.getLength2() < localEps;
	}

	@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;
		}
		Point2d other = (Point2d) obj;
		if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
			return false;
		}
		if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) {
			return false;
		}
		return true;
	}

}
