Created
November 19, 2015 14:28
-
-
Save Nullpo/5ff450b9d154c32b5817 to your computer and use it in GitHub Desktop.
Distancia entre puntos en 3d!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var array = [{ | |
| x: 1, | |
| y: 1, | |
| z: 1 | |
| },{ | |
| x: 50, | |
| y: 1, | |
| z: 1 | |
| },{ | |
| x: 1, | |
| y: 1, | |
| z: 1 | |
| },{ | |
| x: 9, | |
| y: 1, | |
| z: 1 | |
| },{ | |
| x: 5, | |
| y: 1, | |
| z: 1 | |
| }]; | |
| function getMenorDistancia(puntos, puntoPivot){ | |
| var minPunto = null; // {x: 10, y 20, z 30}; | |
| var minDistancia = null; // 10px | |
| var minPuntoPos = null; | |
| var tmp; | |
| for(var j = 0; j < puntos.length; j++){ | |
| tmp = calcularDistancia(puntoPivot, puntos[j]); | |
| if( minDistancia === null || minDistancia > tmp ){ | |
| minPunto = puntos[j]; | |
| minDistancia = tmp; | |
| minPuntoPos = j; | |
| } | |
| } | |
| return minPuntoPos; | |
| } | |
| function obtenerCamino(arr){ | |
| var puntosOriginal = arr.slice(); | |
| var ultimoElemento = puntosOriginal.splice(0,1)[0]; | |
| var nuevoArray = [ultimoElemento]; | |
| var posicionPuntoCercano; | |
| while(puntosOriginal.length != 0){ | |
| posicionPuntoCercano = getMenorDistancia(puntosOriginal, ultimoElemento) | |
| ultimoElemento = puntosOriginal.splice(posicionPuntoCercano,1)[0] | |
| nuevoArray.push(ultimoElemento); | |
| } | |
| return nuevoArray | |
| } | |
| function calcularDistancia(obj1, obj2){ | |
| var deltaX = obj1.x - obj2.x; | |
| var deltaY = obj1.y - obj2.y; | |
| var deltaZ = obj1.z - obj2.z; | |
| return Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ); | |
| } | |
| obtenerCamino(array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment