我有随机的椭圆按顺序绘制7列。但是,我不想在行数组中随机绘制椭圆的数量,而只想绘制它们,因此第一列中的一个椭圆必须与第二列中的一个椭圆相接触,以便在位置之间没有间隙。最终的视觉看起来像一个条形图,在不同的高度进行动画,但使用椭圆数组来实现。与此图像类似。 graph处理随机访问像素的特定位置阵列
我的工作代码如下。我会移动到访问像素颜色值并执行'if条件'来比较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);
}
/EDIT !!!!!/ 我简化了我的代码,因为它有一些不必要的循环。现在使用像素[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--;
}
}
我真的不知道你在问什么。对于一般的“我该如何做这个”类型的问题很难提供帮助。如果你问一个特定的“我试过X,期望Y,但是改为Z”类型的问题,你会有更好的运气。你需要[将你的问题分解成更小的部分](http://happycoding.io/tutorials/how-to/program),然后逐个处理这些部分。那么如果你被困在一个特定的部分,你可以发布一个[mcve]。祝你好运。 –
在改善我的问题的措辞方面有很好的教训。抱歉,我认为我自己已经报道过的阶段以及在这里发布时如何翻译。根据你的建议,我已经在下面的答案中分解了我的工作(由于评论中的字符有限)。 – user2187427