振荡振幅和周期

2022-04-30  本文已影响0人  大龙10

书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
目录

3.6 振荡振幅和周期

1、简谐运动

物体按正弦曲线周期性振荡的运动。
简谐运动可以表示为位置(在这里,只需要x坐标)和时间的函数,它有以下两个参
数。

从正弦曲线中可以看出,曲线的振幅是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);
}
上一篇 下一篇

猜你喜欢

热点阅读