Intersection of line and rectangle with maximum segment length

Posted by Aarkan on Stack Overflow See other posts from Stack Overflow or by Aarkan
Published on 2012-03-18T17:18:41Z Indexed on 2012/03/18 18:03 UTC
Read the original article Hit count: 143

Filed under:

I have a vector represented by the slope m. Then there is rectangle (assume axis aligned), which is represented by top-left and bottom-right corner. Of course, there may be many lines with slope m and intersecting the given rectangle. The problem is to find out the line whose length of line intercept inside the rectangle is maximum among all such lines. i.e., if the line intersects rectangle at P1 and P2, then the problem is to find the equation of line for which length of P1P2 is maximum.

I proceeded like this. Let the line is: y = m*x + c. Then find out the intersection with each side of rectangle and finding out the maxima for distance function between each pair of points. But it will only give me the length of line segment and there seem to be many corner cases to handle.

Could anyone please suggest a better way to do this.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about math