processing中10个运用三角函数的动画案例代码
时间: 2024-03-21 07:43:06 浏览: 16
以下是10个运用三角函数的动画案例的代码:
1. 环形动画
```
float angle = 0;
float radius = 100;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + cos(angle) * radius;
float y = height/2 + sin(angle) * radius;
ellipse(x, y, 20, 20);
angle += 0.05;
}
```
2. 波浪动画
```
float angle = 0;
float amplitude = 100;
float frequency = 0.02;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float y = height/2 + sin(angle) * amplitude;
for (int x = 0; x < width; x += 10) {
ellipse(x, y, 10, 10);
}
angle += frequency;
}
```
3. 旋转动画
```
float angle = 0;
float radius = 100;
void setup() {
size(500, 500);
}
void draw() {
background(255);
translate(width/2, height/2);
rotate(angle);
ellipse(radius, 0, 20, 20);
angle += 0.05;
}
```
4. 弹跳动画
```
float angle = 0;
float amplitude = 100;
float frequency = 0.02;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float y = height/2 + abs(sin(angle) * amplitude);
ellipse(width/2, y, 20, 20);
angle += frequency;
}
```
5. 变形动画
```
float angle = 0;
float amplitude = 100;
float frequency = 0.02;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + cos(angle) * amplitude * sin(angle);
float y = height/2 + sin(angle) * amplitude;
ellipse(x, y, 20, 20);
angle += frequency;
}
```
6. 螺旋动画
```
float angle = 0;
float radius = 10;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + cos(angle) * radius;
float y = height/2 + sin(angle) * radius;
ellipse(x, y, 20, 20);
angle += 0.05;
radius += 0.1;
}
```
7. 螺线动画
```
float angle = 0;
float a = 2;
float b = 1;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + cos(angle) * a * angle;
float y = height/2 + sin(angle) * b * angle;
ellipse(x, y, 20, 20);
angle += 0.05;
}
```
8. 花瓣动画
```
float angle = 0;
float radius = 100;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + cos(angle) * radius * sin(angle);
float y = height/2 + sin(angle) * radius * sin(angle);
ellipse(x, y, 20, 20);
angle += 0.05;
}
```
9. 颤动动画
```
float angle = 0;
float amplitude = 100;
float frequency = 0.02;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float y = height/2 + sin(angle) * amplitude * sin(angle);
ellipse(width/2, y, 20, 20);
angle += frequency;
}
```
10. 摆动动画
```
float angle = 0;
float length = 100;
float gravity = 0.1;
void setup() {
size(500, 500);
}
void draw() {
background(255);
float x = width/2 + sin(angle) * length;
float y = height/2 + cos(angle) * length;
ellipse(x, y, 20, 20);
angle += gravity;
}
```
以上代码都可以在processing中直接运行,并且可以根据需要进行调整和扩展。