Processing 中使用函数来定义图形
来源:网络收集 点击: 时间:2024-03-30Processing 中使用 map 映射函数
输入代码:
void setup() {
size(240, 120);
strokeWeight(12);
smooth();
}
void draw() {
background(204);
stroke(255);
line(120, 60, mouseX, mouseY); // 白色线条
stroke(0);
float mx = map(mouseX, 0, width, 60, 180);
line(120, 60, mx, mouseY); // 黑色线条
}
运行函数,效果是通过鼠标的晃动可以使两个线条跟随着鼠标而动

直线跟随鼠标移动
输入代码:
void setup() {
size(240, 120);
strokeWeight(12);
smooth();
}
void draw() {
background(204);
stroke(255);
line(120, 60, mouseX, mouseY);
stroke(0);
float mx = mouseX/2 + 60;
line(120, 60, mx, mouseY);
}

Processing 中识别鼠标的点击动作
输入代码:
void setup() {
size(240, 120);
smooth();
strokeWeight(30);
}
void draw() {
background(204);
stroke(102);
line(40, 0, 70, height);
if (mousePressed == true) {
stroke(0);
}
line(0, 70, width, 50);
}
当单击鼠标左键时,线条会变成黑色


线条通过鼠标的指示运动
输入代码:
float x;
int offset = 10;
void setup() {
size(240, 120);
smooth();
x = width/2;
}
void draw() {
background(204);
if (mouseX x) {
x += 0.5;
offset = -10;
}
if (mouseX x) {
x -= 0.5;
offset = 10;
}
line(x, 0, x, height);
line(mouseX, mouseY, mouseX + offset, mouseY - 10);
line(mouseX, mouseY, mouseX + offset, mouseY + 10);
line(mouseX, mouseY, mouseX + offset*3, mouseY);
}

通过鼠标位置改变图形
输入代码:
int x = 120;
int y = 60;
int radius = 12;
void setup() {
size(240, 120);
smooth();
ellipseMode(RADIUS);
}
void draw() {
background(204);
float d = dist(mouseX, mouseY, x, y);
if (d radius) {
radius++;
fill(0);
} else {
fill(255);
}
ellipse(x, y, radius, radius);
}

鼠标放置在图形的位置,图形变为黑色
输入代码:
int x = 80;
int y = 30;
int w = 80;
int h = 60;
void setup() {
size(240, 120);
}
void draw() {
background(204);
if ((mouseX x) (mouseX x+w)
(mouseY y) (mouseY y+h)) {
fill(0);
} else {
fill(255);
}
rect(x, y, w, h);
}


代码回顾1:使用 if 语句

经验中还有更多关于 Processing 编程的内容,感兴趣可以查看
函数PROCESSINGPROCESSING编程ARDUINO版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_385377.html