lambdaway
::
line_intersection
5
|
list
|
login
|
load
|
|
_h1 line intersection _p Following [[rosettacode|https://rosettacode.org/wiki/Find_the_intersection_of_two_lines]] Find the point of intersection of two lines in 2D: _ul The 1st line passes though (4,0) and (6,10) . _ul The 2nd line passes though (0,3) and (10,7) . {pre '{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}} } } {:f {- :111 :211}} {:a {- :112 :111}} {:g {- :121 :211}} {:e {- :121 :221}} {:b {- :122 :121}} {:c {- :212 :211}} {:d {- :222 :221}} } {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} {* :b :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}} ;; :111 :121 {A.first {A.last :1}} {A.last {A.last :1}} ;; :112 :122 {A.first {A.first :2}} {A.last {A.first :2}} ;; :211 :221 {A.first {A.last :2}} {A.last {A.last :2}} }}} ;; :212 :222 -> {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} {* :b :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}} ;; :111 :121 {A.first {A.last :1}} {A.last {A.last :1}} ;; :112 :122 {A.first {A.first :2}} {A.last {A.first :2}} ;; :211 :221 {A.first {A.last :2}} {A.last {A.last :2}} }}} ;; :212 :222 '{line_intersect {A.new {A.new 4 0} {A.new 6 10}} {A.new {A.new 0 3} {A.new 10 7}}} -> {line_intersect {A.new {A.new 4 0} {A.new 6 10}} {A.new {A.new 0 3} {A.new 10 7}}} '{line_intersect {A.new {A.new 4 0} {A.new 6 10}} {A.new {A.new 0 3} {A.new 10 7.1}}} -> {line_intersect {A.new {A.new 4 0} {A.new 6 10}} {A.new {A.new 0 3} {A.new 10 7.1}}} '{line_intersect {A.new {A.new 1 -1} {A.new 4 4}} {A.new {A.new 2 5} {A.new 3 -2}}} -> {line_intersect {A.new {A.new 1 -1} {A.new 4 4}} {A.new {A.new 2 5} {A.new 3 -2}}} '{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 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}}} -> {line_intersect {A.new {A.new 0 0} {A.new 1 1}} {A.new {A.new 1 2} {A.new 4 5}}} }
lambdaway v.20211111