processing中三个运用sin函数的动画案例
时间: 2023-11-24 22:06:03 浏览: 139
sin函数的显示
以下是三个运用sin函数的动画案例,基于Processing编写:
1. 弹跳球
```java
float x, y;
float speed = 0.05;
float amplitude = 100;
void setup() {
size(400, 400);
x = width/2;
y = height/2;
}
void draw() {
background(255);
x += 2;
y = height/2 + sin(frameCount * speed) * amplitude;
ellipse(x, y, 50, 50);
if (x > width) {
x = 0;
}
}
```
2. 径向波浪
```java
float angle = 0;
float amplitude = 100;
float frequency = 10;
void setup() {
size(400, 400);
stroke(0, 50);
fill(255, 50);
}
void draw() {
background(255);
translate(width/2, height/2);
beginShape();
for (int i = 0; i <= 360; i += 10) {
float x = sin(radians(i + angle)) * amplitude * sin(radians(i * frequency));
float y = cos(radians(i + angle)) * amplitude * sin(radians(i * frequency));
vertex(x, y);
}
endShape(CLOSE);
angle += 2;
}
```
3. 摆动线条
```java
float angle = 0;
float amplitude = 50;
float frequency = 5;
void setup() {
size(400, 400);
stroke(0);
}
void draw() {
background(255);
translate(width/2, height/2);
rotate(radians(angle));
line(-amplitude, 0, amplitude, 0);
angle += sin(radians(frameCount * frequency)) * 10;
}
```
阅读全文