GeometryUtils { public static List<Point2D> intersection(final Shape s1, final Shape s2) { List<Point2D> intersections = new ArrayList<>(2); Line2D[] lines1 = shapeToLines(s1, false); Line2D[] lines2 = shapeToLines(s2, false); for (Line2D l1 : lines1) { for (Line2D l2 : lines2) { Point2D intersection = intersection(l1, l2); if (intersection != null) { intersections.add(intersection); } } } return intersections; } private  GeometryUtils(); static Line2D[] shapeToLines(Shape path, boolean swapped); static List<Point2D> intersection(final Shape s1, final Shape s2); static Point2D intersection(final Line2D l1, final Line2D l2); static Area grow(final Shape s, final double offset); static Area grow(final Shape s, final double offset, int join,
    		float miterlimit); static Area punch(Area shapeArea, double gap, boolean rounded,
    		Point2D pointPos, Shape pointShape); static List<PathSegment> getSegments(Shape shape); static Shape getShape(List<PathSegment> segments, boolean isDouble); static Shape reverse(Shape shape); static final double EPSILON; static final double EPSILON_SQ; }
@Test public void testIntersectionLineLine() { Line2D l1, l2; Point2D expected; l1 = new Line2D.Double(0.0, 0.0, 1.0, 1.0); l2 = new Line2D.Double(0.0, 1.0, 1.0, 0.0); expected = new Point2D.Double(0.5, 0.5); assertEquals(expected, GeometryUtils.intersection(l1, l2)); l1 = new Line2D.Double( 0.0, 0.0, -1.0, -1.0); l2 = new Line2D.Double( 0.0, -1.0, -1.0, 0.0); expected = new Point2D.Double(-0.5, -0.5); assertEquals(expected, GeometryUtils.intersection(l1, l2)); l1 = new Line2D.Double(0.0, 0.0, 1.0, 1.0); l2 = new Line2D.Double(0.0, 1.0, 1.0, 2.0); expected = null; assertEquals(expected, GeometryUtils.intersection(l1, l2)); }
@Test public void testIntersectionShapeShape() { Shape s1 = new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0); Shape s2 = new Rectangle2D.Double(0.5, 0.5, 1.0, 1.0); Point2D expected1 = new Point2D.Double(1.0, 0.5); Point2D expected2 = new Point2D.Double(0.5, 1.0); List<Point2D> intersections = GeometryUtils.intersection(s1, s2); assertTrue(intersections.contains(expected1)); assertTrue(intersections.contains(expected2)); }