通过查找邻近点来连接点的集合 [英] Connect collection of points by finding neighborhood points
问题描述
我有一个swing应用程序,其中有一个图像上的点的ArrayList。
现在我想连接这些点,使每个点连接到最近的邻居。
像这样:
所以我开始像这样:
Minuatiae.java
public class细节{
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;
$ / code>
Manager.java
列表<细节> minuatiaePoints = new ArrayList<细节>(细节); (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 ++)//< - 你在这里有i ++!
{
if(i == j){
continue;
}
Minutiae testPt = minuatiaePoints.get(j);
double dist = Math.hypot(mPoint.getX() - testPt.getX(),mPoint.getY() - testPt.getY());
if(dist {
minDist = dist;
minPoint = testPt;
g2D.drawLine(mPoint.getX(),mPoint.getY(),minPoint.getX(),minPoint.getY());
}
但它只连接到一个最近点。
任何人都可以帮我解决这个问题吗?
任何链接或示例代码都将非常感谢。
您有两种选择:
$ b
一旦您拥有最小距离,再次进入您的列表并连接所有最小距离=您的最小距离(因此您只需要找到最小距离)。第二次运行时,如果dist =最小距离,那么每当你平等时画线。
第二个选项是保留最小距离点列表然后通过列表画线。
编辑:更新为第二个algortihm添加代码:
pre> 列表与LT;细节点> minuatiaePoints = new ArrayList<细节>(细节); (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);
列出<细节> minDistPoints = new ArrayList< Minutiae>();
for(int j = 0; j< minuatiaePoints.size(); j ++)//< - 你在这里有i ++!
{
if(i == j){
continue;
}
Minutiae testPt = minuatiaePoints.get(j);
double dist = Math.hypot(mPoint.getX() - testPt.getX(),mPoint.getY() - testPt.getY());
if(dist
minDist = dist;
minDistPoints = new ArrayList<细节>();
minDistPoints.add(testPt);
} else if(dist = minDist){
minDistPoints.add(testPt); (Minutae p:minDistPoints){
}
}
(m.getLine(mPoint.getX(),mPoint.getY(),p.getX ),p.getY());
}
}
I have a swing application in which I have a ArrayList of some points on the image. Now I want to connect those points such that each point is connected to its nearest neighbors.
Like this:
So i started like this:
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());
}
But it connects to only one nearest point.
Could anyone help me with this? Any link or example code will be very grateful.
You have two options:
Once you have the minimum distance, have another go at your list and connect all whose minimum distance = your min distance (therefore you only need to find the min. distance). And on the second run if dist=min distance, then you draw the line every time you have the equality.
The second option is to keep a list of minimum distance points and then go through the list to draw the lines.
EDIT: updated to add code for the second 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());
}
}
这篇关于通过查找邻近点来连接点的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!