Find the intersection of two lines: Difference between revisions

Added Algol W
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Added Algol W)
 
(5 intermediate revisions by 3 users not shown)
Line 383:
5.0000 5.0000
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % find the intersection of two lines %
 
record Point ( real x, y );
record Line ( real m, c ); % y = mx + c %
 
% returns the line that passes through p1 and p2 %
reference(Line) procedure findLine ( reference(Point) value p1, p2 ) ;
if x(p1) = x(p2) then begin % the line is verticval %
Line( 0, x(p1) )
end
else begin % the line is not vertical %
real m1;
m1 := ( y(p1) - y(p2) ) / ( x(p1) - x(p2) );
Line( m1, y(p1) - ( m1 * x(p1) ) )
end findLine ;
 
% returns the intersection of two lines %
% - the lines must be distinct and not parallel %
reference(Point) procedure intersection ( reference(Line) value l1, l2 ) ;
begin
real x;
x := ( c(l2) - c(l1) ) / ( m(l1) - m(l2) );
Point( x, ( m(l1) * x ) + c(l1) )
end intersection ;
 
begin % find the intersection of the lines as per the task %
reference(Point) i;
i := intersection( findLine( Point( 4.0, 0.0 ), Point( 6.0, 10.0 ) )
, findLine( Point( 0.0, 3.0 ), Point( 10.0, 7.0 ) )
);
write( r_format := "A", r_w := 8, r_d := 4, x(i), y(i) )
end
end.
</syntaxhighlight>
{{out}}
<pre>
5.0000 5.0000
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
Line 1,074 ⟶ 1,116:
</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}}==
Line 2,254 ⟶ 2,322:
{{out}}
<pre>POINT (5 5)</pre>
 
{{libheader|Pygame}}
Find the intersection by importing the external [https://www.pygame.org/docs/ PyGame] library.
<syntaxhighlight lang="python">import pygame as pg
 
def segment_intersection(a, b, c, d):
""" returns a pygame.Vector2 or None if there is no intersection """
ab, cd, ac = a - b, c - d, a - c
if not (denom:= ab.x * cd.y - ab.y * cd.x):
return
 
t = (ac.x * cd.y - ac.y * cd.x) / denom
u = -(ab.x * ac.y - ab.y * ac.x) / denom
if 0 <= t <= 1 and 0 <= u <= 1:
return a.lerp(b, t)
 
 
if __name__ == '__main__':
a,b = pg.Vector2(4,0), pg.Vector2(6,10) # try (4, 0), (6, 4)
c,d = pg.Vector2(0,3), pg.Vector2(10,7) # for non intersecting test
pt = segment_intersection(a, b, c, d)
print(pt)</syntaxhighlight>
 
{{Out}}
<pre>[5,5]</pre>
 
=={{header|Racket}}==
Line 2,506 ⟶ 2,599:
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>
 
3,049

edits