I think you are asking if one specific line intersects one specific square (if that problem is solved, it applies to any number of squares).
Try this. Say the line has equation y = a*x + b, and the lower left of the squares has
coordinates (x1, y1) while the upper left corner has coordinates (x2, y2).
Find the points y' = a*x1 + b, y'' = a*x2 + b ("*" for multiply);
then depending on whether y' > y'' or vice versa, you have an intervals [y', y''] or [y'', y'], say for argument that the interval is [y', y''].
(In math notation, [m, n] stands for all numbers >= m, and <= n.)
If [y', y''] intersects [y1, y2], the line enters the square.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…