JavaFX line Intersect - Java JavaFX

Java examples for JavaFX:Shape

Description

JavaFX line Intersect

Demo Code


import javafx.geometry.Point2D;
import javafx.geometry.Point3D;

public class Main{
    public static Point2D lineIntersect(Point2D firstLineStart,
            Point2D firstLineEnd, Point2D secondLineStart,
            Point2D secondLineEnd) {
        double x1 = firstLineStart.getX(), y1 = firstLineStart.getY(), x2 = firstLineEnd
                .getX(), y2 = firstLineEnd.getY(), x3 = secondLineStart
                .getX(), y3 = secondLineStart.getY(), x4 = secondLineEnd
                .getX(), y4 = secondLineEnd.getY();
        return GeometryUtil.lineIntersect(x1, y1, x2, y2, x3, y3, x4, y4);
    }/*w  w  w.j  a va 2s .c  om*/
    public static Point2D lineIntersect(double x1, double y1, double x2,
            double y2, double x3, double y3, double x4, double y4) {
        double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
        if (denom == 0.0) { // Lines are parallel.
            return null;
        }
        double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom;
        double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom;
        if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) {
            // Get the intersection point.
            return new Point2D(x1 + ua * (x2 - x1), y1 + ua * (y2 - y1));
        }
        return null;
    }
}

Related Tutorials