如何创建接受一个点和所有点列表的东西,并返回足够接近原始点或足够接近点的点列表。
如果你仍然无法理解我的照片:
我试过了:
int range = 6;
public List<IntPoint> getClosePoints(ref Dictionary<IntPoint,bool> allPoints,IntPoint startingPoint) {
List<IntPoint> closePoints = new List<IntPoint>();
closePoints.Add(startingPoint);
bool gotBigger = true;
while (gotBigger) {
gotBigger = false;
foreach (IntPoint proven in closePoints.ToList()) {
foreach (IntPoint possible in allPoints.Keys.ToList()) {
if (isInRange(proven,possible,range) && !allPoints[possible]) {
gotBigger = true;
closePoints.Add(possible);
allPoints[possible] = true;
}
}
}
}
return closePoints;
}
public bool isInRange(IntPoint A, IntPoint B, int range){
if(A.DistanceTo(B) < range)
return true;
return false;
}
(IntPoint
与Point
类似,来自AForge
,所有点都有bool
值false
但这使我的程序超级滞后,因为它被称为循环的千倍。 :/(而且它现在似乎也没有用)
答案 0 :(得分:3)
试试这个:
public IEnumerable<Point> GetPoints(Point origin, IEnumerable<Point> points, int distance)
{
var result = new HashSet<Point>();
var found = new Queue<Point>();
found.Enqueue(origin)
while(found.Count > 0)
{
var current = found.Dequeue();
var candidates = points
.Where(p => !result.Contains(p) &&
p.Distance(current) <= distance);
foreach(var p in candidates)
{
result.Add(p);
found.Enqueue(p)
}
}
return result;
}
我认为这是直截了当的,无论如何HashSet功能是它可以判断它是否包含近O(1)中的项目。
答案 1 :(得分:2)
这是一个群集问题。只需简单的步骤,