仅在处理中基于一个属性对对象数组进行排序
Sorting an array of objects based on one attribute only in Processing
我有一系列随机绘制的线,来自 class 称为线。
我已将所有对象放入一个数组中。我想用虚线连接任何彼此靠近的线。我能想到的最简单的方法是说如果 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.
问题在于一条线由两点组成,尽管连在一起(双关语意),但您需要独立检查每条线的点 .您真正不需要检查的唯一点是同一 Line 实例中的其他点。
在这种情况下,获得积分 class 可能最符合您的利益。然后 Line 将使用 Point 实例来定义两端而不是原始浮点坐标。通过这种方式,您可以同时拥有线列表和点列表。
通过这种方式,您可以按 x 坐标或 y 坐标对点进行排序,并抓取距离您的点 5 个像素以内的所有点(当然,这不是同一实例或 Line 实例中的其他点)。
能够将处理拆分为点和线很重要,因为您使用多个视图来处理相同的数据。作为一般规则,当以当前形式处理起来变得麻烦时,您应该重新排列所述数据。但是,如果我可以提出建议,那么排序并不是绝对必要的。如果您正在检查一个点和所有其他点,则必须根据当前点重复排序,这比简单地在列表中传递以处理足够接近的所有其他点要多得多。
就像另一个答案所说的那样,您需要比较两个端点才能有意义。您也不必对任何内容进行排序。
您应该使用 dist()
函数而不是尝试仅比较 x 坐标。 dist()
函数取 2 个点并给出它们的距离。您可以使用它来检查两个点是否彼此靠近:
float x1 = 75;
float y1 = 100;
float x2 = 25;
float y2 = 125;
float distance = dist(x1, y1, x2, y2);
if(distance < 100){
println("close");
}
您可以在 Line
class 中使用此功能循环遍历其他线并检查关闭点,或找到最近的点,无论您想要什么。
一如既往,我建议您尝试一些东西,如果遇到困难再问一个问题。
我有一系列随机绘制的线,来自 class 称为线。 我已将所有对象放入一个数组中。我想用虚线连接任何彼此靠近的线。我能想到的最简单的方法是说如果 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.
问题在于一条线由两点组成,尽管连在一起(双关语意),但您需要独立检查每条线的点 .您真正不需要检查的唯一点是同一 Line 实例中的其他点。
在这种情况下,获得积分 class 可能最符合您的利益。然后 Line 将使用 Point 实例来定义两端而不是原始浮点坐标。通过这种方式,您可以同时拥有线列表和点列表。
通过这种方式,您可以按 x 坐标或 y 坐标对点进行排序,并抓取距离您的点 5 个像素以内的所有点(当然,这不是同一实例或 Line 实例中的其他点)。
能够将处理拆分为点和线很重要,因为您使用多个视图来处理相同的数据。作为一般规则,当以当前形式处理起来变得麻烦时,您应该重新排列所述数据。但是,如果我可以提出建议,那么排序并不是绝对必要的。如果您正在检查一个点和所有其他点,则必须根据当前点重复排序,这比简单地在列表中传递以处理足够接近的所有其他点要多得多。
就像另一个答案所说的那样,您需要比较两个端点才能有意义。您也不必对任何内容进行排序。
您应该使用 dist()
函数而不是尝试仅比较 x 坐标。 dist()
函数取 2 个点并给出它们的距离。您可以使用它来检查两个点是否彼此靠近:
float x1 = 75;
float y1 = 100;
float x2 = 25;
float y2 = 125;
float distance = dist(x1, y1, x2, y2);
if(distance < 100){
println("close");
}
您可以在 Line
class 中使用此功能循环遍历其他线并检查关闭点,或找到最近的点,无论您想要什么。
一如既往,我建议您尝试一些东西,如果遇到困难再问一个问题。