JavaScript sine wave

时间秒杀一切 提交于 2019-12-05 21:36:53

The basic sine function is defined as:

f(x) = A sin(wt + p)

where

  • A is the amplitude
  • w is the frequency
  • p is the phase

These factors determine how the graph of f will look like.

The amplitude can be thought of as a scaling factor, the larger A, the larger (absolute values) the peaks and lows of f.

The frequency determines how fast the sine function will run through all its values until it starts over again - sine is a periodic function. The larger k, the faster f will run through one period.

p is the phase, think of it as "shifting" the starting point of the function to the right (positive p) or left (negative). Hard to explain in words, have a look here for graphs.

The function you give in your example is a generalized version of

f: R->R², f(t)=(sin(t), cos(t))

Which is (one of) the parametrizations of the unit circle . If you increase t monotonously and plot x (sin(t)) and y (cos(t)) you will have a point flying on a circle with radius 1.

Your generalized function is

f: R->R², f(t) = (A sin(1/wt), A cos(1/wt)), w > 1

In your case A=ampl, t=top and w=20 for the x coordinate and w=25 for the y coordinate. These slight deviations for w are there make the movement jittery, so that it's no longer a perfect circle, but rather some "distorted" ellipse - snow flakes don't fall in perfect circles, I guess. Additionally this makes the path of the flake appear to be more random than straight perfect circles. It's an illusion though, this is all also very deterministic and still periodic - it's just that x and y movement are "out of phase" so it takes a whole lot longer until one period is completed.

w is chosen > 1 to "slow down" the circular movement. The larger you choose w, the lower the frequency will be and your moving point will complete a full circle much slower.

The larger you choose A, the larger your circle will become.

It just makes the sine wave bigger so the curves can be more easily observed.

Here's a fiddle I tried making. If I change 20 and 25 to 1, the movement gets less interesting. http://jsfiddle.net/AbM9z/1/

It would help to know what values the function is being called with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!