[英]sine wave drawing using JavaScript
I am working on creating a simple snow simulation, but I am failing horribly on the math. 我正在创建一个简单的积雪模拟,但是数学却很糟糕。 Basically, when a snowflake is initialized, it's x position is set randomly somewhere on the svg canvas.
基本上,当雪花被初始化时,它的x位置是在svg画布上随机设置的。 Then from there, it falls straight down.
然后从那里直接落下。 It should be simulating a sine wave (the x coordinates should move like a sine wave).
它应该模拟一个正弦波(x坐标应该像一个正弦波一样移动)。
I haven't done trig since last year so i'm a bit rusty. 自去年以来我还没有做过挖掘,所以我有点生锈。
Math.sin()
takes radians I believe, so I multiply by 180, then divide by pi to convert to degrees? 我相信
Math.sin()
的弧度是多少,所以我乘以180,然后除以pi以转换为度?
Assume x is the current x position of a snowflake, and width is the width of the canvas. 假设x是雪花的当前x位置,而width是画布的宽度。
x += (Math.sin(((x/width)*(180/Math.PI)))*width)
It kind of works, but it is all over the place. 这有点用,但是到处都是。 I have a vague idea of what i'm doing, but I can't seem to push the math from my brain to code.
我对自己的工作有一个模糊的想法,但是我似乎无法将数学从脑子里推到代码上。
Here's a JSFiddle: 这是一个JSFiddle:
What am I doing incorrectly? 我做错了什么?
It should be: 它应该是:
x += A*Math.sin(F*(y/Height)*2*Math.PI);
where A
is the amplitude, ie how many pixels you want the flake to shift left and right (note that total shift will be 2*A). 其中
A
是振幅,即您希望薄片左右移动多少像素(请注意,总移位将为2 * A)。 F is the frequency, or how often you want the flake to shift left and right (I'd set it at random between 2 and 10 for each flake). F是频率,或您希望薄片左右移动的频率(我将每个薄片随机设置为2到10之间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.