Java Utililty Methods Line Intersect

List of utility methods to do Line Intersect

Description

The list of methods to do Line Intersect are organized into topic(s).

Method

intintersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
Checks whether line (x1, y1) - (x2, y2) and line (x3, y3) - (x4, y4) intersect.
double A1 = -(y2 - y1);
double B1 = (x2 - x1);
double C1 = x1 * y2 - x2 * y1;
double A2 = -(y4 - y3);
double B2 = (x4 - x3);
double C2 = x3 * y4 - x4 * y3;
double coefParallel = A1 * B2 - A2 * B1;
if (x3 == x4 && y3 == y4 && (A1 * x3 + B1 * y3 + C1 == 0) && (x3 >= Math.min(x1, x2))
...
double[]intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
intersect Line Segments
final double denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (denominator == 0.d) {
    return null; 
final double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
if (ua <= 0.d || ua >= 1.d) {
    return null; 
final double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
if (ub <= 0.d || ub >= 1.d) {
    return null; 
return new double[] { x1 + ua * (x2 - x1), y1 + ua * (y2 - y1), ua };
intintersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] params)
intersect Lines With Params
double dx = x4 - x3;
double dy = y4 - y3;
double d = dx * (y2 - y1) - dy * (x2 - x1);
if (Math.abs(d) < EPSILON) {
    return 0;
params[0] = (-dx * (y1 - y3) + dy * (x1 - x3)) / d;
if (dx != 0) {
...
booleanintersects(int b1, int e1, int b2, int e2)
Returns true if one span intersects with the other
if (contains(b1, e1, b2, e2) != 0)
    return true;
return (b1 <= b2 && b2 < e1 || b2 <= b1 && b1 < e2);
booleanintersects(int start1, int length1, int start2, int length2)
intersects
if (length1 == 0)
    length1 = 1;
if (length2 == 0)
    length2 = 1;
int min1 = Math.min(start1, start1 + length1);
int max1 = Math.max(start1, start1 + length1);
int min2 = Math.min(start2, start2 + length2);
int max2 = Math.max(start2, start2 + length2);
...
booleanintersects(int x0, int y0, int w0, int h0, int x, int y, int w, int h)
Test to see if two rectangles intersect
if (w0 <= 0 || h0 <= 0 || w <= 0 || h <= 0) {
    return false;
return (x + w > x0 && y + h > y0 && x < x0 + w0 && y < y0 + h0);