2016-02-18 94 views
1

我想創建一個包含隨機放置的隨機角度和strokeWeight線條的草圖。這個想法是,在每一幀,線條都會根據預先設定的規則稍微改變其傾斜度,而這依然取決於strokeWeight和其他參數。爲此,我想我需要在設置時記錄每一行的座標,然後轉換它們,然後再次記錄它們。如果這是有道理的。 基本上這個問題歸結爲我如何記錄畫布上所有線條的座標?如何在畫布上存儲所有形狀的座標

這是我到目前爲止寫,但我怎麼保存的形狀座標:

//Sets up the random lines on the canvas 
//Sets up the authorities (line widths) 
//Sets up the connections between lines 
void setup() { 
    background(204); 
    size(800, 800); 
    for (int x=1;x <1000;x +=1){ 
    fill(75, 70, 80); 
    //For the x y cords 
    float r = random(800); 
    float s = random(800); 
    //for the strokeWeight 
    float fat = random(5); 
    //for the stroke colour which varies with stroke weight 
    float col = random(350); 
    float red = random(350); 
    float g = random(350); 
    float b = random(350); 
    //for the initial tilt 
    float rot = random(360); 


    //stroke(242, 204, 47, 255); 
    strokeWeight(fat); 
    //stroke (red,green,blue,opacity) 
    stroke(fat*100, 180, 180); 
    //stroke(242, 204, 47, 255); 
    line(r,s,r+10,s+10); 
    rotate(radians(rot)); 
} 
} 



//This part is the diffusion animation 
void draw() { 

} 

回答

1

您可能希望創建一個Line類,用於保存畫一條線所需的信息。事情是這樣的:

class Line{ 
    float x1, y1, x2, y2; 

    public Line(float x1, float y1, float x2, float y2){ 
     this.x1 = x1; 
     this.y1 = y1 
     this.x2 = x2; 
     this.y2 = y2; 
    } 

    public void draw(){ 
     line(x1, y1, x2, y2); 
    } 

    public boolean intersects(Line other){ 
     //left as exercise for reader 
    } 
} 

然後你就可以隨機生成線:

ArrayList<Line> lines = new ArrayList<Line>(); 

void setup(){ 
    for(int i = 0; i < 10; i++){ 
     lines.add(new Line(random(width), random(height), random(width), random(height)); 
    } 
} 

void draw(){ 
    background(0); 
    for(Line line : lines){ 
     line.draw(); 
    } 
} 

這是你需要創建一個數據結構是什麼我得到的,當我告訴你在你的其他問題並存儲你的線座標。從這裏你可以編寫邏輯來移動你的線條,或者檢測十字路口等。

+0

很好。我必須移動for(Line line:lines){等塊設置,否則它會連續繪製隨機線條,導致畫布變黑。否則完美。我在學習...... –

+0

@SebastianZeki這不是我的代碼。這隻會在你將我的調用去掉「background(0)'後纔會發生,然後將這些行的隨機化移動到他們的'draw()'函數中,這是你不應該做的。 –

+0

Ahhhh。謝謝凱文。現在排序 –