How to resolve the algorithm Find the intersection of two lines step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the intersection of two lines step by step in the Common Lisp programming language

Table of Contents

Problem Statement

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) .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the Common Lisp programming language

Source code in the common programming language

;; Point is [x y] tuple
(defun point-of-intersection (x1 y1 x2 y2 x3 y3 x4 y4)
"Find the point of intersection of the lines defined by the points (x1 y1) (x2 y2) and (x3 y3) (x4 y4)"
  (let* ((dx1 (- x2 x1))
         (dx2 (- x4 x3))
         (dy1 (- y2 y1))
         (dy2 (- y4 y3))
         (den (- (* dy1 dx2) (* dy2 dx1))) )
    (unless (zerop den) 
  	(list (/ (+ (* (- y3 y1) dx1 dx2) (* x1 dy1 dx2) (* -1 x3 dy2 dx1)) den)	  	
       	      (/ (+ (* (+ x3 x1) dy1 dy2) (* -1 y1 dx1 dy2) (* y3 dx2 dy1)) den) ))))


  

You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the Scala programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the C++ programming language
You may also check:How to resolve the algorithm Input loop step by step in the TypeScript programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the J programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the ActionScript programming language