2016-02-11 46 views
0

我試圖在Processing上實現動畫螺旋曲線,以便在每個draw()函數內逐漸構建曲線。我已經成功地將曲線創建爲靜態形狀 - 現在我正在嘗試更進一步。使用curveVertex動畫曲線Proccesing

不幸的是,儘管我的努力,我的代碼似乎並沒有工作。經過一段等待時間我與以下錯誤消息一起再次獲得形狀靜:

您必須使用curveVertex前beginShape()或beginShape(多邊形)()

與此相反的消息,我已經一個beginShape()指令

這裏是我的代碼(也許在錯誤的地方?):

float x,y; 
float stepSize; 
float angle; 

int counter; 

void setup() { 
    size(900, 600); 
    background(255); 
    frameRate(30); 
    smooth(); 
    //noStroke(); 
    stroke(0); 

    x = 0; 
    y = 0; 
    stepSize = 6; 
    angle = radians(270); 
    counter = 0; 
} 

void draw() { 
    translate(width/3, height/2); 
    if (counter == 0) { 
    beginShape(); 
    curveVertex(x, y); // initial control point 
    } else { 
    if (stepSize > 1.0) { 
     curveVertex(x, y); 

     x = x + cos(angle) * stepSize; 
     y = y + sin(angle) * stepSize; 
     stepSize = stepSize * 0.99; 
     angle = angle - radians(1.5);  
    } else { 
     // return to previous x,y values for last control point 
     angle = angle + radians(1.5); 
     stepSize = stepSize/0.99; 
     y = y - sin(angle) * stepSize; 
     x = x - cos(angle) * stepSize; 

     curveVertex(x, y); // last control point 
     endShape(); 
    } 
    } 
    counter++; 
} 

非常感謝您提供的幫助! - 伊利亞斯

回答

0

這似乎很奇怪,試圖beginShape()一個,curveVertex()endShape()多個電話之間組分流到draw()功能。

相反,你應該跟蹤你想繪製的每一點 - ArrayList<PVector>會派上用場。要繪製曲線,只需遍歷該曲線並繪製每個點。要延長曲線,只需添加一個PVector即可。

float stepSize = 6.0; 
float angle= radians(270); 

ArrayList<PVector> points = new ArrayList<PVector>(); 

void setup() { 
    size(900, 600); 
    frameRate(30); 
    points.add(new PVector(0, 0)); 
} 

void draw() { 
    background(255); 

    translate(width/3, height/2); 

    //draw previous points 
    beginShape(); 
    for (PVector point : points) { 
    curveVertex(point.x, point.y); 
    } 
    endShape(); 

    if (stepSize > 1.0) { 
    //add a new point 
    PVector prevPoint = points.get(points.size()-1); 

    float x = prevPoint.x + cos(angle) * stepSize; 
    float y = prevPoint.y + sin(angle) * stepSize; 
    points.add(new PVector(x, y)); 

    stepSize = stepSize * 0.99; 
    angle = angle - radians(1.5); 
    } 
} 

你可以只存儲最近繪製的點和積累的繪製調用,但重繪每一點可能是這種類型的問題,最常見的辦法脫身。

+0

謝謝凱文,給出了聰明的答案!我這麼知道增強循環,這是我以前不知道的;;) –