Nearest neighbor using polar coordinates
Is there any method to order a set of (x,y) points with respect the origin depending on both distance and angle?
In my problem I have a set of points in the plane, all spread around the center (0,0). These represent some data feature, and nearest points represent similar data. Then I need to order them and so I itarate until all points are considered, taking the NN with respect to the origin (0,0) considering just the distance between points. These points are very closer, and although similar data are closer one each other, some different data are represented far from each other but in the same circumference around the origin (this means that they have the same distance with respect the center), thus often I take points at almost the same distance (the same radius) but that lie in the opposite sides of the (imaginary) circle around the center.
I want to use the polar coordiantes to order these points. So I want to consider first the radial distance, and then the angle. I can compute these tho measures for all the points, but I don't know how to obtain an unique index to order them.
This is the code I use: in avPositions I have a set of pre-computed (x,y) positions of a grid, for each available position I have to select the nearest point in DATA.
for(var i=0;i<avPositions.length; i++)
{
res = posed.indexOf(false);
if(res<0) break;
x = avPositions[i][0];
y = avPositions[i][1];
dist = null;
dist = DATA.map(function(obj) { x1 = obj[0];
y1 = obj[1];
S = (x-x1)*(x-x1) + (y-y1)*(y-y1);
return Math.sqrt(S);
});
nearestIndex = dist.indexOf(Math.min.apply(Math, dist));
while(posed[nearestIndex])
{
dist[nearestIndex] = 99999999;
nearestIndex = dist.indexOf(Math.min.apply(Math, dist));
}
if(nearestIndex < 0) break;
posed[nearestIndex] = true;
DATAgrid[nearestIndex][0] = x;
DATAgrid[nearestIndex][1] = y;
}
链接地址: http://www.djcxy.com/p/85010.html
上一篇: 通过鼠标移动沿圆形路径移动一个点(python / pygame)
下一篇: 使用极坐标的最近邻居