Processing 中使用函数来定义图形

 时间:2026-02-14 12:09: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 中使用函数来定义图形

  • PS窗口排列四联在哪怎么设
  • 如何用ps怎么把图片变成圆形
  • Illustrator中怎样给路径增加锚点?
  • C4D 怎么渲染一个透明的图片
  • PS如何制作爆炸蘑菇云效果
  • 热门搜索
    红烧肉的做法大全 去厦门旅游攻略 镜泊湖旅游攻略 健身减肥操 齐心简介 个人宣言怎么写 武汉欢乐谷攻略 快穿金牌攻略 空之轨迹fc攻略 鬼泣5攻略