Processing 中使用函数来定义图形

2025-09-26 12:13:12

1、Processing 中使用 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); // 黑色劣兼喝线条

}

运行函数,效果是通过鼠标的晃动可以使两个线条跟随着鼠标而动

Processing 中使用函数来定义图形

2、直线跟随鼠标移动

输入代码:

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 中使用函数来定义图形

3、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);

}

当单击鼠标左键时,线条会变成黑色

Processing 中使用函数来定义图形

Processing 中使用函数来定义图形

4、线条通过鼠标的指示运动

输入代码:

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);

}

Processing 中使用函数来定义图形

5、通过鼠标位置改变图形

输入代码:

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);

}

Processing 中使用函数来定义图形

6、鼠标放置在图形屈择的位置,图形变为黑色

输入代码:

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);

}

Processing 中使用函数来定义图形

Processing 中使用函数来定义图形

7、代码回顾1:使用 if 语露追句

Processing 中使用函数来定义图形

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢