public boolean SegmentIntersectRectangle(double a_rectangleMinX, double a_rectangleMinY,
            double a_rectangleMaxX, double a_rectangleMaxY, double a_p1x, double a_p1y,
            double a_p2x, double a_p2y)
    {
        double minX = a_p1x;
        double maxX = a_p2x;

        if (a_p1x > a_p2x)
        {
            minX = a_p2x;
            maxX = a_p1x;
        }

        if (maxX > a_rectangleMaxX)
        {
            maxX = a_rectangleMaxX;
        }

        if (minX < a_rectangleMinX)
        {
            minX = a_rectangleMinX;
        }

        if (minX > maxX)
        {
            return false;
        }

        double minY = a_p1y;
        double maxY = a_p2y;

        double dx = a_p2x - a_p1x;

        if (Math.abs(dx) > 0.0000001)
        {
            double a = (a_p2y - a_p1y) / dx;
            double b = a_p1y - a * a_p1x;
            minY = a * minX + b;
            maxY = a * maxX + b;
        }

        if (minY > maxY)
        {
            double tmp = maxY;
            maxY = minY;
            minY = tmp;
        }

        if (maxY > a_rectangleMaxY)
        {
            maxY = a_rectangleMaxY;
        }

        if (minY < a_rectangleMinY)
        {
            minY = a_rectangleMinY;
        }

        if (minY > maxY)
        {
            return false;
        }

        return true;
    }