Here you can find the source of intersects(Point p1, Point p2, Rectangle rect)
static public boolean intersects(Point p1, Point p2, Rectangle rect)
//package com.java2s; import java.awt.*; public class Main { static public boolean intersects(Point p1, Point p2, Rectangle rect) { // top edge if (intersects(p1, p2, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y))) return true; // bottom edge if (intersects(p1, p2, new Point(rect.x, rect.y + rect.height), new Point(rect.x + rect.width, rect.y + rect.height))) return true; // left edge if (intersects(p1, p2, new Point(rect.x, rect.y), new Point(rect.x, rect.y + rect.height))) return true; // right edge if (intersects(p1, p2, new Point(rect.x + rect.width, rect.y), new Point(rect.x + rect.width, rect.y + rect.height))) return true; return false; }// w w w. ja v a 2 s . c o m static public boolean intersects(Point p1, Point p2, Point p3, Point p4) { double ua = (double) ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / ((p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y)); double ub = (double) ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / ((p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y)); if (ua > 0 && ua < 1 && ub > 0 && ub < 1) return true; return false; } }