Java Line Intersect intersect(int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd)

Here you can find the source of intersect(int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd)

Description

intersect

License

Open Source License

Declaration

public static Point intersect(int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd) 

Method Source Code

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

import java.awt.*;

public class Main {
    public static Point intersect(int xa, int ya, int xb, int yb, int xc, int yc, int xd, int yd) {
        double denom = (double) ((xb - xa) * (yd - yc) - (yb - ya) * (xd - xc));
        double rnum = (double) ((ya - yc) * (xd - xc) - (xa - xc) * (yd - yc));
        if (denom == 0.0D) {
            return rnum != 0.0D ? null
                    : ((xa >= xb || xb >= xc && xb >= xd) && (xa <= xb || xb <= xc && xb <= xd) ? new Point(xa, ya)
                            : new Point(xb, yb));
        } else {/*from ww w  .  j  a v a 2 s . c o m*/
            double r = rnum / denom;
            double snum = (double) ((ya - yc) * (xb - xa) - (xa - xc) * (yb - ya));
            double s = snum / denom;
            if (0.0D <= r && r <= 1.0D && 0.0D <= s && s <= 1.0D) {
                int px = (int) ((double) xa + (double) (xb - xa) * r);
                int py = (int) ((double) ya + (double) (yb - ya) * r);
                return new Point(px, py);
            } else {
                return null;
            }
        }
    }

    public static java.awt.geom.Point2D.Double intersect(double xa, double ya, double xb, double yb, double xc,
            double yc, double xd, double yd) {
        double denom = (xb - xa) * (yd - yc) - (yb - ya) * (xd - xc);
        double rnum = (ya - yc) * (xd - xc) - (xa - xc) * (yd - yc);
        if (denom == 0.0D) {
            return rnum != 0.0D ? null
                    : ((xa >= xb || xb >= xc && xb >= xd) && (xa <= xb || xb <= xc && xb <= xd)
                            ? new java.awt.geom.Point2D.Double(xa, ya)
                            : new java.awt.geom.Point2D.Double(xb, yb));
        } else {
            double r = rnum / denom;
            double snum = (ya - yc) * (xb - xa) - (xa - xc) * (yb - ya);
            double s = snum / denom;
            if (0.0D <= r && r <= 1.0D && 0.0D <= s && s <= 1.0D) {
                double px = xa + (xb - xa) * r;
                double py = ya + (yb - ya) * r;
                return new java.awt.geom.Point2D.Double(px, py);
            } else {
                return null;
            }
        }
    }

    public static java.awt.geom.Point2D.Double intersect(double xa, double ya, double xb, double yb, double xc,
            double yc, double xd, double yd, double limit) {
        double denom = (xb - xa) * (yd - yc) - (yb - ya) * (xd - xc);
        double rnum = (ya - yc) * (xd - xc) - (xa - xc) * (yd - yc);
        if (denom == 0.0D) {
            return rnum != 0.0D ? null
                    : ((xa >= xb || xb >= xc && xb >= xd) && (xa <= xb || xb <= xc && xb <= xd)
                            ? new java.awt.geom.Point2D.Double(xa, ya)
                            : new java.awt.geom.Point2D.Double(xb, yb));
        } else {
            double r = rnum / denom;
            double snum = (ya - yc) * (xb - xa) - (xa - xc) * (yb - ya);
            double s = snum / denom;
            double px;
            double py;
            if (0.0D <= r && r <= 1.0D && 0.0D <= s && s <= 1.0D) {
                px = xa + (xb - xa) * r;
                py = ya + (yb - ya) * r;
                return new java.awt.geom.Point2D.Double(px, py);
            } else {
                px = xa + (xb - xa) * r;
                py = ya + (yb - ya) * r;
                return length(xa, ya, px, py) > limit && length(xb, yb, px, py) > limit
                        && length(xc, yc, px, py) > limit && length(xd, yd, px, py) > limit ? null
                                : new java.awt.geom.Point2D.Double(px, py);
            }
        }
    }

    public static long length(int x1, int y1, int x2, int y2) {
        return (long) Math.sqrt((double) length2(x1, y1, x2, y2));
    }

    public static double length(double x1, double y1, double x2, double y2) {
        return Math.sqrt(length2(x1, y1, x2, y2));
    }

    public static double length(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2) {
        return Math.sqrt(length2(p1.x, p1.y, p2.x, p2.y));
    }

    public static long length2(int x1, int y1, int x2, int y2) {
        return (long) ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }

    public static double length2(double x1, double y1, double x2, double y2) {
        return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
    }
}

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)