package szakdolgozat.client.game;

import szakdolgozat.client.ui.GameDialog;

public class Line {

	private Point P1;
	private Point P2;

	public Point getP1() {
		return P1;
	}

	public void setP1(Point p1) {
		P1 = p1;
	}

	public Point getP2() {
		return P2;
	}

	public void setP2(Point p2) {
		P2 = p2;
	}

	public Line(Point p1, Point p2) {
		super();
		P1 = p1;
		P2 = p2;
	}

	public boolean intersectsLine(Line line) {

		int a = (this.P1.getPositionInBrowserX() - this.P2
				.getPositionInBrowserX())
				* (line.P1.getPositionInBrowserY() - line.P2
						.getPositionInBrowserY());
		int b = (this.P1.getPositionInBrowserY() - this.P2
				.getPositionInBrowserY())
				* (line.P1.getPositionInBrowserX() - line.P2
						.getPositionInBrowserX());
		int d = a - b;
		if (d == 0)
			return false;

		int xi = ((line.P1.getPositionInBrowserX() - line.P2
				.getPositionInBrowserX())
				* (this.P1.getPositionInBrowserX()
						* this.P2.getPositionInBrowserY() - this.P1
						.getPositionInBrowserY()
						* this.P2.getPositionInBrowserX()) - (this.P1
				.getPositionInBrowserX() - this.P2.getPositionInBrowserX())
				* (line.P1.getPositionInBrowserX()
						* line.P2.getPositionInBrowserY() - line.P1
						.getPositionInBrowserY()
						* line.P2.getPositionInBrowserX()))
				/ d;
		int yi = ((line.P1.getPositionInBrowserY() - line.P2
				.getPositionInBrowserY())
				* (this.P1.getPositionInBrowserX()
						* this.P2.getPositionInBrowserY() - this.P1
						.getPositionInBrowserY()
						* this.P2.getPositionInBrowserX()) - (this.P1
				.getPositionInBrowserY() - this.P2.getPositionInBrowserY())
				* (line.P1.getPositionInBrowserX()
						* line.P2.getPositionInBrowserY() - line.P1
						.getPositionInBrowserY()
						* line.P2.getPositionInBrowserX()))
				/ d;
		/*
		 * ("(xi,yi) = (" + xi + "," + yi + ") " +
		 * line.getP1().getPositionInBrowserX() + "---" +
		 * line.getP2().getPositionInBrowserX());
		 */
		int a1 = xi - line.getP1().getPositionInBrowserX();
		int a2 = xi - line.getP2().getPositionInBrowserX();
		if ((a1 > 0 && a2 < 0) || (a1 < 0 && a2 > 0))
			return false;

		GameDialog.lineCanvas.fillRect(xi, yi, 5, 5);
		GameDialog.lineCanvas.stroke();
		return true;
	}

	public boolean lineSegmentIntersection(Line line) {
		double Ax = this.getP1().getPositionInBrowserX();
		double Ay = this.getP1().getPositionInBrowserY();
		double Bx = this.getP2().getPositionInBrowserX();
		double By = this.getP2().getPositionInBrowserY();
		double Cx = line.getP1().getPositionInBrowserX();
		double Cy = line.getP1().getPositionInBrowserY();
		double Dx = line.getP2().getPositionInBrowserX();
		double Dy = line.getP2().getPositionInBrowserY();

		double distAB, theCos, theSin, newX, ABpos;

		// Fail if either line segment is zero-length.
		if (Ax == Bx && Ay == By || Cx == Dx && Cy == Dy)
			return false;

		// Fail if the segments share an end-point.
		if (Ax == Cx && Ay == Cy || Bx == Cx && By == Cy || Ax == Dx
				&& Ay == Dy || Bx == Dx && By == Dy) {
			return false;
		}

		// (1) Translate the system so that point A is on the origin.
		Bx -= Ax;
		By -= Ay;
		Cx -= Ax;
		Cy -= Ay;
		Dx -= Ax;
		Dy -= Ay;

		// Discover the length of segment A-B.
		distAB = Math.sqrt(Bx * Bx + By * By);

		// (2) Rotate the system so that point B is on the positive X axis.
		theCos = Bx / distAB;
		theSin = By / distAB;
		newX = Cx * theCos + Cy * theSin;
		Cy = Cy * theCos - Cx * theSin;
		Cx = newX;
		newX = Dx * theCos + Dy * theSin;
		Dy = Dy * theCos - Dx * theSin;
		Dx = newX;

		// Fail if segment C-D doesn't cross line A-B.
		if (Cy < 0. && Dy < 0. || Cy >= 0. && Dy >= 0.)
			return false;

		// (3) Discover the position of the intersection point along line A-B.
		ABpos = Dx + (Cx - Dx) * Dy / (Dy - Cy);

		// Fail if segment C-D crosses line A-B outside of segment A-B.
		if (ABpos < 0. || ABpos > distAB)
			return false;

		// (4) Apply the discovered position to line A-B in the original
		// coordinate system.
		// *X=Ax+ABpos*theCos;
		// *Y=Ay+ABpos*theSin;

		// Success.
		return true;
	}

	public Point getIntersectionPoint(Line line) {
		if (!this.intersectsLine(line))
			return null;
		double px = this.P1.getPositionInBrowserX(), py = this.P1
				.getPositionInBrowserY(), rx = this.P2.getPositionInBrowserX()
				- px, ry = this.P2.getPositionInBrowserY() - py;
		double qx = line.P1.getPositionInBrowserX(), qy = line.P1
				.getPositionInBrowserY(), sx = line.P2.getPositionInBrowserX()
				- qx, sy = line.P2.getPositionInBrowserY() - qy;

		double det = sx * ry - sy * rx;
		if (det == 0) {
			return null;
		} else {
			double z = (sx * (qy - py) + sy * (px - qx)) / det;
			if (z == 0 || z == 1)
				return null; // intersection at end point!
			Point p = new Point((int) (px + z * rx), (int) (py + z * ry));
			GameDialog.lineCanvas.fillRect(p.getX(), p.getY(), 5, 5);
			GameDialog.lineCanvas.stroke();
			return p;
		}
	} // end intersection line-line

	private void swapPoints() {
		Point temp = new Point();
		temp.setPositionInBrowserX(this.P1.getPositionInBrowserX());
		temp.setPositionInBrowserY(this.P1.getPositionInBrowserY());
		temp.setX(this.P1.getX());
		temp.setY(this.P1.getY());
		this.P1.setPositionInBrowserX(this.P2.getPositionInBrowserX());
		this.P1.setPositionInBrowserY(this.P2.getPositionInBrowserY());
		this.P1.setX(this.P2.getX());
		this.P1.setY(this.P2.getY());
		this.P2.setPositionInBrowserX(temp.getPositionInBrowserX());
		this.P2.setPositionInBrowserY(temp.getPositionInBrowserY());
		this.P2.setX(temp.getX());
		this.P2.setY(temp.getY());

	}

	@Override
	public String toString() {
		return "Line2D [P1=" + P1 + ", P2=" + P2 + "]";
	}

}
