日期:2014-05-18  浏览次数:20984 次

C#遍历周围的点
我有一个二维数组arr[i,j]
想找其中离某点周围最近的点,判断是否满足特定条件,不满足的话继续遍历下一圈离他最近的点
就是一直以矩形的形式遍历下去
请问可以怎么实现?

------解决方案--------------------
用linq。

C# code
/*
0 0 0 1
0 1 0 0
0 0 1 0
0 0 0 0
*/
class PointData
{
    public int x { get; set; }
    public int y { get; set; }
    public int data { get; set; }
}

...

var data = new List<PointData>
{
    new PointData() { x = 0, y = 0, data = 0 },
    new PointData() { x = 1, y = 0, data = 0 },
    new PointData() { x = 2, y = 0, data = 0 },
    new PointData() { x = 3, y = 0, data = 1 },
    new PointData() { x = 0, y = 1, data = 0 },
    new PointData() { x = 1, y = 1, data = 1 },
    new PointData() { x = 2, y = 1, data = 0 },
    new PointData() { x = 3, y = 1, data = 0 },
    new PointData() { x = 0, y = 2, data = 0 },
    new PointData() { x = 1, y = 2, data = 0 },
    new PointData() { x = 2, y = 2, data = 1 },
    new PointData() { x = 3, y = 2, data = 0 },
    new PointData() { x = 0, y = 3, data = 0 },
    new PointData() { x = 1, y = 3, data = 0 },
    new PointData() { x = 2, y = 3, data = 0 },
    new PointData() { x = 3, y = 3, data = 0 }
};
//寻找距离3,3最近的点
var result = data.OrderBy(x => (x.x - 3) * (x.x - 3) + (x.y - 3) * (x.y - 3)).Where(x => x.data == 1).First();
Console.WriteLine("result is {0},{1}.", result.x, result.y);