Intersection
InterviewTime limit1sMemory limit128 MB
Decide, for each test case, whether a line segment and an axis-aligned rectangle share at least one point, including degenerate rectangles.
- Level
Medium6 of 10
- Topics
- Geometry, Implementation, Math, Brute force
- Solved
- No attempts yet
Problem
Write a program that determines whether a given line segment and rectangle intersect.
For example, suppose the segment has start point and end point , and the rectangle has one corner at and the opposite corner at . In this case the segment and the rectangle do not intersect.
The segment and the rectangle intersect if they share at least one point. All coordinates given in the input are integers whose absolute value is at most , but the point of intersection need not have integer coordinates. The rectangle's area may be (that is, it may degenerate to a segment or a single point).
Input
The first line contains the number of test cases . Each test case is given on a single line as eight space-separated integers xstart ystart xend yend xleft ytop xright ybottom.
- is the segment's start point and is its end point.
- and are the coordinates of two opposite corners of the rectangle.
The words left, right, top, and bottom in the variable names do not indicate actual directions; the names are merely a coincidence. Every coordinate is an integer with absolute value at most .
Output
For each test case, print T if the segment and the rectangle intersect, or F otherwise, one result per line. When both endpoints of the segment lie inside the rectangle, that also counts as intersecting, so print T.