根据轴承计算 x 和 y

Calculate the x and y based on bearing

我有一个屏幕,我想根据角度计算下一个 x 和 y。 第一步是此示例从第 1 步开始。 我如何计算下一个脚步,我想向前增加 120,侧步需要穿入和穿出大约 60。

请记住,起点可以是 x = 100,y = 100,角度为 180,因此足迹必须沿 y 轴向上移动。

我尝试了以下 Javascript 但脚步声似乎变得混乱:

this.startingFootPrintX = Math.floor(Math.random() * 1000) + 20; //Random number between 20 and 1000
this.startingFootPrintY = Math.floor(Math.random() * 560) + 20; //Random number between 20 and 560
this.startingAngle = Math.floor(Math.random() * 340) + 20; //Random number between 20 and 340

startFootSteps();

startFootSteps(){
console.log(this.startingFootPrintX);
console.log(this.startingFootPrintY);

this.startingFootPrintX = Math.round(Math.cos(this.startingAngle * Math.PI / 180) * 120 + this.startingFootPrintX);
this.startingFootPrintY = Math.round(Math.sin(this.startingAngle * Math.PI / 180) * 60 + this.startingFootPrintY);

setInterval(function () {
startFootSteps();
}, 3000);
}

图表:

步进方向(黑线)由(cos θ, sin θ)给出。步长偏移方向(小蓝线)由 (sin θ, -cos θ)

给出

位置递归由下式给出:

s判断下一个脚印在黑线的哪一侧,即-1为左脚,+1为右脚。

如果您知道起始位置 c0 和起始脚 s0,closed-form 解决方案为:

每一步双脚交替。

在您的图表示例中,参数是 w = 60, d = 120, θ = 40°, c0 = (96, 438), s0 = -1(从左脚开始)。


更新:JavaScript 代码片段

this.startingPosX = Math.floor(Math.random() * 1000) + 20;
this.startingPosY = Math.floor(Math.random() * 560) + 20;
this.startingAngle = Math.floor(Math.random() * 340) + 20;
this.startingFoot = 1 - 2 * Math.round(Math.random());   // select which foot to start with
this.stepSize = 120;
this.stepWidth = 60;

footsteps(0);

footsteps(n) {
    // should actually pre-calculate these outside but oh well
    let a = this.startingAngle * Math.PI / 180;
    let c = Math.cos(a), s = Math.sin(a);

    // current foot
    let d = this.startingFoot * (1 - 2 * (n % 2));

    // apply equations
    this.footprintX = Math.round(   
        this.startingPosX +             // initial
        this.stepSize * n * c +         // step
        this.stepWidth * 0.5 * d * s    // foot offset
    );
    this.footprintY = Math.round(
        this.startingPosY +             // initial
        this.stepSize * n * s -         // step
        this.stepWidth * 0.5 * d * c    // foot offset
    );

    // draw the foot here
    console.log(this.footprintX);
    console.log(this.footprintY);

    // increment the step counter for the next call
    setInterval(function() { footsteps(n+1); }, 3000);
}