Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Determine if two line segments intersect
ResourceFunction["SegmentIntersectionQ"][{a,b},{c,d}] determines if line segments | |
ResourceFunction["SegmentIntersectionQ"][{{a,b},{c,d}}] determines if line segments | |
Determine if two segments intersect:
| In[1]:= |
| Out[1]= |
Show a trapezoid:
| In[2]:= |
| Out[2]= | ![]() |
Determine if diagonal segments of a trapezoid intersect:
| In[3]:= |
| Out[3]= |
Determine if the non-parallel segments of a trapezoid intersect:
| In[4]:= |
| Out[4]= |
Determine if neighboring segments of a trapezoid intersect:
| In[5]:= |
| Out[5]= |
Determine an edge-case degeneracy:
| In[6]:= |
| Out[6]= |
Show it:
| In[7]:= |
| Out[7]= | ![]() |
SegmentIntersectionQ works in 3D:
| In[8]:= | ![]() |
| Out[10]= |
Show the lines:
| In[11]:= |
| Out[11]= | ![]() |
Various 2D intersections:
| In[12]:= | ![]() |
| Out[12]= |
Various 2D non-intersections:
| In[13]:= | ![]() |
| Out[13]= |
Various 3D intersections:
| In[14]:= | ![]() |
| Out[14]= |
Various 3D non-intersections:
| In[15]:= | ![]() |
| Out[15]= |
Find disjoint segment pairs from a set of twelve points:
| In[16]:= | ![]() |
With timings, find all segment pairs that intersect:
| In[17]:= |
| Out[17]= |
With timings, use RegionIntersection to find all segment pairs that intersect:
| In[18]:= | ![]() |
| Out[18]= |
Note the timing difference and compare the two results:
| In[19]:= |
| Out[19]= |
Show the results:
| In[20]:= | ![]() |
| Out[21]= | ![]() |
Various bad inputs will not work:
| In[22]:= |
| Out[22]= |
Degenerate overlaps are returned as intersections:
| In[23]:= |
| Out[23]= |
Of the 210 possible disjoint pairs of the 21 segments defined by 7 points, pick out those that intersect:
| In[24]:= | ![]() |
| Out[25]= | ![]() |
Nine crossings happens to be the minimum for seven points. Show it:
| In[26]:= | ![]() |
| Out[27]= | ![]() |
For 27 points, the minimal number of intersections is 6180. Set it up:
| In[28]:= | ![]() |
Find the crossings:
| In[29]:= |
| Out[29]= |
Show it:
| In[30]:= | ![]() |
| Out[31]= | ![]() |
Wolfram Language 14.0 (January 2024) or above
This work is licensed under a Creative Commons Attribution 4.0 International License