Processing 如何在重复循环模式中使用自定义形状?

Processing 如何在重复循环模式中使用自定义形状?,processing,Processing,我已经制作了一个自定义形状(myFunction),并且还使用更简单的形状制作了图案。我想知道如何用我的自定义形状替换这些简单形状,同时维护在处理时绘制的图案…您已经在调用函数,例如noFill(),noStroke(),等等 函数也是如此:只需使用它的名称和()来调用它(因为它没有参数):myFunction() 假设您想在模式1中绘制它,您可以执行以下操作: if (pattern==1) { for (int x=50; x<width; x+=100) {

我已经制作了一个自定义形状(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();
      }
    }
  }
当然,你可以随意尝试,让它看起来更好

以下是草图的修改版本,它使用几次按键在运行时更改图案类型和形状类型:

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), random(255));
  ellipse(x, y+30, 50, 20); //plus ellipse cluster
  ellipse(x, y+30, 20, 50);
}

void blobs(){
  noStroke(); 
  fill(random(250), random(120), random(100));
  ellipse(random(width), random(height), 20, 50); 
  noFill();
  stroke(random(255));
  ellipse(random(width), random(height), 50, 20);
}

void myFunction() {
  noFill(); 
  stroke(255);
  ellipse(300, 300, 200, 400); 
  ellipse(300, 300, 400, 200); 
  translate(300, 300); 
  rotate(radians(130)); 
  ellipse(0, 0, 200, 400); 
  translate(0, 0); 
  rotate(radians(0)); 
  ellipse(0, 0, 400, 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;
  }
}
int模式=1;
//0=pluseEllipseCluser,1=水滴,2=myFunction螺旋描记器圆
int-shape=0;
无效设置(){
大小(600600);
println(“按1,2改变图案”);
println(“按p/b/c更改形状”);
}
作废提款(){
背景(30);
仰泳();
如果(模式==1){
对于(int x=50;x=0;y-=random(2.5)){
绘制所选形状(x、y);
}
}
}
}
空心drawSelectedShapes(浮动x、浮动y){
如果(形状==0){
plusEllipseCluser(x,y);
}
如果(形状==1){
斑点();
}
如果(形状==2){
myFunction();
}
}
void plusEllipseCluser(浮点x,浮点y){
填充(随机(255)、随机(255)、随机(255)、随机(255)、随机(255));
椭圆(x,y+30,50,20);//加上椭圆簇
椭圆(x,y+30,20,50);
}
void blobs(){
仰泳();
填充(随机(250)、随机(120)、随机(100));
椭圆(随机(宽度),随机(高度),20,50);
noFill();
中风(随机(255));
椭圆(随机(宽度),随机(高度),50,20);
}
void myFunction(){
noFill();
中风(255);
椭圆(300300200400);
椭圆(300300400200);
翻译(300300);
旋转(弧度(130));
椭圆(0,0200400);
翻译(0,0);
旋转(弧度(0));
椭圆(0,0,400,200);
}
按下void键(){
如果(键=='1'){
模式=1;
}
如果(键=='2'){
模式=2;
}
如果(键=='p'){
形状=0;
}
如果(键=='b'){
形状=1;
}
如果(键=='c'){
形状=2;
}
}
请注意,上面的示例还调用了传递两个参数的
plusEllipseCluser()
:这是定义和调用具有两个参数的函数的基本示例。当然,您以前已经调用过带参数的函数(例如,
random(min,max)
椭圆(x,y,w,h)
,等等)


享受形状和图案的乐趣。

添加足够的代码,以便我们能够理解您的问题,您将获得更好的答案。此外,您可能希望将形状存储到
PShape
顶点缓冲区中,并在循环中平移和绘制该缓冲区。这样,您只需计算一次形状,而不是每次绘制形状时都进行计算。
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), random(255));
  ellipse(x, y+30, 50, 20); //plus ellipse cluster
  ellipse(x, y+30, 20, 50);
}

void blobs(){
  noStroke(); 
  fill(random(250), random(120), random(100));
  ellipse(random(width), random(height), 20, 50); 
  noFill();
  stroke(random(255));
  ellipse(random(width), random(height), 50, 20);
}

void myFunction() {
  noFill(); 
  stroke(255);
  ellipse(300, 300, 200, 400); 
  ellipse(300, 300, 400, 200); 
  translate(300, 300); 
  rotate(radians(130)); 
  ellipse(0, 0, 200, 400); 
  translate(0, 0); 
  rotate(radians(0)); 
  ellipse(0, 0, 400, 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;
  }
}