我從一個名爲Line的類中有一系列隨機繪製的線。 我已將所有對象放入數組中。我想用虛線連接彼此靠近的任何線。我能想到的最簡單的方法就是說,如果x1座標是距另一條線的x1 5個像素,然後繪製連接兩個x1座標的虛線。 我遇到的問題是如何比較所有x1座標和所有其他x1座標。我認爲這應該涉及1.排序數組,然後2.比較連續的數組元素。不過,我只想對x1進行排序,但我不知道如何執行此操作。僅在處理中基於一個屬性對對象數組進行排序
這是到目前爲止我的代碼:
class Line{
public float x1;
public float y1;
public float x2;
public float y2;
public color cB;
public float rot;
public float fat;
public Line(float x1, float y1, float x2, float y2, color tempcB, float rot, float fat){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.cB = tempcB;
this.rot = rot;
this.fat = fat;
};void draw(){
line(x1, y1, x2, y2);
//float rot = random(360);
float fat = random(5);
strokeWeight(fat);
////stroke (red,green,blue,opacity)
stroke(fat*100, 0, 0);
rotate(rot);
}
}
//Create array of objects
ArrayList<Line> lines = new ArrayList<Line>();
void setup(){
background(204);
size(600, 600);
for(int i = 0; i < 200; i++){
float r = random(500);
float s = random(500);
lines.add(new Line(r,s,r+10,s+10,color(255,255,255),random(360),random(5)));
}
//Draw out all the lines from the array
for(Line line : lines){
line.draw();
//Print them all out
println(line.x1,line.y1,line.x2,line.y2,line.cB,line.rot,line.fat);
}
}
//Now create connections between the elements
//If the x1 of the line is <5 pixels from another line then create a dotted line between the x1 points.
嗯。想想看,我可以把線條做成矩形。我想我可以檢查中心點的距離。你會介意這個問題嗎? –
@SebastianZeki這會簡化問題,但我認爲這不會完全準確。 – Neil