2017-04-24 3 views
0

7 개의 열에 걸쳐 순차적으로 행에 임의의 타원이 그려져 있습니다. 그러나 행 배열 내의 임의의 위치에 임의로 타원 수를 그리는 대신 열만있는 타원 중 하나가 두 열의 타원 중 하나를 만지도록 위치 만 그리면 위치간에 틈이 없어집니다. 마지막 비주얼은 막대 그래프와 같이 보이지만 다른 막대 높이에서 애니메이션을 만들지 만 타원 배열을 사용하면 그렇게 할 수 있습니다. 이 이미지와 비슷합니다. graph임의의 픽셀의 특정 위치에 액세스하는 처리 Array

내 작업 코드는 다음과 같습니다. 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); 
} 

/편집 !!!!!/ 일부 불필요한 루프가 있으므로 코드를 간소화했습니다. 이제 픽셀 [loc]을 사용하여 흰색과 검정색 픽셀의 위치를 ​​결정하고 거기에서부터 이동하십시오.

편집을 할 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

정말 당신이 무엇을 요구하는지 모르겠습니다. 일반적인 "어떻게해야합니까?"라는 질문을하는 것이 정말 어렵습니다. 특정 질문을하면 훨씬 더 나은 행운이 생길 것입니다. "X를 시도했는데 Y가 예상되었지만 Z가 대신 입력되었습니다."질문을 입력하십시오. 당신은 [작은 조각으로 당신의 문제를 깰 수] (http://happycoding.io/tutorials/how-to/program)하고 그 조각을 한 번에 하나씩 가져 가야합니다. 그런 다음 특정 곡에 붙어 있다면 그 곡의 [mcve]를 올릴 수 있습니다. 행운을 빕니다. –

+0

제 질문의 표현을 개선하는 데 좋은 교훈입니다. 미안 해요. 제가 혼자서 다뤘던 단계와 여기에 올릴 때 어떻게 번역했는지 당연한 생각이었습니다.귀하의 조언을 바탕으로 나는 아래 답변에서 내 의견을 (제한된 문자로 인해) 세분화했습니다. – user2187427

답변

1

편집 - 그것은 지금의 경우 다른 사람이 비슷한 문제가 발생 이하에 게시 solved.Code된다. I는 무작위 배열 길이를 생성하고 연속 랜덤 X 량 타원을 그릴이 배열을 통해 반복 시도

: I 질문을 재 진술 한 상기 통보에 기초

. 이것은 막대 그래프와 같이 높이가 다른 일련의 흰색 타원으로 시각적으로 변환됩니다. 아래의 최소 코드는 배열 길이를 반복하며 배열 길이의 각 픽셀에 차례로 타원을 성공적으로 그립니다. 이것이 내가 원하는거야. 그러나 무작위로 지정되기 때문에 때로는 타원 사이에 간격 (검은 색 픽셀)이 생깁니다. 예를 들어, 행 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 루프의 다음 구조를 시도했다.픽셀 순열 길이만큼 무작위 길이를 뺀 다음 전체 픽셀 높이를 통해 루프 시퀀싱을위한 두 번째를 추가하여 두 번째를 추가한다. 이제 작동합니다.

//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

질문이 해결되면이 대답을 올바른 것으로 표시하고 싶을 수 있습니다. –