Interpolate coordinates in Mercator space for better accuracy

This commit is contained in:
Mattéo Delabre 2020-07-23 19:19:35 +02:00
parent d688bff813
commit f080bb808e
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
3 changed files with 1509 additions and 44 deletions

1508
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@turf/turf": "^5.1.6",
"axios": "^0.19.2",
"color": "^3.1.2",
"csv-parse": "^4.8.3",

View File

@ -1,4 +1,5 @@
const axios = require('axios');
const turf = require('@turf/turf');
const network = require('./network.json');
const server = 'http://localhost:4321';
@ -189,38 +190,46 @@ const updatePositions = (courses, time) =>
const previousNode = segment.points[nextNodeIndex - 1];
const nextNode = segment.points[nextNodeIndex];
const previousPoint = turf.toMercator([
previousNode.lon,
previousNode.lat
]);
const nextPoint = turf.toMercator([
nextNode.lon,
nextNode.lat
]);
const curLength = course.traveledDistance
- previousNode.distance;
const totalLength = nextNode.distance
- previousNode.distance;
const progression = curLength / totalLength;
const t = curLength / totalLength;
course.position = {
lat: progression * nextNode.lat
+ (1 - progression) * previousNode.lat,
lon: progression * nextNode.lon
+ (1 - progression) * previousNode.lon,
};
course.position = [
t * nextPoint[0] + (1 - t) * previousPoint[0],
t * nextPoint[1] + (1 - t) * previousPoint[1],
];
}
}
else
{
const currentStop = network.stops[course.currentStop];
course.position = {
lat: currentStop.lat,
lon: currentStop.lon,
};
const currentNode = network.stops[course.currentStop];
course.position = turf.toMercator([
currentNode.lon,
currentNode.lat
]);
}
}
};
const run = callback =>
const start = () =>
{
const courses = {};
let lastFrame = null;
let lastUpdate = null;
const loop = () =>
const update = () =>
{
const now = Date.now();
@ -233,12 +242,9 @@ const run = callback =>
const time = lastFrame === null ? 0 : now - lastFrame;
lastFrame = now;
updatePositions(courses, time);
callback(courses);
};
const interval = setInterval(loop, 24);
return () => clearInterval(interval);
return {courses, update};
};
exports.run = run;
exports.start = start;