2017-04-24 51 views
0

我有隨機的橢圓按順序繪製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--; 
    } 
} 
+1

我真的不知道你在問什麼。對於一般的「我該如何做這個」類型的問題很難提供幫助。如果你問一個特定的「我試過X,期望Y,但是改爲Z」類型的問題,你會有更好的運氣。你需要[將你的問題分解成更小的部分](http://happycoding.io/tutorials/how-to/program),然後逐個處理這些部分。那麼如果你被困在一個特定的部分,你可以發佈一個[mcve]。祝你好運。 –

+0

在改善我的問題的措辭方面有很好的教訓。抱歉,我認爲我自己已經報道過的階段以及在這裏發佈時如何翻譯。根據你的建議,我已經在下面的答案中分解了我的工作(由於評論中的字符有限)。 – user2187427

回答

1

編輯 - 它現在solved.Code下面貼的情況下,任何人都遇到類似的故障排除。

基於建議上述我已改寫這樣的問題:

我試圖創建一個隨機陣列的長度和通過該循環陣列中的行繪製隨機x量橢圓。這在視覺上轉化爲一系列不同高度的白色橢圓,如條形圖。下面的最小代碼循環遍歷數組長度,並順序地在數組長度的每個像素處成功繪製一個橢圓。這就是我要的。但是,由於它是隨機的,它有時會在橢圓之間留下間隙(黑色像素)。例如,在第1行中,它可以依次繪製3個白色橢圓,然後是1個像素的間隙,然後是長度中的第4個橢圓。我試圖消除'差距'。這個代碼實現了我所瞄準的'一個接一個的橢圓接着繪製序列',但是在沿陣列長度創建橢圓時有黑色間隙。

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.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); 
    } 
    } 
    } 

因此,我繼續嘗試解決我的針對繪製橢圓的,但沒有列在長度之間沒有任何間隙隨機長度環路建設。我希望這更清楚,更符合如何在論壇上構建問題。 謝謝

+0

如果您的問題已解決,您可能需要將此答案標記爲正確。 –