
Post by David Drake on Mar 2, 2020 7:34:29 GMT 5
I created the code below for the Rosetta Code task "Find the intersection of two lines" (found here: rosettacode.org/wiki/Find_the_intersection_of_two_lines). The task states: Finding the intersection of two lines that are in the same plane is an important topic in collision detection. Find the point of intersection of two lines in 2D. The 1st line passes though (4,0) and (6,10) . The 2nd line passes though (0,3) and (10,7) . 'Find the intersection of lines 'y1=mx1+b1 'y2=mx2+b2 'calulate b1,b2 'calculate m1,m2
on error goto [errorHandler] 'parallel lines
x11=4:y11=0 'line 1, point 1 x12=6:y12=10 'line 1, point 2 x21=0:y21=3 'line 2, point 1 x22=10:y22=7 'line 2, point 2
m1=(y12y11)/(x12x11) ' calculate Line 1 slope m2=(y22y21)/(x22x21) ' calculate Line 2 slope b1=y11m1*x11 ' calculate Line 1 intercept b2=y21m2*x21 'calculate Line 2 intercept
print "Find intersecting coordinate for these lines: "; print print "Line 1: y = ";m1;"x + ";b1 print "Line 2: y = ";m2;"x + ";b2
'Note error handler will catch if parallel slopes lead to division by zero
xint = (b2b1)/(m1m2) ' calculate value of intersecting x coordinate yint = m1*xint+b1 ' calculate value of intersecting y coordinate print print "Intersecting coordinate is ";xint;", ";yint print
end
[errorHandler] print "An error has occurred." if Err = 11 then print "Division by zero. Likely the lines are parallel." end
Looking forward to seeing your corrections and suggestions.



Post by tsh73 on Mar 2, 2020 12:37:30 GMT 5
As a sub, along c# example
'Find the intersection of lines 'along c# example data 4,0, 6,10, 0,3, 10, 7 data 0,0, 1,1, 1,2, 4,5
for i = 1 to 2 read s1x, s1y, e1x, e1y 'first line start, end read s2x, s2y, e2x, e2y 'second line print "First line is (";s1x;","; s1y;")("; e1x;","; e1y;")" print "second line is (";s2x;","; s2y;")("; e2x;","; e2y;")"
call findIntersection s1x, s1y, e1x, e1y, s2x, s2y, e2x, e2y, x, y, isParallel if isParallel then print "is parallel" else print "intersects at (";x;","; y;")" end if
print next
end
sub findIntersection s1x, s1y, e1x, e1y, s2x, s2y, e2x, e2y, byRef x, byRef y, byRef isParallel a1 = e1y  s1y b1 = s1x  e1x c1 = a1 * s1x + b1 * s1y
a2 = e2y  s2y b2 = s2x  e2x c2 = a2 * s2x + b2 * s2y
delta = a1 * b2  a2 * b1 'if lines are parallel if delta =0 then isParallel=1: exit sub x = (b2 * c1  b1 * c2) / delta y = (a1 * c2  a2 * c1) / delta end sub
results
First line is (4,0)(6,10) second line is (0,3)(10,7) intersects at (5,5)
First line is (0,0)(1,1) second line is (1,2)(4,5) is parallel



Post by tsh73 on Mar 2, 2020 13:06:05 GMT 5
Also  it is not required but really could gеt handy  how to tell if intersection point actually on segment(s) provided
'Find the intersection of lines 'along c# example data 4,0, 6,10, 0,3, 10,7 data 4,0, 6,10, 0,3, 4,5 data 4,0, 5,4, 0,3, 10,7 data 4,0, 5,4, 0,3, 4,5 data 0,0, 1,1, 1,2, 4,5
for i = 1 to 5 read s1x, s1y, e1x, e1y 'first line start, end read s2x, s2y, e2x, e2y 'second line print "First line is (";s1x;","; s1y;")("; e1x;","; e1y;")" print "second line is (";s2x;","; s2y;")("; e2x;","; e2y;")"
call findIntersection s1x, s1y, e1x, e1y, s2x, s2y, e2x, e2y, x, y, isParallel if isParallel then print "is parallel" else print "intersects at (";x;","; y;")" if (xs1x)*(xe1x)<0 and (ys1y)*(ye1y)<0 then print "on the 1st segment" if (xs2x)*(xe2x)<0 and (ys2y)*(ye2y)<0 then print "on the 2nd segment" end if
print next
end
sub findIntersection s1x, s1y, e1x, e1y, s2x, s2y, e2x, e2y, byRef x, byRef y, byRef isParallel a1 = e1y  s1y b1 = s1x  e1x c1 = a1 * s1x + b1 * s1y
a2 = e2y  s2y b2 = s2x  e2x c2 = a2 * s2x + b2 * s2y
delta = a1 * b2  a2 * b1 'if lines are parallel if delta =0 then isParallel=1: exit sub x = (b2 * c1  b1 * c2) / delta y = (a1 * c2  a2 * c1) / delta end sub
result
First line is (4,0)(6,10) second line is (0,3)(10,7) intersects at (5,5) on the 1st segment on the 2nd segment
First line is (4,0)(6,10) second line is (0,3)(4,5) intersects at (5.11111111,5.55555556) on the 1st segment
First line is (4,0)(5,4) second line is (0,3)(10,7) intersects at (5.27777778,5.11111111) on the 2nd segment
First line is (4,0)(5,4) second line is (0,3)(4,5) intersects at (5.42857143,5.71428571)
First line is (0,0)(1,1) second line is (1,2)(4,5) is parallel



Post by David Drake on Mar 3, 2020 13:42:50 GMT 5
Okay, team, which version should be posted to Rosetta Code? I like all three (the first is, I think easier to understand, the second and third have more features).

