Simulator/src/simulation/simulation/util/Utilities.java
/**
	 * Checks if two lines intersect.
	 * 
	 * @param firstLineStart
	 *            The starting position of the first line.
	 * @param firstLineEnd
	 *            The ending position of the first line.
	 * @param secondLineStart
	 *            The starting position of the second line.
	 * @param secondLineEnd
	 *            The ending position of the second line.
	 * @return True if they intersect, false otherwise.
	 */
Checks if two lines intersect.

@param firstLineStart
The starting position of the first line.
@param firstLineEnd
The ending position of the first line.
@param secondLineStart
The starting position of the second line.
@param secondLineEnd
The ending position of the second line.
@return True if they intersect, false otherwise.
Checks if two lines intersect . @param firstLineStart The starting position of the first line . @param firstLineEnd The ending position of the first line . @param secondLineStart The starting position of the second line . @param secondLineEnd The ending position of the second line . @return True if they intersect false otherwise .
public static boolean isLineCollision(Position firstLineStart, Position firstLineEnd, Position secondLineStart,
			Position secondLineEnd) {
		double x1 = firstLineStart.x;
		double x2 = firstLineEnd.x;
		double x3 = secondLineStart.x;
		double x4 = secondLineEnd.x;
		double y1 = firstLineStart.y;
		double y2 = firstLineEnd.y;
		double y3 = secondLineStart.y;
		double y4 = secondLineEnd.y;
		if (x1 == x2 && y1 == y2 || x3 == x4 && y3 == y4) {
			return false;
		}
		double ax = x2 - x1;
		double ay = y2 - y1;
		double bx = x3 - x4;
		double by = y3 - y4;
		double cx = x1 - x3;
		double cy = y1 - y3;
		double alphaNumerator = by * cx - bx * cy;
		double commonDenominator = ay * bx - ax * by;
		if (commonDenominator > 0) {
			if (alphaNumerator < 0 || alphaNumerator > commonDenominator) {
				return false;
			}
		} else if (commonDenominator < 0) {
			if (alphaNumerator > 0 || alphaNumerator < commonDenominator) {
				return false;
			}
		}
		double betaNumerator = ax * cy - ay * cx;
		if (commonDenominator > 0) {
			if (betaNumerator < 0 || betaNumerator > commonDenominator) {
				return false;
			}
		} else if (commonDenominator < 0) {
			if (betaNumerator > 0 || betaNumerator < commonDenominator) {
				return false;
			}
		}
		if (commonDenominator == 0) {
			double y3LessY1 = y3 - y1;
			double collinearityTestForP3 = x1 * (y2 - y3) + x2 * (y3LessY1) + x3 * (y1 - y2); 
			if (collinearityTestForP3 == 0) {
				if (x1 >= x3 && x1 <= x4 || x1 <= x3 && x1 >= x4 || x2 >= x3 && x2 <= x4 || x2 <= x3 && x2 >= x4
						|| x3 >= x1 && x3 <= x2 || x3 <= x1 && x3 >= x2) {
					if (y1 >= y3 && y1 <= y4 || y1 <= y3 && y1 >= y4 || y2 >= y3 && y2 <= y4 || y2 <= y3 && y2 >= y4
							|| y3 >= y1 && y3 <= y2 || y3 <= y1 && y3 >= y2) {
						return true;
					}
				}
			}
			return false;
		}
		return true;
	}
public static boolean isLineCollision ( Position firstLineStart , Position firstLineEnd , Position secondLineStart , Position secondLineEnd ) { double x1 = firstLineStart . x ; double x2 = firstLineEnd . x ; double x3 = secondLineStart . x ; double x4 = secondLineEnd . x ; double y1 = firstLineStart . y ; double y2 = firstLineEnd . y ; double y3 = secondLineStart . y ; double y4 = secondLineEnd . y ; if ( x1 == x2 && y1 == y2 || x3 == x4 && y3 == y4 ) { return false ; } double ax = x2 - x1 ; double ay = y2 - y1 ; double bx = x3 - x4 ; double by = y3 - y4 ; double cx = x1 - x3 ; double cy = y1 - y3 ; double alphaNumerator = by * cx - bx * cy ; double commonDenominator = ay * bx - ax * by ; if ( commonDenominator > 0 ) { if ( alphaNumerator < 0 || alphaNumerator > commonDenominator ) { return false ; } } else if ( commonDenominator < 0 ) { if ( alphaNumerator > 0 || alphaNumerator < commonDenominator ) { return false ; } } double betaNumerator = ax * cy - ay * cx ; if ( commonDenominator > 0 ) { if ( betaNumerator < 0 || betaNumerator > commonDenominator ) { return false ; } } else if ( commonDenominator < 0 ) { if ( betaNumerator > 0 || betaNumerator < commonDenominator ) { return false ; } } if ( commonDenominator == 0 ) { double y3LessY1 = y3 - y1 ; double collinearityTestForP3 = x1 * ( y2 - y3 ) + x2 * ( y3LessY1 ) + x3 * ( y1 - y2 ) ; if ( collinearityTestForP3 == 0 ) { if ( x1 >= x3 && x1 <= x4 || x1 <= x3 && x1 >= x4 || x2 >= x3 && x2 <= x4 || x2 <= x3 && x2 >= x4 || x3 >= x1 && x3 <= x2 || x3 <= x1 && x3 >= x2 ) { if ( y1 >= y3 && y1 <= y4 || y1 <= y3 && y1 >= y4 || y2 >= y3 && y2 <= y4 || y2 <= y3 && y2 >= y4 || y3 >= y1 && y3 <= y2 || y3 <= y1 && y3 >= y2 ) { return true ; } } } return false ; } return true ; }
Checks if two lines intersect.
Checks if two lines intersect .
// Code adapted from: http://www.java-gaming.org/index.php?topic=22590.0 // Return false if either of the lines have zero length // Fastest method, based on Franklin Antonio's "Faster Line Segment // Intersection" topic "in Graphics Gems III" book // (http://www.graphicsgems.org/) // This code wasn't in Franklin Antonio's method. It was added by // Keith Woodward. // The lines are parallel. // Check if they're collinear. // see // http://mathworld.wolfram.com/Collinear.html // If p3 is collinear with p1 and p2 then p4 will also be collinear, // since p1-p2 is parallel with p3-p4 // The lines are collinear. Now check if they overlap.