What is the equation of intersection between a line segment and a square?

If we have a line segment l and an axis-aligned fixed size square s, what is the algebraic equation of length of intersection between l and s? (with respect to center of s)

EDIT: in fact we are searching the length of subsegment of l that is inner the square

• The intersection of a line segment and a square might depend on whether you consider the interior of the square to be included in the definition. If it is, then you are intersecting two convex sets and the result is either empty or a convex subset of the line segment, i.e. a smaller line segment or a point. – hardmath Mar 2 '17 at 13:19
• Not enough information. Is the square centered at the origin or is it on the positive axes or is it anywhere in the plane? Is your line $l$ a line through the origin or is it a general line y = mx + b? – victoria Mar 2 '17 at 13:41
• @victoria line l is in a general line but fixed, and the square can be anywhere in plane. In fact, as I said in question I want an algebraic equation that shows the intersection length with respect to place of square s – JalalJaberi Mar 2 '17 at 14:36
• @hardmath To be clear, I want the length of line segment that is inner the square – JalalJaberi Mar 2 '17 at 14:37
• The problem is that you have four possible cases depending on the slope and intercept of the line, the position of the square, and the side $s$ of the square. There's a fifth and most common result, that they don't intersect at all. There won't be one simple rule but five cases. – victoria Mar 2 '17 at 15:39

Consider the line segment connecting points $(x_0,y_0)$ and $(x_1,y_1)$ in the Cartesian plane, of length $\ell = \sqrt{(x_1-x_0)^2 + (y_1-y_0)^2}$.

The entire length of the line segment is interior to the square if and only if both its endpoints are interior to the square. So it makes sense to begin the determination of length with a check on which of the endpoints (if either) fall inside the square.

Let the (filled) square with axis-parallel sides be $[a,a+s] \times [b,b+s]$. A point $(x,y)$ is in the square if and only if:
$$0 \le x-a \le s \;\text{ and }\; 0 \le y-b \le s$$

There are three cases: both inside, one inside & one outside, both outside. Other than the first of these cases, the length you want will depend on locating intersections between line segments, a topic that has been explored here before.

In the second case (one endpoint inside, one outside), there is always a single intersection to locate (between the given line segment and one edge of the square).

In the third case (both endpoints outside) we have the most difficult checks to perform. The given line segment may have no intersection with edges, it may have distinct intersections with two edges, or it might one intersection at a corner of the square (which zero length you might consider equivalent to missing the square entirely).

There is no apparent value to trying to package all those possible outcomes into one "algebraic equation", but if you explain why you need this, it can be done with a certain amount of complexity.