I recently needed a method that...well, the title says it all. This method takes the x, y, width and height values of a rectangle, and the two endpoints of a line, and returns true if the line intersects the rectangle range at all.
Apologies if I'm missing something or misinterpreting your code, but it seems there are a couple potential issues there. First, this:
double m = (ly2-ly1)/(lx2-lx1);
May fail if the line is vertical or nearly vertical. For this reason, the slope form for lines is hardly every used in collision detection code. In general, representations that are invariant under rotation should be preferred.
Also, I think there may be some cases for which the function may not return the correct result, but I'd have to look at it a bit more carefully to be sure.
In any case, if you just need a boolean result, you don't need to do quite that much work. The separating axis test for a line segment and a box is simple and robust, and will handle all cases correctly. (Your code starts out as the SAT, but then instead of testing the axis parallel to the line normal, performs edge intersection tests instead.)