Find the intersection of two lines: Difference between revisions

Added Algol W
No edit summary
(Added Algol W)
 
(8 intermediate revisions by 6 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 569 ⟶ 611:
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
end</syntaxhighlight>
ycd = 3 + x * 0.4000
x = 5
yab = 5
ycd = 5
intersection: (5, 5)</pre>
 
==={{header|Run BASIC}}===
Line 1,069 ⟶ 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 1,103 ⟶ 1,176:
<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>
 
Line 2,208 ⟶ 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,460 ⟶ 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>
 
Line 2,839 ⟶ 3,001:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class Point {
construct new(x, y) {
_x = x
3,049

edits