延長された線分を (y 切片、勾配) などとしてパラメータ化してみることができます。特定の線分と交差する延長線のスペースは、線が点であるかのように照会できる (y 切片、勾配) 空間内の形状を形成します。(縦線は特例扱い)
四角形の境界線セグメントのいずれかと交差する線の結合を取り、延長されていないときに実際には四角形を横切らないセグメントを除外します。
// we will intersect against three of the rect's borders (the 4th's results are redundant)
borders = {(TopLeft, TopRight), (TopRight, BottomRight), (BottomRight, BottomLeft)}
// each border forms a shape in (y, slope) space defined by two intersecting half spaces
// we query the line space using something standard like kd-trees
lines1 = Union(borders.ForEach(LineSpace.Inside(ShapeOfSegmentInIntersectSpace(?border))))
// now filter out lines that don't cross the rect when extended
// since we already know they intersect when extended, the check is pretty simple
lines2 = lines1.Where(?line.BoundingRect.Intersects(rect))