Find the intersection of two lines: Difference between revisions
Find the intersection of two lines (view source)
Revision as of 16:03, 11 January 2024
, 4 months agoAdded Easylang
(Added solution for Action!) |
(Added Easylang) |
||
(25 intermediate revisions by 18 users not shown) | |||
Line 15:
{{trans|Python}}
<
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)</
{{out}}
Line 42:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<
INTERSEC CSECT
USING INTERSEC,R13 base register
Line 176:
PG DC CL80' '
REGEQU
END INTERSEC</
{{out}}
<pre>
Line 184:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
DEFINE REALPTR="CARD"
TYPE PointR=[REALPTR x,y]
PROC Det(REAL POINTER x1,y1,x2,y2,res)
Line 281 ⟶ 282:
IntToReal(4,x4) IntToReal(5,y4)
Test(p1,p2,p3,p4)
RETURN</
{{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 297 ⟶ 298:
{{works with|Ada|Ada|2005}}
<
procedure Intersection_Of_Two_Lines
Line 341 ⟶ 342:
Ada.Text_IO.Put_Line(p.y'Img);
end Intersection_Of_Two_Lines;
</syntaxhighlight>
{{out}}
<pre> 5.00000E+00 5.00000E+00
Line 348 ⟶ 349:
=={{header|ALGOL 68}}==
Using "school maths".
<
# mode to hold a point #
MODE POINT = STRUCT( REAL x, y );
Line 377 ⟶ 378:
print( ( fixed( x OF i, -8, 4 ), fixed( y OF i, -8, 4 ), newline ) )
END</
{{out}}
<pre>
Line 383 ⟶ 384:
</pre>
=={{header|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 409 ⟶ 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>
{{out}}
<pre>
Line 415 ⟶ 416:
5 5
</pre>
=={{header|Arturo}}==
{{trans|Go}}
<syntaxhighlight lang="rebol">define :point [x,y][]
define :line [a, b][
init: [
this\slope: div this\b\y-this\a\y this\b\x-this\a\x
this\yInt: this\a\y - this\slope*this\a\x
]
]
evalX: function [line, x][
line\yInt + line\slope * x
]
intersect: function [line1, line2][
x: div line2\yInt-line1\yInt line1\slope-line2\slope
y: evalX line1 x
to :point @[x y]
]
l1: to :line @[to :point [4.0 0.0] to :point [6.0 10.0]]
l2: to :line @[to :point [0.0 3.0] to :point [10.0 7.0]]
print intersect l1 l2</syntaxhighlight>
{{out}}
<pre>[x:5.0 y:5.0]</pre>
=={{header|AutoHotkey}}==
<
x1 := L1[1,1], y1 := L1[1,2]
x2 := L1[2,1], y2 := L1[2,2]
Line 424 ⟶ 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))
}</
Examples:<
L2 := [[0,3], [10,7]]
MsgBox % LineIntersectionByPoints(L1, L2)</
Outputs:<pre>5.000000, 5.000000</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_THE_INTERSECTION_OF_TWO_LINES.AWK
# converted from Ring
Line 458 ⟶ 489:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 473 ⟶ 504:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight 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
3 PRINT CHR$ (13)"HIT ANY KEY TO END PROGRAM": IF NOT NAN THEN FOR K = 0 TO 1 STEP 0:C = C = 0: HCOLOR= 3 * C: HPLOT X,Y: FOR I = 1 TO 30:K = PEEK (49152) > 127: NEXT I,K
4 GET K$: TEXT : END
5 FOR L = A TO B:S$(L) = "NAN": IF X1(L) < > X2(L) THEN S(L) = (Y1(L) - Y2(L)) / (X1(L) - X2(L)):S$(L) = STR$ (S(L))
6 NEXT L:NAN = S$(A) = S$(B): IF NAN THEN RETURN
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</syntaxhighlight>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|BASIC256}}
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
PRINT "The two lines are:"
PRINT "yab ="; (ya - xa * ((yb - ya) / (xb - xa))); "+ x*"; ((yb - ya) / (xb - xa))
PRINT "ycd ="; (yc - xc * ((yd - yc) / (xd - xc))); "+ x*"; ((yd - yc) / (xd - xc))
x = ((yc - xc * ((yd - yc) / (xd - xc))) - (ya - xa * ((yb - ya) / (xb - xa)))) / (((yb - ya) / (xb - xa)) - ((yd - yc) / (xd - xc)))
PRINT "x ="; x
y = ya - xa * ((yb - ya) / (xb - xa)) + x * ((yb - ya) / (xb - xa))
PRINT "yab ="; y
PRINT "ycd ="; (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
PRINT "intersection: ("; x; ","; y; ")"</syntaxhighlight>
{{out}}
<pre>The two lines are:
yab =-20 + x* 5
ycd = 3 + x* 0.4
x = 5
yab = 5
ycd = 5
intersection: ( 5, 5 )</pre>
==={{header|BASIC256}}===
{{works with|QBasic}}
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="freebasic">xa = 4 : xb = 6 : xc = 0 : xd = 10
ya = 0 : yb = 10 : yc = 3 : yd = 7
print "The two lines are:"
print "yab = "; (ya-xa*((yb-ya)/(xb-xa))); " + x*"; ((yb-ya)/(xb-xa))
print "ycd = "; (yc-xc*((yd-yc)/(xd-xc))); " + x*"; ((yd-yc)/(xd-xc))
x = ((yc-xc*((yd-yc)/(xd-xc)))-(ya-xa*((yb-ya)/(xb-xa))))/(((yb-ya)/(xb-xa))-((yd-yc)/(xd-xc)))
print "x = "; x
y = ya-xa*((yb-ya)/(xb-xa))+x*((yb-ya)/(xb-xa))
print "yab = "; y
print "ycd = "; (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc)))
print "intersection: ("; x; ", "; y ; ")"</syntaxhighlight>
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">define xa = 4, xb = 6, xc = 0, xd = 10
define ya = 0, yb = 10, yc = 3, yd = 7
print "The two lines are:"
print "yab = ", (ya - xa * ((yb - ya) / (xb - xa))), " + x * ", ((yb - ya) / (xb - xa))
print "ycd = ", (yc - xc * ((yd - yc) / (xd - xc))), " + x * ", ((yd - yc) / (xd - xc))
let x = ((yc - xc * ((yd - yc) / (xd - xc))) - (ya - xa * ((yb - ya) / (xb - xa)))) / (((yb - ya) / (xb - xa)) - ((yd - yc) / (xd - xc)))
print "x = ", x
let y = ya - xa * ((yb - ya) / (xb - xa)) + x * ((yb - ya) / (xb - xa))
print "yab = ", y
print "ycd = ", (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
print "intersection: (", x, comma, " ", y, ")"</syntaxhighlight>
{{out| Output}}<pre>The two lines are:
yab = -20 + x * 5
ycd = 3 + x * 0.4000
x = 5
yab = 5
ycd = 5
intersection: (5, 5)</pre>
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="lb">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
print "The two lines are:"
print "yab = "; (ya - xa * ((yb - ya) / (xb - xa))); "+ x*"; ((yb - ya) / (xb - xa))
print "ycd = "; (yc - xc * ((yd - yc) / (xd - xc))); "+ x*"; ((yd - yc) / (xd - xc))
x = ((yc - xc * ((yd - yc) / (xd - xc))) - (ya - xa * ((yb - ya) / (xb - xa)))) / (((yb - ya) / (xb - xa)) - ((yd - yc) / (xd - xc)))
print "x = "; x
y = ya - xa * ((yb - ya) / (xb - xa)) + x * ((yb - ya) / (xb - xa))
print "yab = "; y
print "ycd = "; (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
print "intersection: ("; x; ","; y; " )"</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
{{trans|REXX}} (version 1)
Works with 1k of RAM.
<
20 LET YA=0
30 LET XB=6
Line 492 ⟶ 613:
150 PRINT "YAB=";Y
160 PRINT "YCD=";YC-XC*((YD-YC)/(XD-XC))+X*((YD-YC)/(XD-XC))
170 PRINT "INTERSECTION: ";X;",";Y</
{{out}}
<pre>THE TWO LINES ARE:
Line 501 ⟶ 622:
YCD=5
INTERSECTION: 5,5</pre>
==={{header|True BASIC}}===
{{works with|QBasic}}
{{works with|BASIC256}}
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">LET xa = 4
LET ya = 0
LET xb = 6
LET yb = 10
LET xc = 0
LET yc = 3
LET xd = 10
LET yd = 7
PRINT "The two lines are:"
PRINT "yab ="; (ya-xa*((yb-ya)/(xb-xa))); " + x*"; ((yb-ya)/(xb-xa))
PRINT "ycd ="; (yc-xc*((yd-yc)/(xd-xc))); " + x*"; ((yd-yc)/(xd-xc))
LET x = ((yc-xc*((yd-yc)/(xd-xc)))-(ya-xa*((yb-ya)/(xb-xa))))/(((yb-ya)/(xb-xa))-((yd-yc)/(xd-xc)))
PRINT "x ="; x
LET y = ya-xa*((yb-ya)/(xb-xa))+x*((yb-ya)/(xb-xa))
PRINT "yab ="; y
PRINT "ycd ="; (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc)))
PRINT "intersection: ("; x; ","; y ; " )"
END</syntaxhighlight>
==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">xa = 4: xb = 6: xc = 0: xd = 10
ya = 0: yb = 10: yc = 3: yd = 7
print "The two lines are:"
print "yab = ", (ya - xa * ((yb - ya) / (xb - xa))), " + x*", ((yb - ya) / (xb - xa))
print "ycd = ", (yc - xc * ((yd - yc) / (xd - xc))), " + x*", ((yd - yc) / (xd - xc))
x = ((yc - xc * ((yd - yc) / (xd - xc))) - (ya - xa * ((yb - ya) / (xb - xa)))) / (((yb - ya) / (xb - xa)) - ((yd - yc) / (xd - xc)))
print "x = ", x
y = ya - xa * ((yb - ya) / (xb - xa)) + x * ((yb - ya) / (xb - xa))
print "yab = ", y
print "ycd = ", (yc - xc * ((yd - yc) / (xd - xc)) + x * ((yd - yc) / (xd - xc)))
print "intersection: (", x, ", ", y, ")"</syntaxhighlight>
=={{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">
#include<stdlib.h>
#include<stdio.h>
Line 595 ⟶ 753:
return 0;
}
</syntaxhighlight>
Invocation and output:
<pre>
Line 602 ⟶ 760:
</pre>
=={{header|C sharp|C#}}==
<
using System.Drawing;
public class Program
Line 627 ⟶ 785:
Console.WriteLine(FindIntersection(p(0f, 0f), p(1f, 1f), p(1f, 2f), p(4f, 5f)));
}
}</
{{out}}
<pre>
Line 636 ⟶ 794:
=={{header|C++}}==
<
#include <cmath>
#include <cassert>
Line 704 ⟶ 862:
assert(fabs(iy - 5.0) < eps);
return 0;
}</
{{out}}
Line 710 ⟶ 868:
=={{header|Clojure}}==
<
(defn compute-line [pt1 pt2]
(let [[x1 y1] pt1
Line 723 ⟶ 881:
{:x x
:y (+ (* (:slope line1) x)
(:offset line1))}))</
{{out}}
Line 734 ⟶ 892:
(intercept line1 line2) ; {:x 5, :y 5}
</pre>
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
;; Point is [x y] tuple
(defun point-of-intersection (x1 y1 x2 y2 x3 y3 x4 y4)
"Find the point of intersection of the lines defined by the points (x1 y1) (x2 y2) and (x3 y3) (x4 y4)"
(let* ((dx1 (- x2 x1))
(dx2 (- x4 x3))
(dy1 (- y2 y1))
(dy2 (- y4 y3))
(den (- (* dy1 dx2) (* dy2 dx1))) )
(unless (zerop den)
(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>
{{out}}
<pre>
(point-of-intersection 4 0 6 10 0 3 10 7) => (5 5)
</pre>
=={{header|D}}==
{{trans|Kotlin}}
<
struct Point {
Line 777 ⟶ 955:
l2 = Line(Point(1.0, 2.0), Point(4.0, 5.0));
writeln(findIntersection(l1, l2));
}</
{{out}}
<pre>{5.000000, 5.000000}
{-inf, -inf}</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This subroutine not only finds the intersection, it test for various degenerate condition where the intersection can fail.
<syntaxhighlight lang="Delphi">
{Vector structs and operations - these would normally be in}
{a library, but are produced here so everything is explicit}
type T2DVector=packed record
X,Y: double;
end;
type T2DLine = packed record
P1,P2: T2DVector;
end;
function MakeVector2D(const X,Y: double): T2DVector;
begin
Result.X:=X;
Result.Y:=Y;
end;
function MakeLine2D(X1,Y1,X2,Y2: double): T2DLine;
begin
Result.P1:=MakeVector2D(X1,Y1);
Result.P2:=MakeVector2D(X2,Y2);
end;
function DoLinesIntersect2D(L1,L2: T2DLine; var Point: T2DVector): boolean;
{Finds intersect point only if the lines actually intersect}
var distAB, theCos, theSin, newX, ABpos: double;
begin
Result:=False;
{ Fail if either line segment is zero-length.}
if (L1.P1.X=L1.P2.X) and (L1.P1.Y=L1.P2.Y) or
(L2.P1.X=L2.P2.X) and (L2.P1.Y=L2.P2.Y) then exit;
{ Fail if the segments share an end-point.}
if (L1.P1.X=L2.P1.X) and (L1.P1.Y=L2.P1.Y) or
(L1.P2.X=L2.P1.X) and (L1.P2.Y=L2.P1.Y) or
(L1.P1.X=L2.P2.X) and (L1.P1.Y=L2.P2.Y) or
(L1.P2.X=L2.P2.X) and (L1.P2.Y=L2.P2.Y) then exit;
{ (1) Translate the system so that point A is on the origin.}
L1.P2.X:=L1.P2.X-L1.P1.X; L1.P2.Y:=L1.P2.Y-L1.P1.Y;
L2.P1.X:=L2.P1.X-L1.P1.X; L2.P1.Y:=L2.P1.Y-L1.P1.Y;
L2.P2.X:=L2.P2.X-L1.P1.X; L2.P2.Y:=L2.P2.Y-L1.P1.Y;
{ Discover the length of segment A-B.}
distAB:=sqrt(L1.P2.X*L1.P2.X+L1.P2.Y*L1.P2.Y);
{ (2) Rotate the system so that point B is on the positive X L1.P1.Xis.}
theCos:=L1.P2.X/distAB;
theSin:=L1.P2.Y/distAB;
newX:=L2.P1.X*theCos+L2.P1.Y*theSin;
L2.P1.Y :=L2.P1.Y*theCos-L2.P1.X*theSin; L2.P1.X:=newX;
newX:=L2.P2.X*theCos+L2.P2.Y*theSin;
L2.P2.Y :=L2.P2.Y*theCos-L2.P2.X*theSin; L2.P2.X:=newX;
{ Fail if segment C-D doesn't cross line A-B.}
if (L2.P1.Y<0) and (L2.P2.Y<0) or (L2.P1.Y>=0) and (L2.P2.Y>=0) then exit;
{ (3) Discover the position of the intersection point along line A-B.}
ABpos:=L2.P2.X+(L2.P1.X-L2.P2.X)*L2.P2.Y/(L2.P2.Y-L2.P1.Y);
{ Fail if segment C-D crosses line A-B outside of segment A-B.}
if (ABpos<0) or (ABpos>distAB) then exit;
{ (4) Apply the discovered position to line A-B in the original coordinate system.}
Point.X:=L1.P1.X+ABpos*theCos;
Point.Y:=L1.P1.Y+ABpos*theSin;
Result:=True;
end;
procedure TestIntersect(Memo: TMemo; L1,L2: T2DLine);
var Int: T2DVector;
var S: string;
begin
Memo.Lines.Add('Line-1: '+Format('(%1.0f,%1.0f)->(%1.0f,%1.0f)',[L1.P1.X,L1.P1.Y,L1.P2.X,L1.P2.Y]));
Memo.Lines.Add('Line-2: '+Format('(%1.0f,%1.0f)->(%1.0f,%1.0f)',[L2.P1.X,L2.P1.Y,L2.P2.X,L2.P2.Y]));
if DoLinesIntersect2D(L1,L2,Int) then Memo.Lines.Add(Format('Intersect = %2.1f %2.1f',[Int.X,Int.Y]))
else Memo.Lines.Add('No Intersect.');
end;
procedure TestLineIntersect(Memo: TMemo);
var L1,L2: T2DLine;
var S: string;
begin
L1:=MakeLine2D(4,0,6,10);
L2:=MakeLine2D(0,3,10,7);
TestIntersect(Memo,L1,L2);
Memo.Lines.Add('');
L1:=MakeLine2D(0,0,1,1);
L2:=MakeLine2D(1,2,4,5);
TestIntersect(Memo,L1,L2);
end;
</syntaxhighlight>
{{out}}
<pre>
Line-1: (4,0)->(6,10)
Line-2: (0,3)->(10,7)
Intersect = 5.0 5.0
Line-1: (0,0)->(1,1)
Line-2: (1,2)->(4,5)
No Intersect.
</pre>
=={{header|EasyLang}}==
{{trans|Python}}
<syntaxhighlight>
proc intersect ax1 ay1 ax2 ay2 bx1 by1 bx2 by2 . rx ry .
rx = 1 / 0
ry = 1 / 0
d = (by2 - by1) * (ax2 - ax1) - (bx2 - bx1) * (ay2 - ay1)
if d = 0
return
.
ua = ((bx2 - bx1) * (ay1 - by1) - (by2 - by1) * (ax1 - bx1)) / d
ub = ((ax2 - ax1) * (ay1 - by1) - (by2 - by1) * (ax1 - bx1)) / d
if abs ua > 1 or abs ub > 1
return
.
rx = ax1 + ua * (ax2 - ax1)
ry = ay1 + ua * (ay2 - ay1)
.
intersect 4 0 6 10 0 3 10 7 rx ry
print rx & " " & ry
intersect 4 0 6 10 0 3 10 7.1 rx ry
print rx & " " & ry
intersect 0 0 1 1 1 2 4 5 rx ry
print rx & " " & ry
</syntaxhighlight>
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
;; y = a*x + b
(let ()
(defun line-prop (p1 p2)
(let* ((prop-a (/ (- (plist-get p2 'y) (plist-get p1 'y))
(- (plist-get p2 'x) (plist-get p1 'x))))
(prop-b (- (plist-get p1 'y) (* prop-a (plist-get p1 'x)))))
(list 'a prop-a 'b prop-b) ) )
(defun calculate-intersection (line1 line2)
(if (= (plist-get line1 'a) (plist-get line2 'a))
(progn (error "The two lines don't have any intersection.") nil)
(progn
(let (int-x int-y)
(setq int-x (/ (- (plist-get line2 'b) (plist-get line1 'b))
(- (plist-get line1 'a) (plist-get line2 'a))))
(setq int-y (+ (* (plist-get line1 'a) int-x) (plist-get line1 'b)))
(list 'x int-x 'y int-y) ) ) ) )
(let ((p1 '(x 4.0 y 0.0)) (p2 '(x 6.0 y 10.0))
(p3 '(x 0.0 y 3.0)) (p4 '(x 10.0 y 7.0)))
(let ((line1 (line-prop p1 p2))
(line2 (line-prop p3 p4)))
(message "%s" (calculate-intersection line1 line2)) ) )
)
</syntaxhighlight>
{{out}}
<pre>
(x 5.0 y 5.0)
</pre>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
type Intersection
model
Point point
fun asText = <|when(me.point == null, "No intersection", me.point.asText())
end
type Point
model
real x, y
fun asText = <|"(" + me.x + "," + me.y + ")"
end
type Line
model
Point s,e
end
type Main
fun getIntersectionByLines = Intersection by Line n1, Line n2
real a1 = n1.e.y - n1.s.y
real b1 = n1.s.x - n1.e.x
real c1 = a1 * n1.s.x + b1 * n1.s.y
real a2 = n2.e.y - n2.s.y
real b2 = n2.s.x - n2.e.x
real c2 = a2 * n2.s.x + b2 * n2.s.y
real delta = a1 * b2 - a2 * b1
if delta == 0 do return Intersection() end
return Intersection(Point((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta))
end
Line n1 = Line(Point(4, 0), Point(6, 10))
Line n2 = Line(Point(0, 3), Point(10, 7))
writeLine(getIntersectionByLines(n1, n2))
n1 = Line(Point(0, 0), Point(1, 1))
n2 = Line(Point(1, 2), Point(4, 5))
writeLine(getIntersectionByLines(n1, n2))
</syntaxhighlight>
{{out}}
<pre>
(5.0,5.0)
No intersection
</pre>
=={{header|F_Sharp|F#}}==
<
(*
Find the point of intersection of 2 lines.
Line 796 ⟶ 1,190:
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>
{{out}}
<pre>
Line 805 ⟶ 1,199:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
math.matrices.laplace math.vectors prettyprint sequences ;
Line 824 ⟶ 1,218:
{ 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 .</
{{out}}
<pre>
Line 834 ⟶ 1,228:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 880 ⟶ 1,274:
write(*,*)x,y
end subroutine intersect
end program intersect_two_lines</
{{out}}
<pre> 5.00000000 5.00000000 </pre>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
#Define NaN 0 / 0 ' FreeBASIC returns -1.#IND
Line 929 ⟶ 1,323:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> 5 5
-1.#IND -1.#IND</pre>
=={{header|Frink}}==
<syntaxhighlight lang="frink">lineIntersection[x1, y1, x2, y2, x3, y3, x4, y4] :=
{
det = (x1 - x2)(y3 - y4) - (y1 - y2)(x3 - x4)
if det == 0
return undef
t1 = (x1 y2 - y1 x2)
t2 = (x3 y4 - y3 x4)
px = (t1 (x3 - x4) - t2 (x1 - x2)) / det
py = (t1 (y3 - y4) - t2 (y1 - y2)) / det
return [px, py]
}
println[lineIntersection[4, 0, 6, 10, 0, 3, 10, 7]]</syntaxhighlight>
{{out}}
<pre>
[5, 5]
</pre>
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
Line 981 ⟶ 1,396:
}
}
</syntaxhighlight>
{{Out}}
<pre>{5 5}</pre>
Line 987 ⟶ 1,402:
=={{header|Groovy}}==
{{trans|Java}}
<
private static class Point {
double x, y
Line 1,033 ⟶ 1,448:
println(findIntersection(l1, l2))
}
}</
{{out}}
<pre>(5.0, 5.0)
Line 1,039 ⟶ 1,454:
=={{header|Haskell}}==
<
type Point = (Float, Float)
Line 1,074 ⟶ 1,489:
case interSection of
Left x -> x
Right x -> show x</
{{Out}}
<pre>(5.0,5.0)</pre>
Line 1,081 ⟶ 1,496:
{{trans|C++}}
'''Solution:'''
<
findIntersection=: (det ,."1 [: |: -/"2) %&det -/"2</
'''Examples:'''
<
line2=: 0 3 ,: 10 7
line3=: 0 3 ,: 10 7.1
Line 1,100 ⟶ 1,515:
__ __
findIntersection line6 ,: line7
2.5 1.5</
=={{header|Java}}==
{{trans|Kotlin}}
<
private static class Point {
double x, y;
Line 1,150 ⟶ 1,565:
System.out.println(findIntersection(l1, l2));
}
}</
{{out}}
<pre>{5.000000, 5.000000}
Line 1,158 ⟶ 1,573:
{{Trans|Haskell}}
===ES6===
<
'use strict';
// INTERSECTION OF TWO LINES ----------------------------------------------
Line 1,231 ⟶ 1,646:
]);
return show(lrIntersection.Left || lrIntersection.Right);
})();</
{{Out}}
<pre>[5,5]</pre>
Line 1,240 ⟶ 1,655:
points P1 and P2. Array destructuring is used for simplicity.
<
def det(a;b;c;d): a*d - b*c ;
Line 1,261 ⟶ 1,676:
then error("lineIntersect: parallel lines")
else [.xnom/.denom, .ynom/.denom]
end ;</
Example:
<
{{out}}
<syntaxhighlight lang
=={{header|Julia}}==
Line 1,271 ⟶ 1,686:
{{trans|Kotlin}}
<
x::T
y::T
Line 1,301 ⟶ 1,716:
l1 = Line(Point{Float64}(0, 0), Point{Float64}(1, 1))
l2 = Line(Point{Float64}(1, 2), Point{Float64}(4, 5))
println(intersection(l1, l2))</
{{out}}
Line 1,308 ⟶ 1,723:
==== GeometryTypes library version ====
<
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>
=={{header|Kotlin}}==
{{trans|C#}}
<
class PointF(val x: Float, val y: Float) {
Line 1,346 ⟶ 1,761:
l2 = LineF(PointF(1f, 2f), PointF(4f, 5f))
println(findIntersection(l1, l2))
}</
{{out}}
Line 1,353 ⟶ 1,768:
{-Infinity, -Infinity}
</pre>
=={{header|Lambdatalk}}==
{{trans|Python}}
<syntaxhighlight lang="scheme">
{def line_intersect
{def line_intersect.sub
{lambda {:111 :121 :112 :122 :211 :221 :212 :222}
{let { {:x :111} {:y :121}
{:z {- {* {- :222 :221} {- :112 :111}}
{* {- :212 :211} {- :122 :121}} } }
{:a {- :112 :111}} {:b {- :122 :121}}
{:c {- :212 :211}} {:d {- :222 :221}}
{:e {- :121 :221}} {:f {- :111 :211}}
{:g {- :121 :211}} {:h {- :122 :121}}
} {if {> :z 0}
then {A.new ∞ ∞}
else {let { {:x :x} {:y :y} {:a :a} {:b :b}
{:t1 {/ {- {* :c :e} {* :d :f}} :z} }
{:t2 {/ {- {* :a :g} {* :h :f}} :z} }
} {if {and {>= :t1 0} {<= :t1 1} {>= :t2 0} {<= :t2 1}}
then {A.new {+ :x {* :t1 :a}} {+ :y {* :t1 :b}} }
else {A.new ∞ ∞}} }}}}}
{lambda {:1 :2}
{line_intersect.sub
{A.first {A.first :1}} {A.last {A.first :1}}
{A.first {A.last :1}} {A.last {A.last :1}}
{A.first {A.first :2}} {A.last {A.first :2}}
{A.first {A.last :2}} {A.last {A.last :2}} }}}
-> line_intersect
{line_intersect {A.new {A.new 4 0} {A.new 6 10}}
{A.new {A.new 0 3} {A.new 10 7}}}
-> [5,5]
{line_intersect {A.new {A.new 4 0} {A.new 6 10}}
{A.new {A.new 0 3} {A.new 10 7.1}}}
-> [5.010893246187364,5.05446623093682]
{line_intersect {A.new {A.new 1 -1} {A.new 4 4}}
{A.new {A.new 2 5} {A.new 3 -2}}}
-> [2.5,1.5]
{line_intersect {A.new {A.new 0 0} {A.new 0 0}}
{A.new {A.new 0 3} {A.new 10 7}}}
-> [∞,∞]
{line_intersect {A.new {A.new 0 0} {A.new 1 1}}
{A.new {A.new 1 2} {A.new 4 5}}}
-> [∞,∞]
</syntaxhighlight>
=={{header|Lua}}==
{{trans|C#}}
<
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,367 ⟶ 1,828:
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))</
{{out}}
<pre>5 5</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Lineintersection (lineAtuple, lineBtuple) {
class line {
Line 1,402 ⟶ 1,863:
}
Lineintersection (4,0,6,10), (0,3,10,7) ' print 5 5
</syntaxhighlight>
{{out}}
<pre>
Line 1,409 ⟶ 1,870:
=={{header|Maple}}==
<
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));</
{{Output|Out}}
<pre>[5, 5]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
InfiniteLine[{{4, 0}, {6, 10}}],
InfiniteLine[{{0, 3}, {10, 7}}]
]</
{{out}}
<pre>Point[{5, 5}]</pre>
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">
function cross=intersection(line1,line2)
a=polyfit(line1(:,1),line1(:,2),1);
Line 1,431 ⟶ 1,892:
cross=[a(1) -1; b(1) -1]\[-a(2);-b(2)];
end
</syntaxhighlight>
{{out}}
<pre>line1=[4 0; 6 10]; line2=[0 3; 10 7]; cross=intersection(line1,line2)
Line 1,441 ⟶ 1,902:
=={{header|Modula-2}}==
<
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,497 ⟶ 1,958:
ReadChar;
END LineIntersection.</
=={{header|Nim}}==
{{trans|Go}}
<
Line = tuple
slope: float
Line 1,526 ⟶ 1,987:
line1 = createLine((0.0, 0.0), (1.0, 1.0))
line2 = createLine((1.0, 2.0), (4.0, 5.0))
echo intersection(line1, line2)</
{{out}}
<pre>
Line 1,537 ⟶ 1,998:
If warning are enabled the second print will issue a warning since we are trying to print out an undef
<
sub intersect {
my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = @_;
Line 1,558 ⟶ 2,019:
($ix, $iy) = intersect(0, 0, 1, 1, 1, 2, 4, 5);
print "$ix $iy\n";
</syntaxhighlight>
=={{header|Phix}}==
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/intersect.htm here].
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">function</span> <span style="color: #000000;">abc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- yeilds {a,b,c}, corresponding to ax+by=c</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">e</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">intersect</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e1</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">delta</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">a2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">a2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c1</span>
<span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"parallel lines/do not intersect"</span>
<span style="color: #0000FF;">:{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">/</span><span style="color: #000000;">delta</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">intersect</span><span style="color: #0000FF;">({</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {5,5}</span>
<span style="color: #000000;">intersect</span><span style="color: #0000FF;">({</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7.1</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {5.010893246,5.054466231}</span>
<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;">0</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;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</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;">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>
<!--</syntaxhighlight>-->
=={{header|Processing}}==
<
// test lineIntersect() with visual and textual output
float lineA[] = {4, 0, 6, 10}; // try 4, 0, 6, 4
Line 1,622 ⟶ 2,088:
float y = Ay1 + uA * (Ay2 - Ay1);
return new PVector(x, y);
}</
==={{header|Processing Python mode}}===
<
def setup():
Line 1,654 ⟶ 2,120:
x = Ax1 + uA * (Ax2 - Ax1)
y = Ay1 + uA * (Ay2 - Ay1)
return x, y</
=={{header|Python}}==
Find the intersection without importing third-party libraries.
<
""" returns a (x, y) tuple or None if there is no intersection """
d = (By2 - By1) * (Ax2 - Ax1) - (Bx2 - Bx1) * (Ay2 - Ay1)
Line 1,677 ⟶ 2,143:
(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)</
{{Out}}
Line 1,686 ⟶ 2,152:
{{Works with|Python|3.7}}
<
from itertools import product
Line 1,801 ⟶ 2,267:
# MAIN ---
if __name__ == '__main__':
main()</
{{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.
<
if __name__ == "__main__":
line1 = LineString([(4, 0), (6, 10)])
line2 = LineString([(0, 3), (10, 7)])
print(line1.intersection(line2))</
{{out}}
<pre>POINT (5 5)</pre>
Line 1,817 ⟶ 2,283:
=={{header|Racket}}==
{{trans|C++}}
<
(define (det a b c d) (- (* a d) (* b c))) ; determinant
Line 1,835 ⟶ 2,301:
(module+ test (line-intersect 4 0 6 10
0 3 10 7))</
{{out}}
Line 1,846 ⟶ 2,312:
{{trans|zkl}}
<syntaxhighlight lang="raku"
Real $cx, Real $cy, Real $dx, Real $dy ) {
Line 1,871 ⟶ 2,337:
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 );</
{{out}}
<pre>Intersection point: (5 5)
Line 1,877 ⟶ 2,343:
Intersection point: Lines are parallel
</pre>
===Using a geometric algebra library===
See task [[geometric algebra]].
<syntaxhighlight lang=raku>use Clifford;
# We pick a projective basis,
# and we compute its pseudo-scalar and its square.
my ($i, $j, $k) = @e;
my $I = $i∧$j∧$k;
my $I2 = ($I**2).narrow;
# Homogeneous coordinates of point (X,Y) are (X,Y,1)
my $A = 4*$i + 0*$j + $k;
my $B = 6*$i + 10*$j + $k;
my $C = 0*$i + 3*$j + $k;
my $D = 10*$i + 7*$j + $k;
# We form lines by joining points
my $AB = $A∧$B;
my $CD = $C∧$D;
# The intersection is their meet, which we
# compute by using the De Morgan law
my $ab = $AB*$I/$I2;
my $cd = $CD*$I/$I2;
my $M = ($ab ∧ $cd)*$I/$I2;
# Affine coordinates are (X/Z, Y/Z)
say $M/($M·$k) X· $i, $j;</syntaxhighlight>
{{out}}
<pre>(5 5)</pre>
=={{header|REXX}}==
Line 1,882 ⟶ 2,383:
Naive implementation.
To be improved for parallel lines and degenerate lines such as y=5 or x=8.
<
Parse Value '(4.0,0.0)' With '(' xa ',' ya ')'
Parse Value '(6.0,10.0)' With '(' xb ',' yb ')'
Line 1,898 ⟶ 2,399:
Say 'yab='y
Say 'ycd='yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc))
Say 'Intersection: ('||x','y')'</
{{out}}
<pre>The two lines are:
Line 1,912 ⟶ 2,413:
<br>
Variables are named after the Austrian notation for a straight line: y=k*x+d
<
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 1,983 ⟶ 2,484:
If res='' Then /* not any special case */
res='Intersection is ('||x'/'y')' /* that's the result */
Return ' -->' res</
{{out}}
<pre>A=(4.0/0.0) B=(6.0/10.0) C=(0.0/3.0) D=(10.0/7.0)
Line 2,001 ⟶ 2,502:
=={{header|Ring}}==
<
# Project : Find the intersection of two lines
Line 2,021 ⟶ 2,522:
see "ycd=" + (yc-xc*((yd-yc)/(xd-xc))+x*((yd-yc)/(xd-xc))) + nl
see "intersection: " + x + "," + y + nl
</syntaxhighlight>
Output:
<pre>
Line 2,031 ⟶ 2,532:
ycd=5
intersection: 5,5
</pre>
=={{header|RPL}}==
{{works with|HP|48}}
« C→R ROT C→R ROT →V2
SWAP 1 4 ROLL 1 { 2 2 } →ARRY /
» '<span style="color:blue">→LINECOEFF</span>' STO
« <span style="color:blue">→LINECOEFF</span> ROT ROT <span style="color:blue">→LINECOEFF</span> OVER -
ARRY→ DROP NEG SWAP /
DUP2 * 1 GET ROT 2 GET + R→C
» '<span style="color:blue">INTERSECT</span>' STO
Latest versions of RPL have powerful equation handling and solving functions:
{{works with|HP|49}}
« DROITE UNROT DROITE OVER -
'X' SOLVE DUP EQ→ NIP
UNROT SUBST EQ→ NIP COLLECT R→C
» '<span style="color:blue">INTERSECT</span>' STO
(4,0) (6,10) (0,3) (10,7) <span style="color:blue">INTERSECT</span>
{{out}}
<pre>
1: (5,5)
</pre>
=={{header|Ruby}}==
<
class Line
Line 2,052 ⟶ 2,576:
def to_s
"y = #{@a}x #{@b.positive? ? '+' : '-'} #{@b.abs}"
end
Line 2,061 ⟶ 2,585:
puts "Line #{l1} intersects line #{l2} at #{l1.intersect(l2)}."
</syntaxhighlight>
{{out}}
<pre>Line y = 5.0x
=={{header|Rust}}==
<
struct Point {
x: f64,
Line 2,112 ⟶ 2,636:
let l2 = Line(Point::new(1.0, 2.0), Point::new(4.0, 5.0));
println!("{:?}", l1.intersect(l2));
}</
{{Out}}
<pre>
Line 2,120 ⟶ 2,644:
=={{header|Scala}}==
<
val (l1, l2) = (LineF(PointF(4, 0), PointF(6, 10)), LineF(PointF(0, 3), PointF(10, 7)))
Line 2,149 ⟶ 2,673:
println(findIntersection(l01, l02))
}</
{{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,156 ⟶ 2,680:
{{trans|Raku}}
<
func intersection(ax, ay, bx, by,
Line 2,179 ⟶ 2,703:
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))</
{{out}}
<pre>
Line 2,188 ⟶ 2,712:
=={{header|Swift}}==
<
var x: Double
var y: Double
Line 2,223 ⟶ 2,747:
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)!)")</
{{out}}
<pre>Intersection at : Point(x: 5.0, y: 5.0)</pre>
Line 2,231 ⟶ 2,755:
{{trans|Rexx}}
Simple version:
<
([A](2,2)-[A](1,2))/([A](2,1)-[A](1,1))→B
[A](1,2)-[A](1,1)*B→A
Line 2,239 ⟶ 2,763:
A+X*B→Y
C+X*D→Z
Disp {X,Y}</
{{out}}
<pre>
Line 2,245 ⟶ 2,769:
</pre>
Full version:
<
{4,2}→Dim([B])
0→M
Line 2,298 ⟶ 2,822:
End
End
Disp {X,Y,M}</
{{out}}
<pre>
Line 2,309 ⟶ 2,833:
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<
Public Type Point
Line 2,374 ⟶ 2,898:
Debug.Assert ip.invalid
End Sub</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,403 ⟶ 2,927:
End Sub
End Module</
{{out}}
<pre>{X=5, Y=5}
Line 2,410 ⟶ 2,934:
=={{header|Wren}}==
{{trans|Kotlin}}
<
construct new(x, y) {
_x = x
Line 2,451 ⟶ 2,975:
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))</
{{out}}
Line 2,457 ⟶ 2,981:
(5, 5)
(-infinity, -infinity)
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func real Det; real A0, B0, A1, B1;
return A0*B1 - A1*B0;
func Cramer; real A0, B0, C0, A1, B1, C1;
real Denom;
[Denom:= Det(A0, B0, A1, B1);
RlOut(0, Det(C0, B0, C1, B1) / Denom);
RlOut(0, Det(A0, C0, A1, C1) / Denom);
];
real L0, L1, M0, M1;
[L0:= [[ 4., 0.], [ 6., 10.]];
L1:= [[ 0., 3.], [10., 7.]];
M0:= (L0(1,1) - L0(0,1)) / (L0(1,0) - L0(0,0));
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));
]</syntaxhighlight>
{{out}}
<pre>
5.00000 5.00000
</pre>
=={{header|zkl}}==
{{trans|C++}}
<
detAB,detCD := det(ax,ay, bx,by), det(cx,cy, dx,dy);
abDx,cdDx := ax - bx, cx - dx; // delta x
Line 2,473 ⟶ 3,021:
return(xnom/denom, ynom/denom);
}
fcn det(a,b,c,d){ a*d - b*c } // determinant</
<
{{out}}
<pre>
|