Processing 如何以另一种(初学者)方式编写此处理代码?

Processing 如何以另一种(初学者)方式编写此处理代码?,processing,Processing,我如何用另一种(初学者)方式编写代码(如下)?我不希望使用createShape、setFill和addChild。相反,有没有其他方法来执行相同的操作 grid = createShape(GROUP) for i in range(C*R): self.cell = createShape(RECT, (i%C)*S, (i//C)*S, S, S) self.cell.setFill(colors[i] if i in filled else 210) grid.a

我如何用另一种(初学者)方式编写代码(如下)?我不希望使用createShape、setFill和addChild。相反,有没有其他方法来执行相同的操作

grid = createShape(GROUP)
for i in range(C*R):
    self.cell = createShape(RECT, (i%C)*S, (i//C)*S, S, S)
    self.cell.setFill(colors[i] if i in filled else 210)
    grid.addChild(self.cell)

假设您正在尝试创建矩形网格:

final int _numRows = 5;
final int _numCols = 7;

int l = 20;
int t = 20;
int w = 90;
int h = 60;
int hg = 10;
int vg = 10;
int left;
int top;
 
 void rectGrid() {
  for(int k = 0; k < _numRows; k++) {
   for(int j = 0; j < _numCols; j++){
   left = l + j*(w+vg);
   top = t + k*(h+hg);
   stroke(255);
   strokeWeight(2);
   fill(118);
   rect( left, top, w, h);
   }
  }
 }
 
 
void setup() { 
  size(800,500);
  background(0,0,245);
  rectGrid();
}

void draw() {
}

final int\u numRows=5;
最终整数=7;
int l=20;
int t=20;
int w=90;
int h=60;
int hg=10;
int vg=10;
int左;
int top;
void rectGrid(){
对于(int k=0;k<\u numRows;k++){
对于(int j=0;j<\u numCols;j++){
左=l+j*(w+vg);
顶部=t+k*(h+hg);
中风(255);
冲程重量(2);
填充(118);
矩形(左、上、w、h);
}
}
}
无效设置(){
规模(800500);
背景(0,0245);
矩形网格();
}
作废提款(){
}
添加颜色数组:

/*
 Adds color array to rectangle grid.
*/

final int _numRows = 5;
final int _numCols = 7;

int l = 20;
int t = 20;
int w = 90;
int h = 60;
int hg = 10;
int vg = 10;
int left;
int top;
int count = 0;
color[] c;

 void colorArray(){
   for(int x=0; x< _numRows*_numCols; x++){
     c[x] = color(random(255),random(255),random(255))
   }
 }  
 
 void rectGrid() {
  for(int k = 0; k < _numRows; k++) {
   for(int j = 0; j < _numCols; j++){
   left = l + j*(w+vg);
   top = t + k*(h+hg);
   stroke(255);
   strokeWeight(2);
   fill(c[count]);
   rect( left, top, w, h);
   count++;
   }
  }
 }
 
void setup() { 
  size(800,500);
  background(0,0,245);
  c = new color[_numCols*_numRows];
  colorArray();
  // Make sure the color array is filled first
  rectGrid();
}

void draw() {
}

/*
将颜色数组添加到矩形网格。
*/
最终整数=5;
最终整数=7;
int l=20;
int t=20;
int w=90;
int h=60;
int hg=10;
int vg=10;
int左;
int top;
整数计数=0;
颜色[]c;
void colorArray(){
对于(int x=0;x<\u numRows*\u numCols;x++){
c[x]=颜色(随机(255)、随机(255)、随机(255))
}
}  
void rectGrid(){
对于(int k=0;k<\u numRows;k++){
对于(int j=0;j<\u numCols;j++){
左=l+j*(w+vg);
顶部=t+k*(h+hg);
中风(255);
冲程重量(2);
填充(c[计数]);
矩形(左、上、w、h);
计数++;
}
}
}
无效设置(){
规模(800500);
背景(0,0245);
c=新颜色[_numCols*_numRows];
colorArray();
//确保先填充颜色数组
矩形网格();
}
作废提款(){
}

谢谢!但是,我如何在代码中包含我的代码的第四行,即self.cell.setFill(如果我填写了else 210,则使用颜色[I])?颜色[I]是一个数组,而您没有提供该信息。如果您提供,我很乐意添加它。颜色=[0]*(C)*(R+1),其中,C和R是列数和行数。顺便说一句,我使用的是python,我没有使用python,但它看起来只是初始化了一个颜色数组;它似乎没有给出任何关于颜色的信息。网格是什么样子的?有不同颜色的矩形吗?是的,我已经写了完整的代码。要我把密码发给你吗?出于某种原因,我不想在这里发布它。最简单的:
对于范围内的I(C*R):填充(如果我在其他210中填充颜色[I])rect(I%C)*S,(I//C)*S,S,S)
(缩进可能需要修复)…但这是效率最低的方法。您可以尝试使用在
setup()
中缓存网格,然后使用
image()
draw()中进行渲染,谢谢!你的代码可以工作,但是有一个问题与这里没有的一行有关!!!也就是说,这行-grid.addChild(单元格)?如何将其添加到代码中?请阅读并运行示例以了解其工作原理。我上面的例子每次都会重新绘制每个矩形,而PShape方法会创建形状(main
GROUP
shape with child
RECT
形状(因此
addChild
)),然后在
draw()
中使用
shape()
进行渲染。如果你正在绘制网格,你还应该练习用直线而不是矩形来绘制网格(
beginShape()
/
vertex()
/
endShape()
),这将更有效(删除重复项),但也是一个很好的智力难题;)