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 a 2s . co  m

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

    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. intersection(Line2D a, Line2D b)
  2. intersection(Line2D lineA, Line2D lineB)
  3. intersection(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  4. intersectionPoint(final Line2D l1, final Line2D l2)
  5. intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
  6. intersectLines(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] point)
  7. intersectLineSegments(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  8. intersectLinesWithParams(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] params)
  9. intersects(int b1, int e1, int b2, int e2)