2016-02-26 48 views
0

Im新處理。我想學習如何繪製我正在做的一些建模,我使用gwoptics來做到這一點。他們有一個名爲RollingGraph的例子。這基本上繪製出任何你想要的沿時間滾動x軸。如何從類中返回值,從處理中的數組中派生

問題是我不太明白我如何繪製我想要的東西。

我有一個數組,讓我們說哪個在畫布上隨機繪製橢圓,並且這些橢圓會隨機地在每一幀中旋轉。我如何獲得滾動圖來繪製所有旋轉的總和,可以是circle.rot?

到目前爲止,我有最好的MCVE我能得到....

import org.gwoptics.graphics.graph2D.Graph2D; 
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation; 
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace; 



    class eq implements ILine2DEquation{ 
     public double computePoint(double x,int pos) { 
     return mouseX; //////HOW DO I GET THIS TO RETURN THE SUM OF circle.rot?????? 
     } 
    } 

    class eq2 implements ILine2DEquation{ 
     public double computePoint(double x,int pos) { 
     return mouseY; 
     }  
    } 

    class eq3 implements ILine2DEquation{ 
     public double computePoint(double x,int pos) { 
     if(mousePressed) 
      return 400; 
     else 
      return 0; 
     }  
    } 

    RollingLine2DTrace roll,roll2,roll3; 
    Graph2D g; 


class Circle{ 
    public float x1; 
    public float y1; 
    public float x2; 
    public float y2; 
    public color cB; 
    public float rot; 
    public float authority; 
    public float fert = 1; 
    public float r = x1; //radius 


    public Circle(float x1, float y1, float x2, float y2, color tempcB, float rot, float authority, float fert){ 
      this.x1 = x1; 
      this.y1 = y1; 
      this.x2 = x2; 
      this.y2 = y2; 
      this.cB = tempcB; 
      this.authority = random(255); 
      this.fert = random(1); 
      this.rot= random(360); 
     } 
    } 

    public ArrayList<Circle> circles = new ArrayList<Circle>(); 

    void setup(){ 
     size(1000, 1000); 
     frameRate(6); 
     rectMode(CENTER); 
     ellipseMode(CENTER); 

     int sec = second(); 

     roll = new RollingLine2DTrace(new eq() ,100,0.01f); 
     roll.setTraceColour(0, 255, 0); 


     roll2 = new RollingLine2DTrace(new eq2(),100,0.01f); 
     roll2.setTraceColour(255, 0, 0); 

     roll3 = new RollingLine2DTrace(new eq3(),100,0.05f); 
     roll3.setTraceColour(255, 255, 255); 

     g = new Graph2D(this, 400, 200, false); 
     g.setYAxisMax(600); 
     g.addTrace(roll); 
     g.addTrace(roll2); 
     g.addTrace(roll3); 
     g.position.y = 50; 
     g.position.x = 100; 
     g.setYAxisTickSpacing(100); 
     g.setXAxisMax(5f); 
     smooth(); 
     background(204); 

     noStroke(); 
     fill(255, 204,100); 
      for(int i = 1; i < 48; i++){ 
      float r = random(100,height-200); 
      float s = random(100,width-200); 
      float t = 20; 
      float u = 20; 
      circles.add(new Circle(r,s,t,u,color(100,14,14),random(360),color(100,14,14),random(10))); 
     } 
    } 

    void draw() { 
     background(204); 
     g.draw(); 

    for(Circle circle : circles){ 
     pushMatrix(); 
      translate(circle.x1, circle.y1); 
      rotate(random(360)); 
      translate(-circle.x1, -circle.y1); 
      fill(circle.authority); 
      strokeWeight(0); 
      stroke(100,0,0); 
      rect(circle.x1, circle.y1, 24, 36,0, 0, 12, 18); 
      popMatrix(); 
     } 
    } 

回答

1

如果我理解你的問題,所有你需要做的就是遍歷實例和計算總。

首先,你將需要一個數據結構來保存你的所有實例。你已經說過你正在使用一個數組,所以它聽起來像你已經覆蓋。你必須確保這個數組是的範圍下一步,所以這可能意味着在草圖級別(不在函數或類中)聲明它。其次,你將需要一個for循環來迭代數據結構中的實例。你可以使用一個變量來加總。事情是這樣的:

float total = 0; 
for(Circle c : yourCircleArray){ 
    total += c.rot; 
} 

你可能將它放入一個功能,讓你可以調用它,只要你想要的。

編輯:在你的代碼更仔細地觀察,你確實有一個ArrayList,而不是一個數組。它看起來像它在小品水平已經初始化,因此,所有你需要做的是這樣的:

public double computePoint() { 
    float total = 0; 
    for(Circle c : circles){ 
     total += c.rot; 
    } 
    return total; 
} 

如果你不能得到那個工作,試圖通過消除你的任何圖書館的依存關係創建MCVE導入在草圖的頂部。請記住,MCVE應該將其縮小到一個特定問題(如何從數組中獲得總數),而不是完成最終目標。