1

我有一个摇摆应用程序,其中我有一个图像上一些点的 ArrayList。现在我想连接这些点,使每个点都连接到它最近的邻居。

像这样:

在此处输入图像描述

所以我是这样开始的:

细枝末节.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;
 }
}

管理器.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());
        }

但它只连接到一个最近的点。

谁能帮我解决这个问题?任何链接或示例代码将不胜感激。

4

1 回答 1

1

你有两个选择:

一旦你有了最小距离,在你的列表中再去一次,连接所有最小距离=你的最小距离的人(因此你只需要找到最小距离)。在第二次运行时,如果 dist=min 距离,那么每次你有相等时你都会画线。

第二个选项是保留最小距离点列表,然后通过列表绘制线条。

编辑:更新为第二个算法添加代码:

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());
        }
    }
于 2013-08-26T13:59:59.247 回答