十个processing中运用三角函数的案例
时间: 2024-03-01 16:52:32 浏览: 87
三角函数图例程序
5星 · 资源好评率100%
以下是十个运用三角函数的Processing案例:
1. 用三角函数绘制正弦曲线
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
stroke(0);
noFill();
beginShape();
for (float x = 0; x < width; x += 5) {
float y = sin(angle) * 100 + height/2;
vertex(x, y);
angle += 0.1;
}
endShape();
}
```
2. 用三角函数实现平滑移动的圆
```
float angle = 0;
float radius = 100;
void setup() {
size(400, 400);
}
void draw() {
background(255);
float x = cos(angle) * radius + width/2;
float y = sin(angle) * radius + height/2;
ellipse(x, y, 50, 50);
angle += 0.1;
}
```
3. 用三角函数制作星形图案
```
float angle = 0;
float radius = 100;
void setup() {
size(400, 400);
}
void draw() {
background(255);
beginShape();
for (int i = 0; i < 5; i++) {
float x = cos(angle + i*TWO_PI/5) * radius + width/2;
float y = sin(angle + i*TWO_PI/5) * radius + height/2;
vertex(x, y);
}
endShape(CLOSE);
angle += 0.1;
}
```
4. 用三角函数绘制螺旋线
```
float angle = 0;
float radius = 10;
void setup() {
size(400, 400);
}
void draw() {
background(255);
float x = cos(angle) * radius + width/2;
float y = sin(angle) * radius + height/2;
ellipse(x, y, 5, 5);
angle += 0.05;
radius += 0.1;
}
```
5. 用三角函数绘制椭圆轨迹
```
float angle = 0;
float a = 100;
float b = 50;
void setup() {
size(400, 400);
}
void draw() {
background(255);
float x = cos(angle) * a + width/2;
float y = sin(angle) * b + height/2;
ellipse(x, y, 10, 10);
angle += 0.05;
}
```
6. 用三角函数制作旋转的矩形
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
rectMode(CENTER);
translate(width/2, height/2);
rotate(angle);
rect(0, 0, 100, 50);
angle += 0.1;
}
```
7. 用三角函数绘制正多边形
```
float angle = 0;
float radius = 100;
int sides = 6;
void setup() {
size(400, 400);
}
void draw() {
background(255);
beginShape();
for (int i = 0; i < sides; i++) {
float x = cos(angle + i*TWO_PI/sides) * radius + width/2;
float y = sin(angle + i*TWO_PI/sides) * radius + height/2;
vertex(x, y);
}
endShape(CLOSE);
angle += 0.1;
}
```
8. 用三角函数绘制波浪线
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
beginShape();
for (float x = 0; x < width; x += 5) {
float y = sin(angle + x/10) * 50 + height/2;
vertex(x, y);
}
endShape();
angle += 0.1;
}
```
9. 用三角函数绘制迷宫
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
stroke(0);
for (int x = 0; x < width; x += 20) {
for (int y = 0; y < height; y += 20) {
if (sin(angle + x/10 + y/10) > 0) {
line(x, y, x+20, y+20);
} else {
line(x+20, y, x, y+20);
}
}
}
angle += 0.1;
}
```
10. 用三角函数制作旋转的渐变
```
float angle = 0;
void setup() {
size(400, 400);
}
void draw() {
background(255);
for (int x = 0; x < width; x += 10) {
for (int y = 0; y < height; y += 10) {
float c = map(sin(angle + x/10 + y/10), -1, 1, 0, 255);
stroke(c);
point(x, y);
}
}
angle += 0.1;
}
```
阅读全文