Processing自定义形状绘制
1、绘制一个箭头形状
输入代码:
size(480, 120);
beginShape(); //开始形状的绘制
vertex(180, 82); //设置形状内点的坐标
vertex(207, 36);
vertex(214, 63);
vertex(407, 11);
vertex(412, 30);
vertex(219, 82);
vertex(226, 109);
endShape(); //结束形状的绘制
2、在步骤一中,我们成功的绘制了箭头,但是可以看到这并不是一个封闭的图形,我们需要将图形封闭,输入代码:
size(480, 120);
beginShape();
vertex(180, 82);
vertex(207, 36);
vertex(214, 63);
vertex(407, 11);
vertex(412, 30);
vertex(219, 82);
vertex(226, 109);
endShape(CLOSE); //结束形状,并封闭
3、使用简单命令,绘制出动物
输入代码:
size(480, 120);
smooth();
beginShape();
vertex(50, 120);
vertex(100, 90);
vertex(110, 60);
vertex(80, 20);
vertex(210, 60);
vertex(160, 80);
vertex(200, 90);
vertex(140, 100);
vertex(130, 120);
endShape();
fill(0);
ellipse(155, 60, 8, 8);
fill(255);
beginShape();
vertex(370, 120);
vertex(360, 90);
vertex(290, 80);
vertex(340, 70);
vertex(280, 50);
vertex(420, 10);
vertex(390, 50);
vertex(410, 90);
vertex(460, 120);
endShape();
fill(0);
ellipse(345, 50, 10, 10);
4、绘制一个更复杂的图形
输入代码:
size(480, 480);
smooth();
strokeWeight(2);
background(204);
ellipseMode(RADIUS);
stroke(102);
line(266, 257, 266, 162);
line(276, 257, 276, 162);
line(286, 257, 286, 162);
line(276, 155, 246, 112);
line(276, 155, 306, 56);
line(276, 155, 342, 170);
// Body
noStroke();
fill(102);
ellipse(264, 377, 33, 33);
fill(0);
rect(219, 257, 90, 120);
fill(102);
rect(219, 274, 90, 6);
fill(0);
ellipse(276, 155, 45, 45);
fill(255);
ellipse(288, 150, 14, 14);
fill(0);
ellipse(288, 150, 3, 3);
fill(153);
ellipse(263, 148, 5, 5);
ellipse(296, 130, 4, 4);
ellipse(305, 162, 3, 3);
5、使用变量来绘制相同的图形
输入代码:
size(480, 120);
smooth();
int y = 60;
int d = 80;
ellipse(75, y, d, d);
ellipse(175, y, d, d);
ellipse(275, y, d, d);
这种方法的优点是在我们需要修改图形尺寸的时候,只需修改变量的值,不需要逐个修改每个语句内的参数。
6、根据窗口尺寸来绘制图形
输入代码:
size(480, 120);
smooth();
line(0, 0, width, height); // 范围 (0,0) — (480, 120)
line(width, 0, 0, height); // 范围 (480, 0) — (0, 120)
ellipse(width/2, height/2, 60, 60);
7、在代码中加入基本数学运算来定义图形
输入代码:
size(480, 120);
int x = 25;
int h = 20;
int y = 25;
rect(x, y, 300, h);
x = x + 100;
rect(x, y + h, 300, h);
x = x - 250;
rect(x, y + h*2, 300, h);
8、使用 for 循环语句来绘制相同的图形
输入代码:
size(480, 120);
smooth();
strokeWeight(8);
for (int i = 20; i < 400; i += 60) {
line(i, 40, i + 60, 80);
}