/*-
 *  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 GmBoundedStraight2d extends GmStraight2d {
	
	private double length;
	private Point2d target;
	
	public static GmBoundedStraight2d of(Point2d start, Vector2d dir) {
		GmBoundedStraight2d straight = new GmBoundedStraight2d(start, dir);
		straight.target = straight.getOrigin().plus(dir);
		straight.length = dir.getLength();
		return straight;
	}
	
	public static GmBoundedStraight2d of(Point2d from, Point2d to) {
		Vector2d dir = to.minus(from);
		GmBoundedStraight2d straight = new GmBoundedStraight2d(from, dir);
		straight.target = to;
		straight.length = dir.getLength();
		return straight;
	}
	
	private GmBoundedStraight2d(Point2d start, Vector2d dir) {
		super(start, dir);
	}
	
	public Point2d getTarget() {
		return target;
	}

	public boolean isWithinBoundaries(double parameter, double eps) {
		if (Math.abs(parameter) < eps) {
			parameter = 0.0;
		}
		if (Math.abs(parameter - length) < eps) {
			parameter = length;
		}

		if (0 <= parameter && parameter <= length) {
			return true;
		}
		return false;
	}

	public double getLength() {
		return length;
	}

	public boolean isWithinBoundaries(double parameter) {
		return isWithinBoundaries(parameter, Global.getHighAccuracyTolerance());
	}

}
