2017-03-07 84 views
1

我正在處理草圖,其中使用二維數組中的點繪製Delaunay圖。 我隨機生成陣列中的點,所以現在它們正在快速生成,因爲draw()循環運行。處理中的間隔運行功能

for(int i = 0; i < points.length; i++){ 
    pX = random(nX); 
    pY = random(nY); 
    points[i][0] = pX; 
    points[i][1] = pY; 
} 

我要生成這些數字,例如每1秒。我試着用下面的代碼,但線路被只繪製一個很短的時間每隔1秒,間隔似乎有所不同......

if (millis() - timeCheck > timeInterval){ 
    for(int i = 0; i < points.length; i++){ 
    timeCheck = millis(); 

    pX = random(nX); 
    pY = random(nY); 

    points[i][0] = pX; 
    points[i][1] = pY; 
    } 
} 

我的最終目標是讓老之間的變速線新的點,因爲它們正在生成。我想使用寬鬆的方式,所以整個事情看起來不錯,順暢。

這是整個代碼。我使用mesh library來繪製網格。

import megamu.mesh.*; 
import processing.svg.*; 
import processing.pdf.*; 

    boolean recording = false; 
    void rec() { 
    if (key == 'r' || key == 'R') { 
     recording = !recording; 
    } 
    } 

    float numX; 
    float numY; 

    float x; 
    float y; 

    float offset = 0.00; 
    float easing = 0.05; 

    Delaunay myDelaunay ; 

    int timeCheck; 
    int timeInterval = 1000; 

void setup(){ 

    size(600,400); 

    timeCheck = millis(); 

} 

void draw(){ 

    rec(); 
    if (recording){ 
    beginRecord(SVG, "####.svg"); 
    } 

    offset = offset + .005; 
    background(noise(offset) * 50); 
    stroke((noise(offset) * 255)+100, (noise(offset) * 100)+50, noise(offset) * 255); 

    float[][] points = new float[10][2]; 

    numX = (width); 
    numY = (height); 

    float nX = noise(offset) * width; 
    float pX = random(nX); 
    float targetX = random(nX); 
    float dX; 

    float nY = noise(offset) * height; 
    float pY = random(nY); 
    float targetY = random(nY); 
    float dY; 

    if (millis() - timeCheck > timeInterval){ 
    for(int i = 0; i < points.length; i++){ 
     timeCheck = millis(); 
     //println(timeCheck); 

     pX = random(nX); 
     pY = random(nY); 

     points[i][0] = pX; 
     points[i][1] = pY; 

    } 
    } 

    myDelaunay = new Delaunay(points); 

    float[][] myEdges = myDelaunay.getEdges(); 

    for(int i=0; i<myEdges.length; i++) { 

     dX = targetX - pX; 
     x += dX * easing; 
     dY = targetY - pY; 
     y += dY * easing; 

     float startX = myEdges[i][0]; 
     float startY = myEdges[i][1]; 
     float endX = myEdges[i][2]; 
     float endY = myEdges[i][3]; 
     line(startX, startY, endX, endY); 
     ellipse(startX, startY, 5, 5); 
    } 

    endRecord(); 

} 

回答

0

如果你想減慢調用整個繪製循環的速度,frameRate()是你的朋友。它以一個整數參數作爲每秒繪製循環被調用的次數。它默認爲60,但調用它可以將其更改爲任何你想要的。我只是把這個放在這裏,這是你想要的。

如果你想在相同的幀速率,但只有在for循環中,而出現一次,使用模數師。處理將draw()循環的調用次數存儲爲稱爲frameCount的變量。你可以使用模數師像這樣使它使得每秒的循環只調用一次:

if(frameCount % 60 == 1){//such that you generate the values on frame 1, and every 60th frame after that, assumes the frame rate is 60 fps. 
    for(int i = 0; i < points.length; i++){ 
     pX = random(nX); 
     pY = random(nY); 
     points[i][0] = pX; 
     points[i][1] = pY; 
    } 
} 

鏈接,瞭解更多有關frameRate()here

+0

是否有你使用'frameCount%60 == 1'而不是'frameCount%60 == 0'的原因? –

+0

@KevinWorkman這是我的印象,在調用'draw()'後,'frameCount = 1',所以'frameCount%60 == 1'將允許在draw()的第一次迭代中生成這些點。 – UnknowableIneffable

+0

是的'frameCount'確實從'1'開始。有趣的一點。 –