Processing 循环并重复一个模式

Processing 循环并重复一个模式,processing,Processing,我正在为我的处理类完成一个模式。到目前为止,这是我的代码 color[] lemonspears = { #f9f8ae, //#0 highlight lemon #fffc24, //#1 mid lemon #d6c000, //#2 dark lemon #242000, //#3 detailing #e7b416, //#4 highlight pear #a8da2b, /

我正在为我的处理类完成一个模式。到目前为止,这是我的代码

      color[] lemonspears =
      {
       #f9f8ae, //#0 highlight lemon
       #fffc24, //#1 mid lemon
       #d6c000, //#2 dark lemon
       #242000, //#3 detailing
       #e7b416, //#4 highlight pear 
       #a8da2b, //#5 mid pear
       #667a2a, //#6 dark pear
        #f2e6ba, //#7 light beige
        #8acdff, //#8 light blue
       #0551ff, //#9 dark blue

       };

       color palette[] = lemonspears;
       PShape Circle, Triangle;
       PShape Oval, Pear, Olives, Leaves;
       PShape Twig, Dots, Stem, Lines;

       PShape[] Fruit = new PShape[1];
       PShape[] Details = new PShape[1];

       void setup()
       {
       size(800, 800);
       background(palette[7]);
       }


       void draw() {
       scale(.3,.3);
       pushMatrix();
       //BACKGROUND
       //outer circles

       fill(palette[8]);
       stroke(palette[9]);
       strokeWeight(5);
       circle( 30, 40, 200);
       circle( 760, 40, 200);
       circle( 30, 760, 200);
       circle( 760, 760, 200);

       //mid triangles
       fill(palette[9]);
        stroke(palette[8]);
         triangle( 350, 0, 405, 80, 460, 0);
        stroke(palette[8]);
      triangle( 350, 800, 405, 720, 460, 800);

        //mid circle
          ellipseMode(CENTER);
        stroke(palette[1]);
        fill(palette[4]);
        circle( 405, 400, 200);

       // right dots
        strokeWeight(3);
       stroke(palette[1]);
        fill(palette[4]);
        circle(630, 330, 11);
        circle(720, 400, 11);
        circle(630, 470, 11);

        // left dots
        circle(170, 330, 11);
       circle(80, 400, 11);
       circle(170, 470, 11);
     popMatrix();
     }

我希望这张图片能像一块瓷砖一样横穿整个屏幕,我想知道最好的方法是使用循环。我试过translate(),但由于明显的原因,它不起作用。谢谢

您可以将磁贴另存为PImage(),然后将磁贴视为正方形,使用循环对其进行细分

下面的解决方案应该会产生您想要的输出

color[]调色板=
{
#f9f8ae,//#0高光柠檬
#fffc24,//#1中柠檬
#d6c000,//#2深柠檬色
#242000年,详细说明
#e7b416,//#4
#a8da2b,//#5中梨
#667a2a,/#6黑梨
#f2e6ba,//#7浅米色
#8acdff,/#8浅蓝色
#0551ff,深蓝色
};
布尔值drawTile=true;
无效设置(){
大小(800800);
背景(调色板[7]);
}
空心镶嵌线(浮动比例){
//将草图另存为PImage
PImage tile=get(0,0,宽度,高度);
//计算缩放瓷砖的高度和宽度
int tileWidth=(int)(宽度*比例);
int tileHeight=(int)(高度*刻度);
//调整瓷砖大小
平铺。调整大小(平铺宽度、平铺高度);
//在每个位置重新绘制瓷砖

for(int i=0;i使用
pushMatrix()
/
popMatrix()
隔离坐标空间以单独平移一组形状,这是正确的。如果在for循环中执行此操作,则相应地增加x,y位置,您应该能够重复渲染模式:

color[] lemonspears =
  {
  #f9f8ae, //#0 highlight lemon
  #fffc24, //#1 mid lemon
  #d6c000, //#2 dark lemon
  #242000, //#3 detailing
  #e7b416, //#4 highlight pear 
  #a8da2b, //#5 mid pear
  #667a2a, //#6 dark pear
  #f2e6ba, //#7 light beige
  #8acdff, //#8 light blue
  #0551ff, //#9 dark blue

};

color palette[] = lemonspears;

void setup()
{
  size(800, 800);
  
}

void draw() {
  background(palette[7]);
  scale(.3, .3);
  for(int i = 0 ; i < 3; i++){
    for(int j = 0 ; j < 3; j++){
      pushMatrix();
      translate(150 + i * mouseX,150 + j * mouseX);
      //BACKGROUND
      //outer circles
    
      fill(palette[8]);
      stroke(palette[9]);
      strokeWeight(5);
      circle( 30, 40, 200);
      circle( 760, 40, 200);
      circle( 30, 760, 200);
      circle( 760, 760, 200);
    
      //mid triangles
      fill(palette[9]);
      stroke(palette[8]);
      triangle( 350, 0, 405, 80, 460, 0);
      stroke(palette[8]);
      triangle( 350, 800, 405, 720, 460, 800);
    
      //mid circle
      ellipseMode(CENTER);
      stroke(palette[1]);
      fill(palette[4]);
      circle( 405, 400, 200);
    
      // right dots
      strokeWeight(3);
      stroke(palette[1]);
      fill(palette[4]);
      circle(630, 330, 11);
      circle(720, 400, 11);
      circle(630, 470, 11);
    
      // left dots
      circle(170, 330, 11);
      circle(80, 400, 11);
      circle(170, 470, 11);
      popMatrix();
    }
  }
}
希望这些评论能帮助说明这个想法

我注意到您在顶部声明了
PShape
变量,但没有使用

您也可以使用它。例如,您可以使用创建所有圆和三角形的
(当然,对每个基本体重复使用
createShape()
(例如
createShape(椭圆,x,y,w,h)
createShape(三角形,x1,y1,x2,y2,x3,y3)
,等等)最后,您只需在for循环中调用render组来渲染瓷砖:

color[] lemonspears =
  {
  #f9f8ae, //#0 highlight lemon
  #fffc24, //#1 mid lemon
  #d6c000, //#2 dark lemon
  #242000, //#3 detailing
  #e7b416, //#4 highlight pear 
  #a8da2b, //#5 mid pear
  #667a2a, //#6 dark pear
  #f2e6ba, //#7 light beige
  #8acdff, //#8 light blue
  #0551ff, //#9 dark blue

};

color palette[] = lemonspears;
// container for mulitple circles and triangles;
PShape pattern;

void setup()
{
  size(800, 800);
  
  pattern = createShape(GROUP);
  // outer circles
  pattern.addChild(circle(30, 40, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(760, 40, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(30, 760, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(760, 760, 200, 5, palette[9], palette[8]));
  
  //mid triangles
  pattern.addChild(triangle(350, 0, 405, 80, 460, 0, 5, palette[9], palette[8]));
  pattern.addChild(triangle(350, 800, 405, 720, 460, 800, 5, palette[9], palette[8]));
  
  //mid circle
  pattern.addChild(circle(405, 400, 200, 5, palette[4], palette[1]));
  
  // right dots
  pattern.addChild(circle(630, 330, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(720, 400, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(630, 470, 11, 3, palette[4], palette[1]));
  
  // left dots
  pattern.addChild(circle(170, 330, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(80, 400, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(170, 470, 11, 3, palette[4], palette[1]));
}


void draw() {
  background(palette[7]);
  scale(.3, .3);
  // for each row / column
  for(int i = 0 ; i < 3; i++){
    for(int j = 0 ; j < 3; j++){
      // offset the tile (for the sake of this demo based on mouseX
      pushMatrix();
      translate(150 + i * mouseX,150 + j * mouseX);
      // render the tile
      shape(pattern);
      popMatrix();
    }
  }
}
// utility function to create a circle and set it's styles
PShape circle(float x, float y, float size, float strokeSize, color stroke, color fill){
  PShape shape = createShape(ELLIPSE, x, y, size, size);
  shape.setStrokeWeight(strokeSize);
  shape.setStroke(stroke);
  shape.setFill(fill);
  return shape;
}
// utility function to create a triangle and set it's styles
PShape triangle(float x1, float y1, 
                float x2, float y2,
                float x3, float y3, 
                float strokeSize, color stroke, color fill){
  PShape shape = createShape(TRIANGLE, x1, y1, x2, y2, x3, y3);
  shape.setStrokeWeight(strokeSize);
  shape.setStroke(stroke);
  shape.setFill(fill);
  return shape;
}
color[]柠檬梨=
{
#f9f8ae,//#0高光柠檬
#fffc24,//#1中柠檬
#d6c000,//#2深柠檬色
#242000年,详细说明
#e7b416,//#4
#a8da2b,//#5中梨
#667a2a,/#6黑梨
#f2e6ba,//#7浅米色
#8acdff,/#8浅蓝色
#0551ff,深蓝色
};
调色板[]=柠檬梨;
//多个圆和三角形的容器;
形状图案;
无效设置()
{
大小(800800);
模式=创建形状(组);
//外圈
pattern.addChild(圆圈(30,40,200,5,调色板[9],调色板[8]);
pattern.addChild(圆圈(760,40200,5,调色板[9],调色板[8]);
pattern.addChild(圆圈(307602005,调色板[9],调色板[8]);
pattern.addChild(圆圈(760760200,5,调色板[9],调色板[8]);
//中间三角形
addChild(三角形(350,0405804600,0,5,调色板[9],调色板[8]);
addChild(三角形(350800405720460800,5,调色板[9],调色板[8]);
//中圈
pattern.addChild(圆圈(405400200,5,调色板[4],调色板[1]);
//右圆点
pattern.addChild(圆圈(630330,11,3,调色板[4],调色板[1]);
pattern.addChild(圆圈(720400,11,3,调色板[4],调色板[1]);
pattern.addChild(圆圈(63047011,3,调色板[4],调色板[1]);
//左点
pattern.addChild(圆圈(170330,11,3,调色板[4],调色板[1]);
pattern.addChild(圆圈(80400,11,3,调色板[4],调色板[1]);
pattern.addChild(圆圈(17047011,3,调色板[4],调色板[1]);
}
作废提款(){
背景(调色板[7]);
比例(.3,3);
//对于每行/每列
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
//偏移平铺(为了这个基于mouseX的演示)
pushMatrix();
翻译(150+i*mouseX,150+j*mouseX);
//渲染瓷砖
形状(图案);
popMatrix();
}
}
}
//用于创建圆并设置其样式的实用函数
P形状圆(浮动x、浮动y、浮动大小、浮动笔划大小、颜色笔划、颜色填充){
PShape shape=createShape(椭圆、x、y、大小、大小);
形状。设置行程重量(行程大小);
形状。设定行程(行程);
形状。设置填充(填充);
返回形状;
}
//用于创建三角形并设置其样式的实用函数
P形三角形(浮动x1、浮动y1、,
浮点数x2,浮点数y2,
浮球x3,浮球y3,
浮动笔划大小、颜色笔划、颜色填充){
PShape形状=创建形状(三角形,x1,y1,x2,y2,x3,y3);
形状。设置行程重量(行程大小);
形状。设定行程(行程);
形状。设置填充(填充);
返回形状;
}
color[] lemonspears =
  {
  #f9f8ae, //#0 highlight lemon
  #fffc24, //#1 mid lemon
  #d6c000, //#2 dark lemon
  #242000, //#3 detailing
  #e7b416, //#4 highlight pear 
  #a8da2b, //#5 mid pear
  #667a2a, //#6 dark pear
  #f2e6ba, //#7 light beige
  #8acdff, //#8 light blue
  #0551ff, //#9 dark blue

};

color palette[] = lemonspears;
// container for mulitple circles and triangles;
PShape pattern;

void setup()
{
  size(800, 800);
  
  pattern = createShape(GROUP);
  // outer circles
  pattern.addChild(circle(30, 40, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(760, 40, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(30, 760, 200, 5, palette[9], palette[8]));
  pattern.addChild(circle(760, 760, 200, 5, palette[9], palette[8]));
  
  //mid triangles
  pattern.addChild(triangle(350, 0, 405, 80, 460, 0, 5, palette[9], palette[8]));
  pattern.addChild(triangle(350, 800, 405, 720, 460, 800, 5, palette[9], palette[8]));
  
  //mid circle
  pattern.addChild(circle(405, 400, 200, 5, palette[4], palette[1]));
  
  // right dots
  pattern.addChild(circle(630, 330, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(720, 400, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(630, 470, 11, 3, palette[4], palette[1]));
  
  // left dots
  pattern.addChild(circle(170, 330, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(80, 400, 11, 3, palette[4], palette[1]));
  pattern.addChild(circle(170, 470, 11, 3, palette[4], palette[1]));
}


void draw() {
  background(palette[7]);
  scale(.3, .3);
  // for each row / column
  for(int i = 0 ; i < 3; i++){
    for(int j = 0 ; j < 3; j++){
      // offset the tile (for the sake of this demo based on mouseX
      pushMatrix();
      translate(150 + i * mouseX,150 + j * mouseX);
      // render the tile
      shape(pattern);
      popMatrix();
    }
  }
}
// utility function to create a circle and set it's styles
PShape circle(float x, float y, float size, float strokeSize, color stroke, color fill){
  PShape shape = createShape(ELLIPSE, x, y, size, size);
  shape.setStrokeWeight(strokeSize);
  shape.setStroke(stroke);
  shape.setFill(fill);
  return shape;
}
// utility function to create a triangle and set it's styles
PShape triangle(float x1, float y1, 
                float x2, float y2,
                float x3, float y3, 
                float strokeSize, color stroke, color fill){
  PShape shape = createShape(TRIANGLE, x1, y1, x2, y2, x3, y3);
  shape.setStrokeWeight(strokeSize);
  shape.setStroke(stroke);
  shape.setFill(fill);
  return shape;
}