处理访问随机数组中像素的特定位置

Processing accessing specific location of pixel in random Array

我在 7 列中按顺序在行中绘制了随机椭圆。但是,我不想在行数组中的任何位置随机绘制椭圆的数量,而是只想绘制它们,以便第一列中的一个椭圆必须接触第二列中的一个椭圆等。这样位置之间就没有间隙了。最终的视觉效果看起来像一个条形图,在不同的条形高度上动画,但使用椭圆数组来实现。类似这张图。 graph

我的工作代码如下。我会转向访问像素颜色值并执行 'if condition' 来比较 rowArray[i] 是否在黑色像素旁边,或者是否有我在这里忽略的更简单的方法?所有帮助表示赞赏。谢谢

PImage pix = createImage(7, 7, RGB);
int counter = 0;
int counter2 = 0;
int y = 0;
int x = 0;
int rowArray[ ] = {0, 1, 2, 3, 4, 5, 6, 7};
int colArray[ ] = {0, 1, 2, 3, 4, 5, 6, 7};
int frameDelay = 300; //pause 400 ms between frames being sent to the board
float dot = 0;
int count;

void setup() {

  background(0);
  size(500, 500);
  dot = height/7.0;



  pix.loadPixels();
  for (int i = 0; i < pix.pixels.length; i++) {
    pix.pixels[i] = color(0);
  }
  pix.updatePixels();
  noStroke();
  ellipseMode(CORNER);
}

void draw() {

  //boolean dot = false;
  //randomSeed(0);
  pix.loadPixels();



  if (counter > pix.height) {
    counter = 0;
    y ++;
  }

  if (counter2 > pix.width) {
    counter2 = 0;
    x ++;
    //also refesh screen after one round
    refresh();
  }

  //reset-don't go beyond pixel boundaries
  if (x > pix.width) {
    x = 0;
  }
  if (y > pix.height) {
    y = 0;
  }
  for (int j = 0; j < pix.width; j++) {
    if (j==counter2) {
      for (int i = 0; i < pix.height; i++) {
        if (i == counter) {

          //random height

          i = int(random(rowArray.length));  // Same as int(random(i))
          y=i;
          x=j;
          //draw the white circles
          stroke(64);
          strokeWeight(1);
          fill(255);
          noStroke();
          ellipse(x*dot, y*dot, dot, dot);
        }
      }
    }
  }
  counter++;
  counter2++;



  pix.updatePixels();

  pix.loadPixels();



  delay (frameDelay);
}
void refresh() {

  background(0);
}

/编辑!!!!!/ 我已经简化了我的代码,因为它有一些不必要的 for 循环。现在使用 pixel [loc] 来确定白色和黑色像素的位置并从那里开始。

编辑代码

PImage pix = createImage(7, 7, RGB);
int counter = 0;
//int randCount=0;
int counter2 = 0;
int y = 0;
int x = 0;
//int randCount[ ] = {0, 1, 2, 3, 4, 5, 6};
int randCount[ ] =  new int[7];
//int rowArray[ ] = {0, 1, 2, 3, 4, 5, 6, 7};

int frameDelay = 300; //pause 400 ms between frames being sent to the board
float dotSize = 0;


void setup() {

  background(0);
  size(500, 500);
  dotSize = height/7.0;


  //make all dots black on start
  pix.loadPixels();
  for (int i = 0; i < pix.pixels.length; i++) {
    pix.pixels[i] = color(0);
  }
  pix.updatePixels();
  noStroke();
  ellipseMode(CORNER);
}

void draw() {

  //  boolean dot = false;

  pix.loadPixels();
  //bitshift values from array
  int row1 = 0;
  int row2 = 0;
  int row3 = 0;
  int row4 = 0;
  int row5 = 0;
  int row6 = 0;
  int row7 = 0;


  //randomise how many dots are displayed in the row
  int index = int(random(randCount.length));
  counter=index;

  if (counter > pix.height) {
    counter = 0;
    y ++;
  }

  if (counter2 > pix.width) {
    counter2 = 0;
    x ++;
  }


  //reset-don't go beyond pixel boundaries
  if (x > pix.width) x = 0;
  if (y > pix.height) y = 0;

  //sequence dots row by row 
  for (int i = 0; i < pix.height; i++) {
    if (i == counter) {

      //y is i
      y=i;

      //draw the white circles representing flipdots
      stroke(64);
      strokeWeight(1);
      fill(255);
      noStroke();
      ellipse(x*dotSize, y*dotSize, dotSize, dotSize);
    }
  }
  if (x==7) {
    //also refesh screen after one round
    refresh();
  }

  counter++;
  counter2++;
  detect();


  pix.updatePixels();

  pix.loadPixels();



  delay (frameDelay);
}


//screen refresh
void refresh() {

  background(0);
  y=0;
  x=0;
}

void detect() {
  //pixel location
  int loc = x + y*pix.height;

  // Pixel to the left location and color
  int leftLoc = (x - 1) + y*pix.width;

  // Pixel to the right location and color
  int rightLoc = (x + 1) + y*pix.width;

  // Pixel to the left location and color
  int downLoc = (x - 1) + y*pix.height;

  // Pixel to the right location and color
  int upLoc = (x + 1) + y*pix.height;

  //is the pixel white?
  if ((pix.pixels[loc]==255)&&(pix.pixels[leftLoc]==255)&&(pix.pixels[rightLoc]==255)&&(pix.pixels[downLoc]==255)&&(pix.pixels[upLoc]==255)) {
    y++;
    // x++;
  } else {
    y--;
  }
}

编辑 - 现在 solved.Code 发布在下面,以防其他人遇到类似的故障排除。

根据上述建议,我改写了问题:

我已经尝试创建一个随机数组长度并循环遍历该数组以连续绘制随机 x 数量的椭圆。这在视觉上转化为一系列不同高度的白色椭圆,如条形图。下面的最小代码循环遍历数组长度,并成功地在数组长度中的每个像素处依次绘制一个椭圆。这就是我要的。但是,因为它是随机的,所以有时会在椭圆之间留下间隙(黑色像素)。例如,在第 1 行中,它可能依次绘制 3 个白色椭圆,然后是 1 个像素的间隙,然后是长度上的第 4 个椭圆。我正在尝试删除 'gap'。此代码实现了我的目标 'one ellipse after another draw sequence' 但在沿数组长度创建椭圆时存在黑色间隙。

PImage pix = createImage(7, 7, RGB);
int counter = 0;
//int randCount=0;
int counter2 = 0;
int y = 0;
int x = 0;
int lastY=0;
//int randCount[ ] = {0, 1, 2, 3, 4, 5, 6};
int randCount[ ] =  new int[7];
int rowArray[ ] = {0, 1, 2, 3, 4, 5, 6};
int colArray[]= new int[7];
int frameDelay = 500; //pause 400 ms between frames being sent to the board
float dotSize = 0;


void setup() {

  background(0);
  size(500, 500);
  dotSize = height/7.0;


  //make all dots black on start
  pix.loadPixels();
  for (int i = 0; i < pix.pixels.length; i++) {
    pix.pixels[i] = color(0);
  }
  pix.updatePixels();
  noStroke();
  ellipseMode(CORNER);
}

void draw() {



  pix.loadPixels();


  //here do sequential index plus a random value
  // for(int j = 0; j < rowArray.length; j++){

  //randomise how many dots are displayed in the row
  int index = int(random(randCount.length));

  //counter=index;

  //if beyond pixel boundaries
  if (counter > pix.height) {
    counter = 0;
    y ++;
  }

  if (counter2 > pix.width) {
    counter2 = 0;
    x ++;
  }


  //reset-don't go beyond pixel boundaries
  if (x > pix.width) x = 0;
  if (y > pix.height) y = 0;

  //sequence dots row by row 

  //loop through the randomised array lengths.  
  for (int i=0; i<index; i++) {



    // if dot is within boundary and sequencial.  
    if (i == counter) {

      //y is i. height is i.
      y=i;


      //draw the white circles representing flipdots
      stroke(64);
      strokeWeight(1);
      fill(255);
      noStroke();
      ellipse(x*dotSize, y*dotSize, dotSize, dotSize);
    }
  }


  if (x==7) {
    //also refesh screen after one round
    refresh();
  }

  counter++;
  counter2++;



  pix.updatePixels();

  pix.loadPixels();



  //time between dot animations
  delay (frameDelay);
}


//screen refresh
void refresh() {

  background(0);
  y=0;
  x=0;
}

我认识到问题在于 for 循环的构造方式。然后我尝试了以下 for 循环结构,它解决了 'pixel gap' 通过在整个像素高度添加第二个 for 循环排序,然后将随机长度减去 pixel.height 长度。这现在有效。

  //sequence dots row by row 

  //loop through the randomised array lengths.  
  for (int i=0; i<index; i++) {

  for (int j=0; j<index; j++) {

    // if dot is within boundary and sequencial.  
    if (i == counter) {

      //y is i. height is i.
      y=i-j;


      //draw the white circles representing flipdots
      stroke(64);
      strokeWeight(1);
      fill(255);
      noStroke();
      ellipse(x*dotSize, y*dotSize, dotSize, dotSize);
    }
  }
  }

因此,我将继续尝试解决我的 for 循环的构造问题,该循环绘制随机长度的椭圆,但行中该长度之间没有任何间隙。我希望这更清楚,更符合如何在论坛上构建问题。 谢谢