public Point2D getMinIntersection(final Shape poly, final Line2D.Double line) {

        double midX = line.x2 - line.x1;
        double midY = line.y2 - line.y1;

        final PathIterator pathIterator = poly.getPathIterator(null); //Getting an iterator along the polygon path
        final double[] curr = new double[6]; //Double array with length 6 needed by iterator
        final double[] first = new double[2]; //First point (needed for closing polygon path)
        final double[] prev = new double[2]; //Previously visited point
        Point2D min = null;
        double minMag = Integer.MAX_VALUE;
        while (!pathIterator.isDone()) {
            switch (pathIterator.currentSegment(curr)) {
                case PathIterator.SEG_LINETO: {
                    final Line2D.Double currentLine = new Line2D.Double(prev[0], prev[1], curr[0], curr[1]);
                    if (currentLine.intersectsLine(line)) {
                        Point2D intersect = getIntersection(currentLine, line);
                        if (magnitude(midX, midY, intersect.getX(), intersect.getY()) < minMag) {
                            min = intersect;
                        }
                    }
                    prev[0] = curr[0];
                    prev[1] = curr[1];
                    break;
                }
                case PathIterator.SEG_CLOSE: {
                    final Line2D.Double currentLine = new Line2D.Double(curr[0], curr[1], first[0], first[1]);
                    if (currentLine.intersectsLine(line)) {
                        Point2D intersect = getIntersection(currentLine, line);
                        if (magnitude(midX, midY, intersect.getX(), intersect.getY()) < minMag) {
                            min = intersect;
                        }
                    }
                    break;
                }
                case PathIterator.SEG_MOVETO:
                    first[0] = prev[0] = curr[0];
                    first[1] = prev[1] = curr[1];
                    break;
                default: {
                    throw new IllegalArgumentException("Unsupported PathIterator segment type.");
                }
            }
            pathIterator.next();
        }
        return min;

    }