我有一个swing应用程序,其中我有一个图像上某些点的ArrayList。 现在我想连接这些点,使每个点连接到它最近的邻居。
像这样:
所以我开始这样:
Minuatiae.java
public class Minutiae {
private int x;
private int y;
public Minutiae(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Manager.java
List<Minutiae> minuatiaePoints = new ArrayList<Minutiae>(minutiae);
for(int i = 0; i<minuatiaePoints.size(); i++){
Minutiae mPoint = minuatiaePoints.get(i);
Minutiae minPoint = minuatiaePoints.get((i+1) % minuatiaePoints.size());
int minXDistance = minPoint.getX()-mPoint.getX();
int minYDistance = minPoint.getY()-mPoint.getY();
double minDist = Math.hypot(minXDistance, minYDistance);
for(int j = 0; j < minuatiaePoints.size(); j++) // <- you had i++ here!
{
if (i == j) {
continue;
}
Minutiae testPt = minuatiaePoints.get(j);
double dist = Math.hypot(mPoint.getX() - testPt.getX(), mPoint.getY() - testPt.getY());
if (dist < minDist)
{
minDist = dist;
minPoint = testPt;
}
}
g2D.drawLine(mPoint.getX(), mPoint.getY(), minPoint.getX(), minPoint.getY());
}
但它只连接到一个最近的点。
有人可以帮我吗? 任何链接或示例代码都将非常感激。
答案 0 :(得分:1)
您有两种选择:
一旦你有了最小距离,就可以在你的清单上再找一个并连接所有最小距离=你的最小距离(因此你只需要找到最小距离)。如果dist = min distance,那么在第二次运行时,每次平等时都会绘制线条。
第二个选项是保留最小距离点列表,然后通过列表绘制线条。
编辑:更新为第二个algortihm添加代码:
List<Minutiae> minuatiaePoints = new ArrayList<Minutiae>(minutiae);
for(int i = 0; i<minuatiaePoints.size(); i++){
Minutiae mPoint = minuatiaePoints.get(i);
Minutiae minPoint = minuatiaePoints.get((i+1) % minuatiaePoints.size());
int minXDistance = minPoint.getX()-mPoint.getX();
int minYDistance = minPoint.getY()-mPoint.getY();
double minDist = Math.hypot(minXDistance, minYDistance);
List<Minutiae> minDistPoints = new ArrayList<Minutiae>();
for(int j = 0; j < minuatiaePoints.size(); j++) // <- you had i++ here!
{
if (i == j) {
continue;
}
Minutiae testPt = minuatiaePoints.get(j);
double dist = Math.hypot(mPoint.getX() - testPt.getX(), mPoint.getY() - testPt.getY());
if (dist < minDist)
{
minDist = dist;
minDistPoints = new ArrayList<Minutiae>();
minDistPoints.add(testPt);
} else if (dist = minDist) {
minDistPoints.add(testPt);
}
}
for(Minutae p: minDistPoints){
g2D.drawLine(mPoint.getX(), mPoint.getY(), p.getX(), p.getY());
}
}