如何计算多个航点之间的航点?

时间:2019-06-24 12:50:23

标签: javascript arrays vector

例如,我有一个包含3个航路点的数组: [ [ 526, 1573, 24 ], [ 2224, 809, -1546 ], [ 6869, 96, -3074 ] ]

我也知道我想休息一下,直到到达第一个和最后一个航点之间的n次。所以最后我想要一个n点的数组。

如何在JS中找到这些n个静止点?

谢谢!

编辑:请注意,这不是单个对象!想象每个轴是一个人。他们必须在相同的时间停止相同的时间,但不必明显地在同一地点。

2 个答案:

答案 0 :(得分:1)

您要使用线性插值。

一个简单的例子:

const POINTS = [ [ 526, 1573, 24 ], [ 2224, 809, -1546 ], [ 6869, 96, -3074 ] ];
const N = 10;

function getDistance(point1, point2) {
    // speed in 3d space is mutated according only to the X distance,
    // to keep speed constant in X dimension
    return Math.abs(point1[0] - point2[0]);
}

function go(points, n) {
    const pointDistances = points.slice(1).map((point, index) => getDistance(points[index], point));

    const fullDistance = pointDistances.reduce((sum, distance) => sum + distance, 0);

    const distancePerSection = fullDistance / n;

    return points.slice(1)
        .reduce((last, point, index) => {
            const thisDistance = pointDistances[index];

            const numRestPoints = Math.max(0, Math.floor(thisDistance / distancePerSection) - 1);

            if (!numRestPoints) {
                return last.concat([point]);
            }

            const thisYVector = point[1] - points[index][1];
            const thisZVector = point[2] - points[index][2];

            return last.concat(new Array(numRestPoints).fill(0)
                .reduce((section, item, restIndex) => {
                    return section.concat([[
                        points[index][0] + (restIndex + 1) * distancePerSection,
                        points[index][1] + (restIndex + 1) * thisYVector * distancePerSection / thisDistance,
                        points[index][2] + (restIndex + 1) * thisZVector * distancePerSection / thisDistance
                    ]]);
                }, [])
                .concat([point])
            );

        }, points.slice(0, 1));
}

function test() {
    const result = go(POINTS, N);

    if (result.length !== N) {
        throw new Error('Must be N length');
    }

    if (!result[0].every((value, index) => value === POINTS[0][index])) {
        throw new Error('Doesn\'t start at the first point');
    }
    if (!result[N - 1].every((value, index) => value === POINTS[POINTS.length - 1][index])) {
        throw new Error('Doesn\'t end at the last point');
    }

    if (!POINTS.slice(1, N - 1).every(point =>
        result.some(resultPoint => resultPoint.every((value, index) => value === point[index]))
    )) {
        throw new Error('Doesn\'t go through every provided point');
    }

    console.log(result.slice(1).map((point, index) => getDistance(point, result[index])));

    console.log('The result passed the tests!');
    console.log(JSON.stringify(result, null, 2));
}

test();

我基本上要遍历点列表,并确定它们之间是否应存在任何休息点,如果有的话,请插入它们。

如果您想进一步澄清,请发表评论!

答案 1 :(得分:0)

我现在也通过线性插值解决了这个问题:

我的解决方案:

var waypoints = [[526,1573,24],[2224,809,-1546],[6869,96,-3074]];

var pauses = 20;

generateWaypopints();

function generateWaypopints(){
    var newWaypoints = [];
    var progressAtMainPoints = 1 / (waypoints.length - 1)
    var pausesBetweenWaypoints = pauses * progressAtMainPoints;
    var progressAtPauses = 1 / pausesBetweenWaypoints;

    newWaypoints.push(waypoints[0]);

    var sector = 0;
    var pausesInSector = 0;

    for(var i = 0; i < pauses; i++){
        var progress = progressAtPauses * (pausesInSector + 1)
        var x = Math.round(waypoints[sector][0] + (waypoints[sector + 1][0] - waypoints[sector][0]) * progress);
        var y = Math.round(waypoints[sector][1] + (waypoints[sector + 1][1] - waypoints[sector][1]) * progress);
        var z = Math.round(waypoints[sector][2] + (waypoints[sector + 1][2] - waypoints[sector][2]) * progress);

        if(progress >= 1){
            sector++;
            pausesInSector = 0;
        }else
            pausesInSector++;

        newWaypoints.push([x,y,z]);
    }
    console.log(newWaypoints);
    return newWaypoints;
}