2017-04-24 1 views
0

ランダムな楕円が7列にわたって連続して描画されています。しかし、行配列内のどこにでも楕円の数をランダムに描画するのではなく、それらを描画したいので、列1の楕円の1つが列2の楕円の1つに接触する必要があります。最後のビジュアルは、異なるバーの高さでアニメーション化するが、楕円の配列を使用してバーグラフのように見えます。この画像に似ています。 graphランダムにピクセルの特定の場所にアクセスする処理

私の作業コードは以下のとおりです。 rowArray [i]が黒いピクセルの隣にあるかどうかを比較するためのif条件を実行するか、ここで見落としている簡単な方法がありますか?すべての助けに感謝します。ありがとう。

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]を使用して、白と黒のピクセルの位置を決定し、そこから進みます。

EDITED CODE

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

私は本当にあなたが何を求めているのか分かりません。一般的な「どのようにこれを行うのか」のタイプの質問を手伝うのは本当に難しいです。あなたが特定の質問をした場合、あなたはもっと良い運を得られるでしょう。あなたは[あなたの問題をより小さなものに分割する](http://happycoding.io/tutorials/how-to/program)して、一度に1つずつ取り上げる必要があります。あなたが特定の作品にこだわっている場合は、その作品の[mcve]を投稿することができます。がんばろう。 –

+0

私の質問の表現を改善する良い教訓です。申し訳ありませんが、私が自分でカバーした段階と、ここに投稿されたときにどのように翻訳されるのかは、当然のことです。あなたのアドバイスに基づいて、私は以下の答え(コメント内の限られた文字のため)で私の作業を分解しました。 – user2187427

答えて

1

EDITの-itはsolved.Codeは誰にも同様のトラブルが発生した場合には、以下の投稿になりました。

私は質問を言い換えた上記アドバイスに基づい:

Iは、無作為化配列の長さを作成し、行のランダムx量楕円を描くように、この配列をループ試みました。これは視覚的に、棒グラフのような異なる高さの一連の白い楕円に変換されます。以下の最小コードは配列の長さをループし、配列長の各ピクセルに楕円を順番に描画します。これは私が欲しいものです。しかし、ランダム化されているため、楕円の間に隙間(黒いピクセル)が残ることがあります。たとえば、行1では、3つの白い楕円を連続して描画し、次に1ピクセルのギャップ、次にその長さの4番目の楕円を描画します。私は 'ギャップ'を取り除こうとしています。このコードでは、別の描画シーケンスの後に「1つの楕円」を達成していますが、配列の長さに沿って楕円を作成する際に黒いギャップがあります。

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ループの次の構造を試しました ピクセルの高さ全体にループシーケンシングを追加し、次にランダムな長さを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

あなたの質問が解決した場合は、この回答を正しいものとしてマークしてください。 –

関連する問題