# The length of a line intersecting a rectangle - not just diagonal!

1. Jan 2, 2012

### renucrew

Hi

Given a simple linear equation and the extents of a rectangle, is there a neat way of finding the length of the line section that is contained within the rectangle?

The only general method i can think of would involve a bunch of conditional statements to determine whether the line intersects each side of the rectangle.

2. Jan 2, 2012

### checkitagain

Example:

A rectangle bounded by:

x = a

x = b

y = c

y = d

And the line could be:

y = mx + f **

I do not want to use "e," as it used for a constant.

3. Jan 3, 2012

### renucrew

Hi there, yes i simplified the problem so that one vertex of the rectangle is at the origin and the extents are positive; the length of any line intersecting any rectangle could then be solved by just adjusting the line intercept and gradient to account for the position and orientation if the rectangle is not at the origin or is rotated.

Actually perhaps it would be easier to assume the rectangle is centered at the origin instead.

4. Jan 3, 2012

### renucrew

Maybe considering the rectangle to be a line section in the complex plane could help!??!

5. Jan 3, 2012

6. Jan 3, 2012

### renucrew

Thanks,

Yeah its just normal Cartesian system. Yeah i've looked and only found algorithmic solutions. I think im probably being stupid, but I was after a mathematical expression that could used in various analyses.

7. Jan 3, 2012

### chiro

Hey renucrew and welcome to the forums.

Because the surface/volume/whatever you are talking about is not analytic-continuous/differentiable over the whole surface, you have to do multiple tests instead of one generic test.

As a case of where you could do this, something like a sphere or an ellipse could be used as a single test because it has the required properties, but unfortunately you can't do this with your rectangle.