Java Line Intersect intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)

Here you can find the source of intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)

Description

Return the intersection point between infinite line A defined by points p1 & p2 and infinite line B defined by points p3 & p4.

License

Open Source License

Parameter

Parameter Description
p1 first point of line A
p2 second point of line A
p3 first point of line B
p4 second point of line B

Return

the intersection point

Declaration

public static Point2D.Double intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.awt.geom.Point2D;

public class Main {
    /**//w w w . j  av a  2  s . c om
     * Return the intersection point between infinite line A defined by
     * points p1 & p2 and infinite line B defined by points p3 & p4.
     *
     * @param p1 first point of line A
     * @param p2 second point of line A
     * @param p3 first point of line B
     * @param p4 second point of line B
     * @return the intersection point
     */
    public static Point2D.Double intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4) {
        double x1 = p1.getX();
        double y1 = p1.getY();
        double x2 = p2.getX();
        double y2 = p2.getY();
        double x3 = p3.getX();
        double y3 = p3.getY();
        double x4 = p4.getX();
        double y4 = p4.getY();

        double den = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));

        double v12 = (x1 * y2) - (y1 * x2);
        double v34 = (x3 * y4) - (y3 * x4);

        double x = ((v12 * (x3 - x4)) - ((x1 - x2) * v34)) / den;
        double y = ((v12 * (y3 - y4)) - ((y1 - y2) * v34)) / den;

        return new Point2D.Double(x, y);
    }
}

Related

  1. intersect(Point a, Point b, Point c, Point d)
  2. intersect(Shape shape1, Shape shape2)
  3. intersectClip(final Graphics2D g2d, final Shape clip)
  4. intersection(Line2D a, Line2D b)
  5. intersection(Line2D lineA, Line2D lineB)
  6. intersectionPoint(final Line2D l1, final Line2D l2)
  7. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  8. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  9. intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)