Question: I am trying to figure out where a bunch of line-segments clip into a window around them. I saw the [Liang–Barsky](https://en.wikipedia.org/wiki/Liang%E2%80%93Barsky_algorithm) 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:**

[![enter image description here](https://i.stack.imgur.com/Zsqo3.png)](https://i.stack.imgur.com/Zsqo3.png)

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)`.

Answer: 
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.
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