mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Create a Manhattan Distance Calculation Formula
This is the first attempt of the Manhattan Distance Formula, feel free to update it.
This commit is contained in:
parent
3d2cfb99b7
commit
9f8b5ddd56
18
src/algorithms/math/Manhattan Distance
Normal file
18
src/algorithms/math/Manhattan Distance
Normal file
@ -0,0 +1,18 @@
|
||||
//This function is calculate the manhattan distance
|
||||
@param x
|
||||
@param y
|
||||
@return {SumOhManhattanDistance}
|
||||
|
||||
export default function manhattanDistance(x, y){
|
||||
|
||||
let SumOfManhattanDistance = 0;
|
||||
|
||||
for (let i = 0; i < x.length; i++){
|
||||
for (lei j= 0; j < x[i].length ; j++){
|
||||
//Compute the result of the Manhattan Distance
|
||||
SumOfManhattanDistance += (Math.abs(x[i] -x[j])+Math.abs(y[i] - y[j]));
|
||||
}
|
||||
}
|
||||
//Send the result back to the main function
|
||||
return SumOfManhattanDistance;
|
||||
}
|
Loading…
Reference in New Issue
Block a user