2016-02-18 89 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。谢谢凯文。现在排序 –