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

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Find the intersection of two lines step by step in the jq 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 jq programming language

Source code in the jq programming language

# determinant of 2x2 matrix
def det(a;b;c;d): a*d - b*c ;

# Input: an array representing a line (L1)
# Output: the intersection of L1 and L2 unless the lines are judged to be parallel
# This implementation uses "destructuring" to assign local variables
def lineIntersection(L2):
  .    as [[$ax,$ay], [$bx,$by]]
  | L2 as [[$cx,$cy], [$dx,$dy]]
  | {detAB: det($ax;$ay; $bx;$by),
     detCD: det($cx;$cy; $dx;$dy),
     abDx: ($ax - $bx),
     cdDx: ($cx - $dx),
     abDy: ($ay - $by),
     cdDy: ($cy - $dy)}
  | . + {xnom:  det(.detAB;.abDx;.detCD;.cdDx),
         ynom:  det(.detAB;.abDy;.detCD;.cdDy),
         denom: det(.abDx; .abDy;.cdDx; .cdDy) }
  | if (.denom|length < 10e-6)  # length/0 emits the absolute value
    then error("lineIntersect: parallel lines")
    else [.xnom/.denom, .ynom/.denom]
    end ;

[[4.0, 0.0], [6.0,10.0]] | lineIntersection([[0.0, 3.0], [10.0, 7.0]])

[5,5]

  

You may also check:How to resolve the algorithm Ackermann function step by step in the Dc programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Pascal programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the Ada programming language
You may also check:How to resolve the algorithm Rate counter step by step in the BASIC256 programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the Maxima programming language