Find the intersection of two lines: Difference between revisions

Content added Content deleted
Line 521: Line 521:
{{out}}
{{out}}
<pre>result 1,5,5</pre>
<pre>result 1,5,5</pre>


=={{header|Clojure}}==
<lang Clojure>;; Point is [x y] tuple
(defn compute-line [pt1 pt2]
(let [[x1 y1] pt1
[x2 y2] pt2
m (/ (- y2 y1) (- x2 x1))]
{:slope m
:offset (- y1 (* m x1))}))

(defn intercept [line1 line2]
(let [x (/ (- (:offset line1) (:offset line2))
(- (:slope line2) (:slope line1)))]
{:x x
:y (+ (* (:slope line1) x)
(:offset line1))}))</lang>

{{out}}
<pre>
(def line1 (compute-line [4 0] [6 10]))
(def line2 (compute-line [0 3] [10 7]))
line1 ; {:slope 5, :offset -20}
line2 ; {:slope 2/5, :offset 3}

(intercept line1 line2) ; {:x 5, :y 5}
</pre>


=={{header|D}}==
=={{header|D}}==