Java Geometry Algorithm findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4)

Here you can find the source of findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4)

Description

find Intersection

License

Open Source License

Declaration

static Point2D.Double findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3,
            Point2D.Double p4) 

Method Source Code

//package com.java2s;
/*//ww  w  .  ja va  2s .co m
 * #%L
 * Cytoscape Ding View/Presentation Impl (ding-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.awt.geom.Point2D;

public class Main {
    static Point2D.Double findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3,
            Point2D.Double p4) {

        double denominator = (p4.getY() - p3.getY()) * (p2.getX() - p1.getX())
                - (p4.getX() - p3.getX()) * (p2.getY() - p1.getY());

        double ua = ((p4.getX() - p3.getX()) * (p1.getY() - p3.getY())
                - (p4.getY() - p3.getY()) * (p1.getX() - p3.getX())) / denominator;

        double x = epsilon(p1.getX() + ua * (p2.getX() - p1.getX()));
        double y = epsilon(p1.getY() + ua * (p2.getY() - p1.getY()));
        return new Point2D.Double(x, y);
    }

    static double epsilon(double v) {
        if (Math.abs(v) < 1.0E-10)
            return 0.0;
        return v;
    }
}

Related

  1. extendLine(Point2D p0, Point2D p1, double toLength)
  2. findArea(Point2D p1, Point2D p2, Point2D p3)
  3. findDimension(Point2D[] pts)
  4. findDistancedPoint(double t, Point2D sp, Point2D c1, Point2D c2, Point2D ep)
  5. findIntersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  6. findLineSegmentIntersection(Line2D one, Line2D two, Point2D intersection)
  7. findMiddlePoint(Point2D p1, Point2D p2)
  8. fitCircle(final Point2D P1, final Point2D P2, final Point2D P3)
  9. forceMouseMove(Point pos)