问题描述
我已经制作了一个自定义形状 (myFunction),而且我还使用更简单的形状制作了图案。我想知道如何用我的自定义形状替换那些简单的形状,同时保持处理时绘制的图案...
解决方法
您已经在调用 noFill()
、noStroke()
等函数。
您的函数也是如此:只需使用它的名称和 () 即可调用它(因为它没有参数):myFunction();
假设您想在模式 1 中绘制它,您可以执行以下操作:
if (pattern==1) {
for (int x=50; x<width; x+=100) {
for (int y=20; y<height; y+=100) {
myFunction();
}
}
}
不过,您需要注意渲染。
运行上面的代码不会显示您在 noFill()
中的 myFunction()
中调用的任何内容,也不会在 noStroke()
中的 draw()
中显示,紧跟在 background()
之后:您将无法看到一个没有填充也没有描边的形状:)
一个建议是添加笔画:
void myFunction() {
noFill();
stroke(255);
ellipse(300,300,200,400);
ellipse(300,400,200);
translate(300,300);
rotate(radians(130));
ellipse(0,400);
translate(0,0);
rotate(radians(0));
ellipse(0,200);
}
当然可以随意尝试,让它看起来更漂亮。
这是您的草图的修改版本,它使用几次按键在运行时更改图案类型和形状类型:
int pattern = 1;
// 0 = pluseEllipseCluser,1 = blobs,2= myFunction spirograph circles
int shape = 0;
void setup() {
size(600,600);
println("press 1,2 to change pattern");
println("press p/b/c to change shapes");
}
void draw() {
background(30);
noStroke();
if (pattern==1) {
for (int x=50; x<width; x+=100) {
for (int y=20; y<height; y+=100) {
drawSelectedShapes(x,y);
}
}
}
if (pattern==2) {
float rando = random(10,90);
for (float x= rando; x >= 0; x-=random(2.5)) {
for (float y= rando; y >= 0; y-=random(2.5)) {
drawSelectedShapes(x,y);
}
}
}
}
void drawSelectedShapes(float x,float y){
if(shape == 0){
plusEllipseCluser(x,y);
}
if(shape == 1){
blobs();
}
if(shape == 2){
myFunction();
}
}
void plusEllipseCluser(float x,float y){
fill(random(255),random(255),random(255));
ellipse(x,y+30,50,20); //plus ellipse cluster
ellipse(x,20,50);
}
void blobs(){
noStroke();
fill(random(250),random(120),random(100));
ellipse(random(width),random(height),50);
noFill();
stroke(random(255));
ellipse(random(width),20);
}
void myFunction() {
noFill();
stroke(255);
ellipse(300,200);
}
void keyPressed(){
if(key == '1') {
pattern = 1;
}
if(key == '2') {
pattern = 2;
}
if(key == 'p'){
shape = 0;
}
if(key == 'b'){
shape = 1;
}
if(key == 'c'){
shape = 2;
}
}
请注意,上面的示例还调用 plusEllipseCluser()
传递两个参数:这是定义和调用带有两个参数的函数的基本示例。当然,您之前已经调用过带参数的函数(例如 random(min,max)
、ellipse(x,y,w,h)
等)
享受形状和图案的乐趣。