How to resolve the algorithm Find the intersection of two lines step by step in the JavaScript programming language
How to resolve the algorithm Find the intersection of two lines step by step in the JavaScript 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 JavaScript programming language
The code snippet you provided is a function that finds the intersection point of two lines in the plane.
The intersection
function takes two arguments, each of which is a list of two points representing a line. The function returns an Either
value, which can either be a Left
value containing a string error message or a Right
value containing the intersection point as a tuple of two floats.
The function first calculates the delta of the x and y coordinates of the two lines, and then uses the determinant of the two deltas to determine if the lines are parallel. If the determinant is zero, the lines are parallel and there is no intersection point, so the function returns a Left
value with the error message "(Parallel lines – no intersection)".
If the determinant is not zero, the lines are not parallel and there is an intersection point. The function then calculates the x and y coordinates of the intersection point using the delta values and the determinants of the two lines. The function returns a Right
value with the intersection point as a tuple of two floats.
The rest of the code is a collection of generic functions that are used by the intersection
function. The Left
and Right
functions create Either
values, the apList
function applies a list of functions to a list of arguments, the fst
and snd
functions return the first and second elements of a tuple, respectively, and the map
function applies a function to each element of a list.
The show
function is used to convert a value to a string.
The last part of the code is a test that calls the intersection
function with two sets of points and prints the result.
Source code in the javascript programming language
(() => {
'use strict';
// INTERSECTION OF TWO LINES ----------------------------------------------
// intersection :: Line -> Line -> Either String (Float, Float)
const intersection = (ab, pq) => {
const
delta = f => x => f(fst(x)) - f(snd(x)),
[abDX, pqDX, abDY, pqDY] = apList(
[delta(fst), delta(snd)], [ab, pq]
),
determinant = abDX * pqDY - abDY * pqDX;
return determinant !== 0 ? Right((() => {
const [abD, pqD] = map(
([a, b]) => fst(a) * snd(b) - fst(b) * snd(a),
[ab, pq]
);
return apList(
[([pq, ab]) =>
(abD * pq - ab * pqD) / determinant
], [
[pqDX, abDX],
[pqDY, abDY]
]
);
})()) : Left('(Parallel lines – no intersection)');
};
// GENERIC FUNCTIONS ------------------------------------------------------
// Left :: a -> Either a b
const Left = x => ({
type: 'Either',
Left: x
});
// Right :: b -> Either a b
const Right = x => ({
type: 'Either',
Right: x
});
// A list of functions applied to a list of arguments
// <*> :: [(a -> b)] -> [a] -> [b]
const apList = (fs, xs) => //
[].concat.apply([], fs.map(f => //
[].concat.apply([], xs.map(x => [f(x)]))));
// fst :: (a, b) -> a
const fst = tpl => tpl[0];
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
// show :: a -> String
const show = x => JSON.stringify(x); //, null, 2);
// TEST --------------------------------------------------
// lrIntersection ::Either String Point
const lrIntersection = intersection([
[4.0, 0.0],
[6.0, 10.0]
], [
[0.0, 3.0],
[10.0, 7.0]
]);
return show(lrIntersection.Left || lrIntersection.Right);
})();
You may also check:How to resolve the algorithm Day of the week step by step in the Arc programming language
You may also check:How to resolve the algorithm 24 game step by step in the D programming language
You may also check:How to resolve the algorithm RSA code step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Möbius function step by step in the F# programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Liberty BASIC programming language