书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
目录
3.6 振荡振幅和周期
1、简谐运动
物体按正弦曲线周期性振荡的运动。
简谐运动可以表示为位置(在这里,只需要x坐标)和时间的函数,它有以下两个参
数。
- 振幅 离开运动中心的最大距离。
- 周期 完成一次往复运动所花费的时间。
- 频率:单位时间的周期数。它等于1除以周期,
从正弦曲线中可以看出,曲线的振幅是1,周期是2π(TWO_PI);
正弦函数的结果从来不会大于1,也不会小于-1;
每隔2π弧度(或者360度)波形就会重复。
2、示例
示例代码3-5 简谐运动
void setup() {
size(640,360);
}
void draw() {
background(255);
float period = 120;
float amplitude = 300;
// Calculating horizontal position according to formula for simple harmonic motion
float x = amplitude * sin(TWO_PI * frameCount / period);
stroke(0);
strokeWeight(2);
fill(0,227,0);
translate(width/2,height/2);
line(0,0,x,0);
ellipse(x,0,48,48);
}
![](https://img.haomeiwen.com/i17748967/bd69e114a51f2b39.gif)
网友评论