Here you can find the source of intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
public static Point intersectionZJU(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
//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; } }