我从一个名为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