processing中5个三角函数运用的案例
时间: 2024-03-21 20:42:49 浏览: 130
在processing中,常用的五个三角函数是sin(), cos(), tan(), asin()和acos()。以下是一些这些函数的应用案例:
1. 绘制正弦波
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
translate(200, 200);
stroke(0);
noFill();
beginShape();
for (int i = 0; i < 360; i++) {
float y = sin(radians(i)) * 100;
vertex(i, y);
}
endShape();
angle += 5;
}
```
2. 绘制螺旋线
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
translate(200, 200);
stroke(0);
noFill();
beginShape();
for (int i = 0; i < 360; i++) {
float r = i;
float x = r * cos(radians(angle));
float y = r * sin(radians(angle));
vertex(x, y);
}
endShape();
angle += 5;
}
```
3. 旋转图形
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
translate(200, 200);
rotate(radians(angle));
rectMode(CENTER);
rect(0, 0, 100, 100);
angle += 5;
}
```
4. 创建动态效果
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
translate(200, 200);
rotate(radians(angle));
scale(sin(radians(angle)) + 2);
rectMode(CENTER);
rect(0, 0, 100, 100);
angle += 5;
}
```
5. 生成随机数
```
void setup() {
size(400, 400);
}
void draw() {
background(255);
stroke(0);
for (int i = 0; i < 100; i++) {
float x = random(width);
float y = random(height);
point(x, y);
}
}
```
阅读全文