使用processing编写一张曲线海报
时间: 2023-07-11 08:43:16 浏览: 71
好的,这里给出一个使用 Processing 编写的曲线海报代码示例,具体实现方式可能存在差异,请根据需要进行修改和调整。
```processing
void setup() {
size(800, 600);
smooth();
}
void draw() {
background(255);
// 定义函数,这里以 y=sin(x) + cos(x) 为例
float[] x = new float[1000];
float[] y = new float[1000];
for (int i = 0; i < x.length; i++) {
x[i] = map(i, 0, x.length - 1, 0, 10*TWO_PI);
y[i] = sin(x[i]) + cos(x[i]);
}
// 绘制图像
stroke(255, 0, 0);
strokeWeight(2);
noFill();
beginShape();
for (int i = 0; i < x.length; i++) {
float px = map(x[i], 0, 10*TWO_PI, 0, width);
float py = map(y[i], -2, 2, height, 0);
vertex(px, py);
}
endShape();
// 添加标题和标签
fill(0);
textAlign(CENTER, CENTER);
textSize(24);
text("曲线海报示例", width/2, 50);
textSize(16);
text("X轴", width/2, height - 20);
text("Y轴", 20, height/2);
// 调整坐标轴范围
noFill();
stroke(0, 100);
strokeWeight(1);
rect(0, 0, width - 1, height - 1);
// 添加背景颜色
noStroke();
fill(255, 0, 0, 50);
beginShape();
vertex(0, height);
for (int i = 0; i < x.length; i++) {
float px = map(x[i], 0, 10*TWO_PI, 0, width);
float py = map(y[i], -2, 2, height, 0);
vertex(px, py);
}
vertex(width, height);
endShape();
fill(0, 0, 255, 50);
beginShape();
vertex(0, 0);
for (int i = 0; i < x.length; i++) {
float px = map(x[i], 0, 10*TWO_PI, 0, width);
float py = map(y[i], -2, 2, height, 0);
vertex(px, py);
}
vertex(width, 0);
endShape();
// 保存图像
save("curve_poster.png");
}
```
这段代码会生成一张类似于下面这张海报的曲线图像:
![curve_poster.png](https://i.imgur.com/5LQJGhN.png)