Another way to shortcut this would be to go through the rectangle in the following order: F(topleft),F(topright),F(bottomright),F(bottomleft) and then to check if any of those equation's signs is different from the previous one, meaning that one point is 'below' and the next is 'above' the line.

**2018年09月26日00分27秒**

Very well explained, and it seems to handle the case where the segment is completely enclosed by the box.

**2018年09月26日00分27秒**

I have F(x, y) < 0 as above the line. Although it doesn't make a difference to the algorithm.

**2018年09月26日00分27秒**

Why is the step B necessary? I can't think of case when some corners are on different sides of the line and the line does not intersect the rectangle.

**2018年09月25日00分27秒**

jnovacho, I guess is because it's not really a line it's a segment with endpoints. Even if the line over the segment intersects the segment may not.

**2018年09月26日00分27秒**

Well done. Very helpful.

**2018年09月25日00分27秒**

Upvote. I tried the top answer, but my test against putting a box on top of a line going from 100 50 to 100 100 failed.

**2018年09月26日00分27秒**

This is really simple and works great! I did a javascript test: jsfiddle.net/77eej/2

**2018年09月26日00分27秒**

Because floating point math is inaccurate. Two numbers that mathematically should be equal can differ by a very small amount, causing equality comparisons to fail.

**2018年09月26日00分27秒**

does not work in some cases, try on a box [0,0 100,100], with points [25,125] and [101,100] and see that it will return true. But the segment is clearly outside.

**2018年09月26日00分27秒**

This is too simple and over-eager. It will collect false positives where the start of the line overlaps in x but not y, and the end of the line overlaps in y, but not x (or vice versa).

**2018年09月26日00分27秒**

What is the license?

**2018年09月26日00分27秒**

The question is about line-rect intersection, not rect-rect.

**2018年09月25日00分27秒**

- Intersection of a 2D segment with a rectangle
- Detect if two polygons have an area in common
- removing subview from superview when user moves very fast
- Intersect quadrilateral with axis-aligned rectangle?
- Overlap AABB-Arc and AABB-Pie
- How to register mouseover/enter event for scrolling past (jumping over) elements?
- Find list of pixels between two points
- How can I make a sprite unable to pass through lines?

ADS