Java Line Intersect intersectionPoint(final Line2D l1, final Line2D l2)

Here you can find the source of intersectionPoint(final Line2D l1, final Line2D l2)

Description

Calculates the intersection point where the two infinitely enlarged lines meet.

License

Open Source License

Parameter

Parameter Description
l1 The first line.
l2 The second line.

Return

The intersection point or null if the lines are parallel.

Declaration

public static Point2D intersectionPoint(final Line2D l1, final Line2D l2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main {
    /**//www.j  a  v a2 s  . c  om
     * Calculates the intersection point where the two infinitely enlarged lines
     * meet.
     * 
     * @param l1 The first line.
     * @param l2 The second line.
     * @return The intersection point or <code>null</code> if the lines are
     *         parallel.
     */
    public static Point2D intersectionPoint(final Line2D l1, final Line2D l2) {
        final double x1 = l1.getX1();
        final double y1 = l1.getY1();
        final double x2 = l1.getX2();
        final double y2 = l1.getY2();

        final double x3 = l2.getX1();
        final double y3 = l2.getY1();
        final double x4 = l2.getX2();
        final double y4 = l2.getY2();

        final double d = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
        // TODO check whether both are the same line
        if (d == 0)
            return null;
        final double a = (x1 * y2) - (y1 * x2);
        final double b = (x3 * y4) - (y3 * x4);
        return new Point2D.Double(((a * (x3 - x4)) - ((x1 - x2) * b)) / d, ((a * (y3 - y4)) - ((y1 - y2) * b)) / d);
    }
}

Related

  1. intersect(Shape shape1, Shape shape2)
  2. intersectClip(final Graphics2D g2d, final Shape clip)
  3. intersection(Line2D a, Line2D b)
  4. intersection(Line2D lineA, Line2D lineB)
  5. intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  6. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  7. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  8. intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
  9. intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)