Numerically compute intersection of infinite line and rectangle

I'm trying to numerically compute where an infinite line defined by two points intersects a (finite) rectangle also defined by two points. Here's a illustration of the various ways that the line can intersect the rectangle: How can I determine the number and location of the places where the infinite line intersects the rectangle? I know how to do this when the line is finite, but I can't figure out how to generalize that to an infinite context.

• all four corners on one side: $$0$$ intersections
• one corner on one side, three corners on the other side: $$2$$ intersections
• two corners on one side, two on the other side: $$2$$ intersections
• three on one side: $$1$$ intersection
• one on one side, two on the other sides: $$2$$ intersections
• they are diagonally opposite ones: $$2$$ intersections