You are given two line segments. Do they cross?
Consider one segment as (x1,y1) to (x2,y2), the other segment as (x3,y3) to (x4,y4). You are given a = [x1 y1; x2 y2]; b = [x3 y3; x4 y4]. Return tf=true if a and b intersect or tf=false if a and b do not touch.
When lines do intersect they will do so cleanly at exactly one non-endpoint. That is, they will not nest, overlap, or "kiss" at the endpoints.
Examples
a = [0,0; 1,1];
b = [0,1; 1,0];
tf = true
a = [0,0; 1,0];
b = [0,1; 1,1];
tf = false
Solution Stats
Problem Comments
4 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers77
Suggested Problems
-
How to find the position of an element in a vector without using the find function
2816 Solvers
-
Reverse the Words (not letters) of a String
475 Solvers
-
Split a string into chunks of specified length
2122 Solvers
-
Cell Counting: How Many Draws?
2483 Solvers
-
Sum of first n positive integers
624 Solvers
More from this Author54
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
What about
a = [0,0; 2,2]; b = [1,1; 3,3];
?
Hi Celestino. I added a little bit more clarifying text to the problem statement. That's an interesting test case, but not one I will use for this problem. You could make your own extension to this problem, though!
Test Case 4 should be "true" since these lines intersect.
For Test Case 3, the two lines are on top of each other, so they will technically intersect infinite times :).
@Doddy
The question asks if the "line segments" intersect or not, not the whole line. For lines touching, it is clearly stated in the problem statement, that the line segments should not be overlapping and should intersect only at a single point.