diff --git a/src/js/index.js b/src/js/index.js index e2ea35e..cb210b5 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -260,26 +260,29 @@ function calculateDistance(lat1, lon1, lat2, lon2) { return d; } +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} + function shuffle(array, fix_start_point = true, fix_end_point = true) { - let currentIndex = array.length; + let currentIndex = array.length - 1; if (fix_end_point) { - currentIndex = array.length - 1; + currentIndex = array.length - 2; } let start_index = 0; if (fix_start_point) { start_index = 1; } - // While there remain elements to shuffle... - while (currentIndex != start_index) { - - // Pick a remaining element... - let randomIndex = Math.floor(Math.random() * currentIndex); - currentIndex--; - + while (currentIndex !== start_index) { + let right = currentIndex; + let left = getRandomInt(start_index, currentIndex); // And swap it with the current element. - [array[currentIndex], array[randomIndex]] = [ - array[randomIndex], array[currentIndex] + [array[left], array[right]] = [ + array[right], array[left] ]; + currentIndex--; } } @@ -351,6 +354,7 @@ function recalculateWaypoints(waypoints) { // Три точки, старт, стоп и промежуточная выводим без расчета showTravelPoints([waypoints[0].id, waypoints[1].id, waypoints[2].id]); } else { + // применяем лучшие поиск из 200.000 лучших вариантов (9! = 362880, берем такого порядка число чтобы не грузить браузер расчетами) let path_indexes = stochasticPathFind(waypoints, true, true); showTravelPoints(path_indexes); @@ -360,7 +364,6 @@ function recalculateWaypoints(waypoints) { $(() => { waypoints.loadData(); - initMap(); initRegionDropDown(); initRecalculateWaypointsButton();