Find the intersection of two lines: Difference between revisions

m
syntax highlighting fixup automation
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 15:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F line_intersect(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2)
V d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
I d == 0
Line 33:
V (e, f, g, h) = (0.0, 3.0, 10.0, 7.0)
V pt = line_intersect(a, b, c, d, e, f, g, h)
print(pt)</langsyntaxhighlight>
 
{{out}}
Line 42:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<langsyntaxhighlight lang="360asm">* Intersection of two lines 01/03/2019
INTERSEC CSECT
USING INTERSEC,R13 base register
Line 176:
PG DC CL80' '
REGEQU
END INTERSEC</langsyntaxhighlight>
{{out}}
<pre>
Line 184:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE REALPTR="CARD"
Line 282:
IntToReal(4,x4) IntToReal(5,y4)
Test(p1,p2,p3,p4)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_the_intersection_of_two_lines.png Screenshot from Atari 8-bit computer]
Line 298:
{{works with|Ada|Ada|2005}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Intersection_Of_Two_Lines
Line 342:
Ada.Text_IO.Put_Line(p.y'Img);
end Intersection_Of_Two_Lines;
</syntaxhighlight>
</lang>
{{out}}
<pre> 5.00000E+00 5.00000E+00
Line 349:
=={{header|ALGOL 68}}==
Using "school maths".
<langsyntaxhighlight lang="algol68">BEGIN
# mode to hold a point #
MODE POINT = STRUCT( REAL x, y );
Line 378:
print( ( fixed( x OF i, -8, 4 ), fixed( y OF i, -8, 4 ), newline ) )
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 384:
</pre>
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
⍝ APL has a powerful operator the « dyadic domino » to solve a system of N linear equations with N unknowns
⍝ We use it first to solve the a and b, defining the 2 lines as y = ax + b, with the x and y of the given points
Line 410:
solver ← {(,2 ¯1↑⍵)⌹(2 1↑⍵),1}
I ← solver 2 2⍴((¯1 1)×solver 2 2⍴A,B),(¯1 1)×solver 2 2⍴C,D
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 419:
=={{header|Arturo}}==
{{trans|Go}}
<langsyntaxhighlight lang="rebol">define :point [x,y][]
define :line [a, b][
init: [
Line 441:
l2: to :line @[to :point [0.0 3.0] to :point [10.0 7.0]]
 
print intersect l1 l2</langsyntaxhighlight>
 
{{out}}
Line 448:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">LineIntersectionByPoints(L1, L2){
x1 := L1[1,1], y1 := L1[1,2]
x2 := L1[2,1], y2 := L1[2,2]
Line 455:
return ((x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)) ", "
. ((x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4))
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">L1 := [[4,0], [6,10]]
L2 := [[0,3], [10,7]]
MsgBox % LineIntersectionByPoints(L1, L2)</langsyntaxhighlight>
Outputs:<pre>5.000000, 5.000000</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_THE_INTERSECTION_OF_TWO_LINES.AWK
# converted from Ring
Line 489:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 505:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight lang="gwbasic"> 0 A = 1:B = 2: HOME : VTAB 21: HGR : HCOLOR= 3: FOR L = A TO B: READ X1(L),Y1(L),X2(L),Y2(L): HPLOT X1(L),Y1(L) TO X2(L),Y2(L): NEXT : DATA4,0,6,10,0,3,10,7
1 GOSUB 5: IF NAN THEN PRINT "THE LINES DO NOT INTERSECT, THEY ARE EITHER PARALLEL OR CO-INCIDENT."
2 IF NOT NAN THEN PRINT "POINT OF INTERSECTION : "X" "Y
Line 514:
7 IF S$(A) = "NAN" AND S$(B) < > "NAN" THEN X = X1(A):Y = (X1(A) - X1(B)) * S(B) + Y1(B): RETURN
8 IF S$(B) = "NAN" AND S$(A) < > "NAN" THEN X = X1(B):Y = (X1(B) - X1(A)) * S(A) + Y1(A): RETURN
9 X = (S(A) * X1(A) - S(B) * X1(B) + Y1(B) - Y1(A)) / (S(A) - S(B)):Y = S(B) * (X - X1(B)) + Y1(B): RETURN</langsyntaxhighlight>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
Line 521:
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="qbasic">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
PRINT "The two lines are:"
Line 531:
PRINT "yab ="; y
PRINT "ycd ="; (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
PRINT "intersection: ("; x; ","; y; ")"</langsyntaxhighlight>
{{out}}
<pre>The two lines are:
Line 545:
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="freebasic">xa = 4 : xb = 6 : xc = 0 : xd = 10
ya = 0 : yb = 10 : yc = 3 : yd = 7
print "The two lines are:"
Line 555:
print "yab = "; y
print "ycd = "; (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc)))
print "intersection: ("; x; ", "; y ; ")"</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
Line 561:
{{works with|Liberty BASIC}}
{{works with|QBasic}}
<langsyntaxhighlight lang="lb">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
print "The two lines are:"
Line 571:
print "yab = "; y
print "ycd = "; (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
print "intersection: ("; x; ","; y; " )"</langsyntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
{{trans|REXX}} (version 1)
Works with 1k of RAM.
<langsyntaxhighlight lang="basic"> 10 LET XA=4
20 LET YA=0
30 LET XB=6
Line 592:
150 PRINT "YAB=";Y
160 PRINT "YCD=";YC-XC*((YD-YC)/(XD-XC))+X*((YD-YC)/(XD-XC))
170 PRINT "INTERSECTION: ";X;",";Y</langsyntaxhighlight>
{{out}}
<pre>THE TWO LINES ARE:
Line 607:
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<langsyntaxhighlight lang="qbasic">LET xa = 4
LET ya = 0
LET xb = 6
Line 624:
PRINT "ycd ="; (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc)))
PRINT "intersection: ("; x; ","; y ; " )"
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="freebasic">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
print "The two lines are:"
Line 637:
print "yab = ", y
print "ycd = ", (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
print "intersection: (", x, ", ", y, ")"</langsyntaxhighlight>
 
=={{header|C}}==
This implementation is generic and considers any two lines in the XY plane and not just the specified example. Usage is printed on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 732:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output:
<pre>
Line 740:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
public class Program
Line 764:
Console.WriteLine(FindIntersection(p(0f, 0f), p(1f, 1f), p(1f, 2f), p(4f, 5f)));
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 773:
=={{header|C++}}==
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <cassert>
Line 841:
assert(fabs(iy - 5.0) < eps);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 847:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">;; Point is [x y] tuple
(defn compute-line [pt1 pt2]
(let [[x1 y1] pt1
Line 860:
{:x x
:y (+ (* (:slope line1) x)
(:offset line1))}))</langsyntaxhighlight>
 
{{out}}
Line 873:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
;; Point is [x y] tuple
(defun point-of-intersection (x1 y1 x2 y2 x3 y3 x4 y4)
Line 885:
(list (/ (+ (* (- y3 y1) dx1 dx2) (* x1 dy1 dx2) (* -1 x3 dy2 dx1)) den)
(/ (+ (* (+ x3 x1) dy1 dy2) (* -1 y1 dx1 dy2) (* y3 dx2 dy1)) den) ))))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 894:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
struct Point {
Line 934:
l2 = Line(Point(1.0, 2.0), Point(4.0, 5.0));
writeln(findIntersection(l1, l2));
}</langsyntaxhighlight>
 
{{out}}
Line 941:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
(*
Find the point of intersection of 2 lines.
Line 953:
intersect (fn (4.0,0.0) (6.0,10.0)) (fn (0.0,3.0) (10.0,7.0))
intersect {a=3.18;b=4.23;c=7.13} {a=6.36;b=8.46;c=9.75}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 962:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: arrays combinators.extras kernel math
math.matrices.laplace math.vectors prettyprint sequences ;
 
Line 981:
{ 4 0 } { 6 10 } { 0 3 } { 10 7 } intersection .
{ 4 0 } { 6 10 } { 0 3 } { 10 7+1/10 } intersection .
{ 0 0 } { 1 1 } { 1 2 } { 4 5 } intersection .</langsyntaxhighlight>
{{out}}
<pre>
Line 991:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program intersect_two_lines
implicit none
Line 1,037:
write(*,*)x,y
end subroutine intersect
end program intersect_two_lines</langsyntaxhighlight>
{{out}}
<pre> 5.00000000 5.00000000 </pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 16-08-2017
' compile with: fbc -s console
#Define NaN 0 / 0 ' FreeBASIC returns -1.#IND
Line 1,086:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 5 5
Line 1,092:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">lineIntersection[x1, y1, x2, y2, x3, y3, x4, y4] :=
{
det = (x1 - x2)(y3 - y4) - (y1 - y2)(x3 - x4)
Line 1,105:
}
 
println[lineIntersection[4, 0, 6, 10, 0, 3, 10, 7]]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,113:
 
=={{header|Go}}==
<syntaxhighlight lang="go">
<lang go>
package main
 
Line 1,159:
}
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>{5 5}</pre>
Line 1,165:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Intersection {
private static class Point {
double x, y
Line 1,211:
println(findIntersection(l1, l2))
}
}</langsyntaxhighlight>
{{out}}
<pre>(5.0, 5.0)
Line 1,217:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">type Line = (Point, Point)
 
type Point = (Float, Float)
Line 1,252:
case interSection of
Left x -> x
Right x -> show x</langsyntaxhighlight>
{{Out}}
<pre>(5.0,5.0)</pre>
Line 1,259:
{{trans|C++}}
'''Solution:'''
<langsyntaxhighlight lang="j">det=: -/ .* NB. calculate determinant
findIntersection=: (det ,."1 [: |: -/"2) %&det -/"2</langsyntaxhighlight>
 
'''Examples:'''
<langsyntaxhighlight lang="j"> line1=: 4 0 ,: 6 10
line2=: 0 3 ,: 10 7
line3=: 0 3 ,: 10 7.1
Line 1,278:
__ __
findIntersection line6 ,: line7
2.5 1.5</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class Intersection {
private static class Point {
double x, y;
Line 1,328:
System.out.println(findIntersection(l1, l2));
}
}</langsyntaxhighlight>
{{out}}
<pre>{5.000000, 5.000000}
Line 1,336:
{{Trans|Haskell}}
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
// INTERSECTION OF TWO LINES ----------------------------------------------
Line 1,409:
]);
return show(lrIntersection.Left || lrIntersection.Right);
})();</langsyntaxhighlight>
{{Out}}
<pre>[5,5]</pre>
Line 1,418:
points P1 and P2. Array destructuring is used for simplicity.
 
<langsyntaxhighlight lang="jq"># determinant of 2x2 matrix
def det(a;b;c;d): a*d - b*c ;
 
Line 1,439:
then error("lineIntersect: parallel lines")
else [.xnom/.denom, .ynom/.denom]
end ;</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="jq">[[4.0, 0.0], [6.0,10.0]] | lineIntersection([[0.0, 3.0], [10.0, 7.0]])</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang ="jq">[5,5]</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,449:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="julia">struct Point{T}
x::T
y::T
Line 1,479:
l1 = Line(Point{Float64}(0, 0), Point{Float64}(1, 1))
l2 = Line(Point{Float64}(1, 2), Point{Float64}(4, 5))
println(intersection(l1, l2))</langsyntaxhighlight>
 
{{out}}
Line 1,486:
 
==== GeometryTypes library version ====
<langsyntaxhighlight lang="julia">using GeometryTypes
 
a = LineSegment(Point2f0(4, 0), Point2f0(6, 10))
b = LineSegment(Point2f0(0, 3), Point2f0(10, 7))
@show intersects(a, b) # --> intersects(a, b) = (true, Float32[5.0, 5.0])
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|C#}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class PointF(val x: Float, val y: Float) {
Line 1,524:
l2 = LineF(PointF(1f, 2f), PointF(4f, 5f))
println(findIntersection(l1, l2))
}</langsyntaxhighlight>
 
{{out}}
Line 1,534:
=={{header|Lua}}==
{{trans|C#}}
<langsyntaxhighlight lang="lua">function intersection (s1, e1, s2, e2)
local d = (s1.x - e1.x) * (s2.y - e2.y) - (s1.y - e1.y) * (s2.x - e2.x)
local a = s1.x * e1.y - s1.y * e1.x
Line 1,545:
local line1start, line1end = {x = 4, y = 0}, {x = 6, y = 10}
local line2start, line2end = {x = 0, y = 3}, {x = 10, y = 7}
print(intersection(line1start, line1end, line2start, line2end))</langsyntaxhighlight>
{{out}}
<pre>5 5</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Lineintersection (lineAtuple, lineBtuple) {
class line {
Line 1,580:
}
Lineintersection (4,0,6,10), (0,3,10,7) ' print 5 5
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,587:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">with(geometry):
line(L1, [point(A,[4,0]), point(B,[6,10])]):
line(L2, [point(C,[0,3]), point(E,[10,7])]):
coordinates(intersection(x,L1,L2));</langsyntaxhighlight>
{{Output|Out}}
<pre>[5, 5]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">RegionIntersection[
InfiniteLine[{{4, 0}, {6, 10}}],
InfiniteLine[{{0, 3}, {10, 7}}]
]</langsyntaxhighlight>
{{out}}
<pre>Point[{5, 5}]</pre>
 
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">
<lang MATLAB>
function cross=intersection(line1,line2)
a=polyfit(line1(:,1),line1(:,2),1);
Line 1,609:
cross=[a(1) -1; b(1) -1]\[-a(2);-b(2)];
end
</syntaxhighlight>
</lang>
{{out}}
<pre>line1=[4 0; 6 10]; line2=[0 3; 10 7]; cross=intersection(line1,line2)
Line 1,619:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE LineIntersection;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,675:
 
ReadChar;
END LineIntersection.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight lang="nim">type
Line = tuple
slope: float
Line 1,704:
line1 = createLine((0.0, 0.0), (1.0, 1.0))
line2 = createLine((1.0, 2.0), (4.0, 5.0))
echo intersection(line1, line2)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,715:
If warning are enabled the second print will issue a warning since we are trying to print out an undef
 
<langsyntaxhighlight lang="perl">
sub intersect {
my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = @_;
Line 1,736:
($ix, $iy) = intersect(0, 0, 1, 1, 1, 2, 4, 5);
print "$ix $iy\n";
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/intersect.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">X</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Y</span>
Line 1,766:
<span style="color: #000000;">intersect</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- "parallel lines/do not intersect"</span>
<span style="color: #000000;">intersect</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {2.5,1.5}</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
 
<langsyntaxhighlight lang="java">void setup() {
// test lineIntersect() with visual and textual output
float lineA[] = {4, 0, 6, 10}; // try 4, 0, 6, 4
Line 1,805:
float y = Ay1 + uA * (Ay2 - Ay1);
return new PVector(x, y);
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
 
<langsyntaxhighlight lang="python">from __future__ import division
 
def setup():
Line 1,837:
x = Ax1 + uA * (Ax2 - Ax1)
y = Ay1 + uA * (Ay2 - Ay1)
return x, y</langsyntaxhighlight>
 
=={{header|Python}}==
Find the intersection without importing third-party libraries.
<langsyntaxhighlight 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 """
d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
Line 1,860:
(e, f), (g, h) = (0, 3), (10, 7) # for non intersecting test
pt = line_intersect(a, b, c, d, e, f, g, h)
print(pt)</langsyntaxhighlight>
 
{{Out}}
Line 1,869:
 
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''The intersection of two lines.'''
 
from itertools import product
Line 1,984:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{out}}
<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.
<langsyntaxhighlight lang="python">from shapely.geometry import LineString
 
if __name__ == "__main__":
line1 = LineString([(4, 0), (6, 10)])
line2 = LineString([(0, 3), (10, 7)])
print(line1.intersection(line2))</langsyntaxhighlight>
{{out}}
<pre>POINT (5 5)</pre>
Line 2,000:
=={{header|Racket}}==
{{trans|C++}}
<langsyntaxhighlight lang="racket">#lang racket/base
(define (det a b c d) (- (* a d) (* b c))) ; determinant
 
Line 2,018:
 
(module+ test (line-intersect 4 0 6 10
0 3 10 7))</langsyntaxhighlight>
 
{{out}}
Line 2,029:
{{trans|zkl}}
 
<syntaxhighlight lang="raku" perl6line>sub intersection (Real $ax, Real $ay, Real $bx, Real $by,
Real $cx, Real $cy, Real $dx, Real $dy ) {
 
Line 2,054:
say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7 );
say 'Intersection point: ', intersection( 4,0, 6,10, 0,3, 10,7.1 );
say 'Intersection point: ', intersection( 0,0, 1,1, 1,2, 4,5 );</langsyntaxhighlight>
{{out}}
<pre>Intersection point: (5 5)
Line 2,065:
Naive implementation.
To be improved for parallel lines and degenerate lines such as y=5 or x=8.
<langsyntaxhighlight lang="rexx">/* REXX */
Parse Value '(4.0,0.0)' With '(' xa ',' ya ')'
Parse Value '(6.0,10.0)' With '(' xb ',' yb ')'
Line 2,081:
Say 'yab='y
Say 'ycd='yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc))
Say 'Intersection: ('||x','y')'</langsyntaxhighlight>
{{out}}
<pre>The two lines are:
Line 2,095:
<br>
Variables are named after the Austrian notation for a straight line: y=k*x+d
<langsyntaxhighlight lang="rexx">say ggx1('4.0 0.0 6.0 10.0 0.0 3.0 10.0 7.0')
say ggx1('0.0 0.0 0.0 10.0 0.0 3.0 10.0 7.0')
say ggx1('0.0 0.0 0.0 10.0 0.0 3.0 10.0 7.0')
Line 2,166:
If res='' Then /* not any special case */
res='Intersection is ('||x'/'y')' /* that's the result */
Return ' -->' res</langsyntaxhighlight>
{{out}}
<pre>A=(4.0/0.0) B=(6.0/10.0) C=(0.0/3.0) D=(10.0/7.0)
Line 2,184:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Find the intersection of two lines
 
Line 2,204:
see "ycd=" + (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc))) + nl
see "intersection: " + x + "," + y + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,217:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">Point = Struct.new(:x, :y)
 
class Line
Line 2,244:
 
puts "Line #{l1} intersects line #{l2} at #{l1.intersect(l2)}."
</syntaxhighlight>
</lang>
{{out}}
<pre>Line y = 5.0x + -20.0 intersects line y = 0.4x + 3.0 at #<struct Point x=5.0, y=5.0>.</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">#[derive(Copy, Clone, Debug)]
struct Point {
x: f64,
Line 2,295:
let l2 = Line(Point::new(1.0, 2.0), Point::new(4.0, 5.0));
println!("{:?}", l1.intersect(l2));
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,303:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Intersection extends App {
val (l1, l2) = (LineF(PointF(4, 0), PointF(6, 10)), LineF(PointF(0, 3), PointF(10, 7)))
 
Line 2,332:
println(findIntersection(l01, l02))
 
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/DAqMtEx/0 (JavaScript)]
or by [https://scastie.scala-lang.org/WQOqakOlQnaBRFBa1PuRYw Scastie (JVM)].
Line 2,339:
{{trans|Raku}}
 
<langsyntaxhighlight lang="ruby">func det(a, b, c, d) { a*d - b*c }
 
func intersection(ax, ay, bx, by,
Line 2,362:
say ('Intersection point: ', intersection(4,0, 6,10, 0,3, 10,7))
say ('Intersection point: ', intersection(4,0, 6,10, 0,3, 10,7.1))
say ('Intersection point: ', intersection(0,0, 1,1, 1,2, 4,5))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,371:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">struct Point {
var x: Double
var y: Double
Line 2,406:
let l2 = Line(p1: Point(x: 0.0, y: 3.0), p2: Point(x: 10.0, y: 7.0))
 
print("Intersection at : \(l1.intersection(of: l2)!)")</langsyntaxhighlight>
{{out}}
<pre>Intersection at : Point(x: 5.0, y: 5.0)</pre>
Line 2,414:
{{trans|Rexx}}
Simple version:
<langsyntaxhighlight lang="ti83b">[[4,0][6,10][0,3][10,7]]→[A]
([A](2,2)-[A](1,2))/([A](2,1)-[A](1,1))→B
[A](1,2)-[A](1,1)*B→A
Line 2,422:
A+X*B→Y
C+X*D→Z
Disp {X,Y}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,428:
</pre>
Full version:
<langsyntaxhighlight lang="ti83b">[[4,0][6,10][0,3][10,7]]→[A]
{4,2}→Dim([B])
0→M
Line 2,481:
End
End
Disp {X,Y,M}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,492:
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<langsyntaxhighlight lang="vb">Option Explicit
 
Public Type Point
Line 2,557:
Debug.Assert ip.invalid
End Sub</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Drawing
 
Module Module1
Line 2,586:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>{X=5, Y=5}
Line 2,593:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">class Point {
construct new(x, y) {
_x = x
Line 2,634:
l1 = Line.new(Point.new(0, 0), Point.new(1, 1))
l2 = Line.new(Point.new(1, 2), Point.new(4, 5))
System.print(findIntersection.call(l1, l2))</langsyntaxhighlight>
 
{{out}}
Line 2,643:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func real Det; real A0, B0, A1, B1;
return A0*B1 - A1*B0;
 
Line 2,659:
M1:= (L1(1,1) - L1(0,1)) / (L1(1,0) - L1(0,0));
Cramer(M0, -1., M0*L0(0,0)-L0(0,1), M1, -1., M1*L1(0,0)-L1(0,1));
]</langsyntaxhighlight>
 
{{out}}
Line 2,668:
=={{header|zkl}}==
{{trans|C++}}
<langsyntaxhighlight lang="zkl">fcn lineIntersect(ax,ay, bx,by, cx,cy, dx,dy){ // --> (x,y)
detAB,detCD := det(ax,ay, bx,by), det(cx,cy, dx,dy);
abDx,cdDx := ax - bx, cx - dx; // delta x
Line 2,680:
return(xnom/denom, ynom/denom);
}
fcn det(a,b,c,d){ a*d - b*c } // determinant</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">lineIntersect(4.0,0.0, 6.0,10.0, 0.0,3.0, 10.0,7.0).println();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits