Line Segment Intersection
Determine if two line segments intersect. Fundamental geometric primitive used in graphics, CAD, GIS. Uses orientation and collinearity tests.
Visualization
Interactive visualization for Line Segment Intersection
Line Intersection:
- • Check if 2 line segments intersect
- • Uses cross product
Interactive visualization with step-by-step execution
Implementation
1function doSegmentsIntersect(
2 p1: Point, q1: Point,
3 p2: Point, q2: Point
4): boolean {
5 const o1 = orientation(p1, q1, p2);
6 const o2 = orientation(p1, q1, q2);
7 const o3 = orientation(p2, q2, p1);
8 const o4 = orientation(p2, q2, q1);
9
10 // General case
11 if (o1 !== o2 && o3 !== o4) return true;
12
13 // Special cases: collinear and overlapping
14 if (o1 === 0 && onSegment(p1, p2, q1)) return true;
15 if (o2 === 0 && onSegment(p1, q2, q1)) return true;
16 if (o3 === 0 && onSegment(p2, p1, q2)) return true;
17 if (o4 === 0 && onSegment(p2, q1, q2)) return true;
18
19 return false;
20}
21
22function orientation(p: Point, q: Point, r: Point): number {
23 const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
24 if (val === 0) return 0; // collinear
25 return val > 0 ? 1 : 2; // clockwise or counterclockwise
26}
27
28function onSegment(p: Point, q: Point, r: Point): boolean {
29 return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&
30 q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
31}Deep Dive
Theoretical Foundation
Two segments intersect if: (1) they have different orientations on each other, OR (2) they are collinear and overlap. Orientation of ordered triplet (p,q,r): clockwise, counterclockwise, or collinear, determined by cross product sign.
Complexity
Time
O(1)
O(1)
O(1)
Space
O(1)
Applications
Industry Use
CAD systems (technical drawing)
Computer graphics (line clipping)
Geographic Information Systems (map overlay)
Collision detection in games and simulations
Ray tracing and rendering algorithms
Circuit board design and routing
Robotics (obstacle detection)
Computational geometry libraries
Use Cases
Related Algorithms
A* Search Algorithm
Informed search algorithm combining best-first search with Dijkstra's algorithm using heuristics. Widely used in pathfinding and graph traversal, A* is optimal and complete when using admissible heuristic. Used in games, GPS navigation, and robotics. Invented by Peter Hart, Nils Nilsson, and Bertram Raphael in 1968.
Convex Hull (Graham Scan)
Find smallest convex polygon containing all points. Graham Scan invented by Ronald Graham in 1972, runs in O(n log n). Essential in computational geometry, computer graphics, and pattern recognition.
Caesar Cipher
The Caesar Cipher is one of the oldest and simplest encryption techniques, named after Julius Caesar who used it to protect military messages around 100 BC. It works by shifting each letter in the plaintext by a fixed number of positions down the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. Despite being used for over 2000 years, it's extremely weak by modern standards with only 25 possible keys, making it trivially breakable by brute force or frequency analysis.
Vigenère Cipher
Polyalphabetic substitution cipher using keyword. Invented by Giovan Battista Bellaso in 1553, misattributed to Blaise de Vigenère. More secure than Caesar, resists simple frequency analysis.