Java Line Intersect intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)

Here you can find the source of intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)

Description

intersection ZJU

License

Apache License

Declaration

public static Point intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Point;

public class Main {
    public static Point intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        long d = ((long) (y4 - y3)) * (x2 - x1) - ((long) (x4 - x3)) * (y2 - y1);
        if (d == 0)
            return new Point(-1, -1); // parallel lines
        double a = (((long) (x4 - x3)) * (y1 - y3) - ((long) (y4 - y3)) * (x1 - x3)) * 1.0 / d;
        int x = x1 + (int) (a * (x2 - x1));
        int y = y1 + (int) (a * (y2 - y1));
        // check they are within the bounds of the lines
        if (x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && x <= Math.max(x3, x4) && x >= Math.min(x3, x4)
                && y >= Math.min(y1, y2) && y <= Math.max(y1, y2) && y <= Math.max(y3, y4) && y >= Math.min(y3, y4)) // they
            // cross!!
            return new Point(x, y);
        return new Point(-1, -1);
    }/*  ww w. j a v  a2 s .  c om*/

    /**
     * @author Ali
     * @param x1
     * @param p1
     *            .y
     * @param p2
     *            .x
     * @param p2
     *            .y
     * @param p3
     *            .x
     * @param p3
     *            .y
     * @param p4
     *            .x
     * @param p4
     *            .y
     * @return
     */
    public static Point intersectionZJU(Point p1, Point p2, Point p3, Point p4) {
        int d = ((p4.y - p3.y)) * (p2.x - p1.x) - ((p4.x - p3.x)) * (p2.y - p1.y);
        if (d == 0)
            return null; // parallel lines
        double a = (((p4.x - p3.x)) * (p1.y - p3.y) - ((p4.y - p3.y)) * (p1.x - p3.x)) / (double) d;
        int x = p1.x + (int) (a * (p2.x - p1.x));
        int y = p1.y + (int) (a * (p2.y - p1.y));
        // check they are within the bounds of the lines
        if (x >= Math.min(p1.x, p2.x) && x <= Math.max(p1.x, p2.x) && x <= Math.max(p3.x, p4.x)
                && x >= Math.min(p3.x, p4.x) && y >= Math.min(p1.y, p2.y) && y <= Math.max(p1.y, p2.y)
                && y <= Math.max(p3.y, p4.y) && y >= Math.min(p3.y, p4.y)) // they
            // cross!!
            return new Point(x, y);
        return null;
    }

    public static int min(int x, int y) {
        return x <= y ? x : y;
    }

    public static int max(int x, int y) {
        return x >= y ? x : y;
    }
}

Related

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