private Point2D.Double getIntersectingPoint(double theta, Rectangle2D r) {
        double cx = r.getCenterX();
        double cy = r.getCenterY();
        double W = r.getWidth() / 2;
        double H = r.getHeight() / 2;
        double R = Point2D.distance(cx, cy, cx + W, cy + H);
        double x = cx + R * Math.cos(theta);
        double y = cy + R * Math.sin(theta);
        Point2D.Double p = new Point2D.Double();
        int outcode = r.outcode(x, y);
        switch (outcode) {
            case Rectangle2D.OUT_TOP:
                p.x = cx - H * ((x - cx) / (y - cy));
                p.y = cy - H;
                break;
            case Rectangle2D.OUT_LEFT:
                p.x = cx - W;
                p.y = cy - W * ((y - cy) / (x - cx));
                break;
            case Rectangle2D.OUT_BOTTOM:
                p.x = cx + H * ((x - cx) / (y - cy));
                p.y = cy + H;
                break;
            case Rectangle2D.OUT_RIGHT:
                p.x = cx + W;
                p.y = cy + W * ((y - cy) / (x - cx));
                break;
            default:
                p.x = cx + W;
                p.y = cy + W * ((y - cy) / (x - cx));

        }
        return p;
    }