给定 X 计算 Sin Y(带偏移量)
我有一个函数(从这里)计算给定 X 时的坐标 Y
function calculateSinYGivenX(time, frequency = 1, amplitude = 1, phase = 0, offset = 0) {
return Math.sin((time * frequency * (Math.PI * 2)) + (phase * (Math.PI * 2))) * amplitude + offset;
}
到目前为止,我的测试按预期
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(0);
expect(calculateSinYGivenX(time = 0.25, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(1);
expect(calculateSinYGivenX(time = 0.5, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toBeCloseTo(0);
expect(calculateSinYGivenX(time = 0.75, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(-1);
expect(calculateSinYGivenX(time = 1, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toBeCloseTo(0);
通过说明:
当我尝试将波向右移动时(phase = 0.5
),他们失败了
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 0.5, offset = 0)).toEqual(-1);
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 1, offset = 0)).toEqual(0);
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 1.5, offset = 0)).toEqual(1);
Expected -1.2246467991473532e-16 to equal -1
Expected 2.4492935982947064e-16 to equal 0
Expected -3.6739403974420594e-16 to equal 1
说明:
不幸的是,我的数学不够好,无法找出我哪里出错了。我已经使用在线资源来达到这一点,但我似乎无法让它按预期工作。
I have a function (from here) that calculates the coordinate Y when given X
function calculateSinYGivenX(time, frequency = 1, amplitude = 1, phase = 0, offset = 0) {
return Math.sin((time * frequency * (Math.PI * 2)) + (phase * (Math.PI * 2))) * amplitude + offset;
}
So far my tests pass as expected
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(0);
expect(calculateSinYGivenX(time = 0.25, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(1);
expect(calculateSinYGivenX(time = 0.5, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toBeCloseTo(0);
expect(calculateSinYGivenX(time = 0.75, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toEqual(-1);
expect(calculateSinYGivenX(time = 1, frequency = 1, amplitude = 1, phase = 0, offset = 0)).toBeCloseTo(0);
To illustrate:
When I try to shift the wave to the right (phase = 0.5
), they fail
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 0.5, offset = 0)).toEqual(-1);
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 1, offset = 0)).toEqual(0);
expect(calculateSinYGivenX(time = 0, frequency = 1, amplitude = 1, phase = 1.5, offset = 0)).toEqual(1);
Expected -1.2246467991473532e-16 to equal -1
Expected 2.4492935982947064e-16 to equal 0
Expected -3.6739403974420594e-16 to equal 1
To illustrate:
I'm unfortunately not good enough at maths to figure out where I'm going wrong. I've used online sources to get to this point, but I can't seem to get it to work as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过阅读和测试一些内容后,阶段只是开始时间,因此可以将其理解为“时间偏移”。
因此,
时间 + 相位
的组合将始终给出相同的结果(对于固定频率、幅度和偏移)例如:
因此,相位变量的情况按预期工作。
注意JS中的浮点变量句柄,你使用了toBeCloseTo(x)函数,所以我建议你使用它来进行比较。
After reading and testing few things, phase is just the starting time, so it can be understood as 'time offset'.
As a result, the composition of
time + phase
will always give the same result (for fixed frequency, amplitude and offset)For example :
So things are working as expected for the phase variable.
Be careful of floating point variable handle in JS, you used the
toBeCloseTo(x)
function, so I suggest you to use it for your comparisons.