1401. Segments Meet Test
Two cables are strung on a flat site. Each cable is a closed straight segment given as [x1, y1, x2, y2], the coordinates of its two end poles, and a cable may degenerate to a single point when both ends coincide. The inspector must decide whether the two cables touch.
Return true if the closed segments first and second share at least one point, including the case where they only touch at an endpoint or overlap along a stretch of the same line, and false otherwise. Use exact integer arithmetic only: take the orientation (sign of the cross product) of each endpoint against the other segment, reject when both endpoints of a segment lie strictly on the same side of the other, and also require the bounding boxes to overlap, which settles every collinear and degenerate case. The solution runs in O(1) time and O(1) space.
Example 1
- Input:
- first = [0,0,4,4], second = [0,4,4,0]
- Output:
- true
- Explanation:
The two diagonals of a square cross at (2,2).
Example 2
- Input:
- first = [0,0,2,0], second = [3,0,5,0]
- Output:
- false
- Explanation:
Both lie on the x-axis but are separated by a gap between x = 2 and x = 3.
Constraints
first.length == second.length == 4
-104 ≤ every coordinate ≤ 104
A segment may be a single point
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(1)
- Space
- O(1)