Find the intersection of two lines: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Reorganize examples--native, internal lib, external lib) |
(→{{header|Python}}: Describe approaches (proced/func) and mark library w header) |
||
Line 1,356: | Line 1,356: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
Find the intersection procedurally, with no imports. |
|||
Simpler version |
|||
<lang python>def line_intersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2): |
<lang python>def line_intersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2): |
||
""" returns a (x, y) tuple or None if there is no intersection """ |
""" returns a (x, y) tuple or None if there is no intersection """ |
||
Line 1,380: | Line 1,380: | ||
{{Out}} |
{{Out}} |
||
<pre>(5.0, 5.0)</pre> |
<pre>(5.0, 5.0)</pre> |
||
Or, one approach to doing this by hand, without importing special libraries: |
|||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
Find the intersection using a functional approach, by importing [https://docs.python.org/3/library/functools.html functools.reduce] |
|||
<lang python>'''The intersection of two lines.''' |
<lang python>'''The intersection of two lines.''' |
||
Line 1,497: | Line 1,497: | ||
{{out}} |
{{out}} |
||
<pre>(5.0, 5.0)</pre> |
<pre>(5.0, 5.0)</pre> |
||
{{libheader|Shapely}} |
|||
Find the intersection by importing the external [https://shapely.readthedocs.io/en/latest/manual.html Shapely] library. |
|||
<lang python>from shapely.geometry import LineString |
<lang python>from shapely.geometry import LineString |
||