Find the intersection of two lines: Difference between revisions

Content added Content deleted
m (Removed useless "$" when using "echo".)
Line 241: Line 241:
<pre>
<pre>
5.0000 5.0000
5.0000 5.0000
</pre>
=={{header|APL}}==
<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
⍝ System for line 1 will be:
⍝ 0 = 4a + b
⍝ 10 = 6a + b
⍝ The two arguments to be passed to the dyadic domino are:
⍝ The (0, 10) vector as the left argument
⍝ The ( 4 1 ) matrix as the right argument.
⍝ ( 6 1 )
⍝ The following solver will take ⍵ the matrix of coordinates, one point per row, then massage the argument to extract x,y
⍝ and inject 1, where needed
⍝ Applied twice, we now have a, b and a', b' defining the two lines, we need to resolve it in x and y
⍝ y = ax + b
⍝ y = a'x + b'
⍝ In order to reuse the same solver, we need to format it a little bit, and change the sign of a and a', and multiply (a,b) by (-1, 1):
⍝ b = -ax + y
⍝ b' = -a'x + y
A ← 4 0
B ← 6 10
C ← 0 3
D ← 10 7
solver ← {(,2 ¯1↑⍵)⌹(2 1↑⍵),1}
I ← solver 2 2⍴((¯1 1)×solver 2 2⍴A,B),(¯1 1)×solver 2 2⍴C,D
</lang>
{{out}}
<pre>
I
5 5
</pre>
</pre>