沿着sin曲线均匀的空间圆圈 [英] Evenly space circles along sin curve
问题描述
我试图沿着正弦曲线均匀地定位精灵(视觉上是圆形的,例如半径50)。
I am trying to position sprites (visually circlular with, say radius 50) evenly along a sin curve.
目前,我用于x和y的函数是:
Currently, the function I am using for x and y are:
for(int i=0; i<number_of_sprites; i++){
x = sprite_index*60
y = sin(sprite_index)*60
sprite.position = CGPointMake(x, y)
}
即使精灵沿x轴均匀分布,你也不是彼此等距的。
Even though the sprites are all evenly spaced along the x axis, you are not all equidistant from each other.
这是有点伪代码 - 我只是在寻找数学函数来做这件事。
This is somewhat pseudocode - I'm simply looking for the mathematical function to do this.
如何解决这个问题?
推荐答案
根据这个答案,沿着正弦波计算弧长的解决方案是椭圆积分很难计算分析地说。
According to this answer on math.stackexchange.com, the solution for calculating the arc length along a sine wave is an elliptic integral which is difficult to calculate analytically.
幸运的是,你有一台计算机可以使用数值积分应用相同的公式来实现所需曲线的近似 。
Fortunately you have a computer at your disposal that can apply the same formula using numerical integration to achieve an approximation of the desired curve.
鉴于衍生物sin(x)
是 cos(x)
,如果你非常缓慢地增加 x
变量(每步增量为 dx
)然后在每一步,你的 ds
将是 dx * sqrt(1 + cos(x)^ 2)
。继续累积 ds
值,直到达到(或超过)你想要的间距,然后画一个点。
Given the derivative of sin(x)
is cos(x)
, if you increment your x
variable very slowly (with a per-step increment of dx
) then at each step your ds
will be dx * sqrt(1 + cos(x)^2)
. Keep accumulating the ds
values until it reaches (or exceeds) your desired spacing and only then draw a point.
有关演示,请参见 http://jsfiddle.net/alnitak/fp7aknoc/ 。
这篇关于沿着sin曲线均匀的空间圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!