Find where line-segments can intersect with a box I am trying to figure out where a bunch of line-segments clip into a window around them. I saw the Liang–Barsky algorithm, but that seems to assume the segments already intersect with the edges of the box, which these do not.
Say I have a box from (0,0) to (26,16), and the following segments: A: (7,6) - (16,3)
B: (10,6) - (19,6)
C: (13,10) - (21,3)
D: (16,12) - (19,14)

Illustration:

I imagine I need to extend the segments to a certain X or Y point, till they hit the edge of the window, but I don't know how.
How would I find the points where these segments, when extended, will intersect into the edge of the box? For instance, segment B, when extended, will intersect the box at (0,6) and (26,6).
A: Parametrise the lines using $t$:
$$\begin{cases}
x(t) = x_0 + t ( x_1 - x_0 ) \\
y(t) = y_0 + t ( y_1 - y_0 ) \end{cases}$$
Note that for $t = 0$, $x(t) = x_0$ and $y(t) = y_0$, and for $t = 1$, $x(t) = x_1$ and $y(t) = y_1$.
To find where the line intersects with $x_C$, solve $x(t) = x_C$ for $t$:
$$t = \frac{x_C - x_0}{x_1 - x_0}$$
If $x_1 = x_0$, there is no solution, because the line is vertical, or degenerate (a point).
To find where the line intersects with $y_C$, solve $y(t) = y_C$ for $t$:
$$t = \frac{y_C - y_0}{y_1 - y_0}$$
Again, if $y_1 = y_0$, there is no solution, because the line is horizontal, or degenerate (a point).
If the line is not horizontal or vertical or degenerate, you get two solutions $t$ for the vertical edges, and two solutions for the horizontal edges. If the line segment is contained within the box, then two of the solutions are negative, and two positive. The correct intersections are then the larger of the negative $t$, and the smaller of the positive $t$.
To find the actual coordinates of the intersection point, use the formula for $x(t)$ and $y(t)$, above.
A: Given two points $A,B$, the line through them has equation $(y-y_A)/(y_B-y_A)=(x-x_A)/(x_B-x_A)$, which - to account for the due comment by amd - we shall better write as $ (y-y_A)(x_B-x_A)=(x-x_A)(y_B-y_A)$ so that vertical and horizontal lines are included.
Then 


*

*put in it $x=0$  and determine if the resulting $y$ is comprised
between $0$ and $16$: if yes keep the point $(0,y)$ as intersection, if not discard;

*same for $x=26$

*then introduce in the equation $y=0$ and check the resulting $0 \le x \le 26$, again keep/discard the point if yes/no

*same for $y=16$


You will definitely end with keeping exactly two points, apart from possible duplication when the intersection is exactly on a corner. In this case discard any duplicated point.
example:
1st segment : $(7,6),(16,3) \quad \to \quad x_A=7,\; x_B=16,\; y_A=6,\; y_B=3$
equation: $(y-6)9=(x-7)(-3)\; \to; 3y=25-x$
points:
$x=0, y=25/3\;$, YES
$x=26,y=-1/3\;$, NO
$x=25, y=0\;$, YES
$x=-23, y=16\;$, NO  
