在每个像素的位图中,我试图获得它的第一、第二、第三......级别的邻居,直到位图结束,但我的解决方案有点慢,所以让我知道你们是否有更好的算法或方法:
private IEnumerable<Point> getNeightboorsOfLevel(int level, Point startPos, Point[,] bitMap)
{
var maxX = bitMap.GetLength(0);
var maxY = bitMap.GetLength(1);
if (level > Math.Max(maxX, maxY)) yield break;
int startXpos = startPos.X - level;
int startYpos = startPos.Y - level;
int sizeXY = level * 2;
var plannedTour = new Rectangle(startXpos, startYpos, sizeXY, sizeXY);
var tourBoundaries = new Rectangle(0, 0, maxX, maxY);
for(int UpTour = plannedTour.X; UpTour<plannedTour.Width; UpTour++)
if (tourBoundaries.Contains(UpTour,plannedTour.Y))
yield return bitMap[UpTour,plannedTour.Y];
for(int RightTour = plannedTour.Y; RightTour<plannedTour.Height;RightTour++)
if (tourBoundaries.Contains(plannedTour.Right,RightTour))
yield return bitMap[plannedTour.Right,RightTour];
for(int DownTour = plannedTour.X; DownTour<plannedTour.Width;DownTour++)
if (tourBoundaries.Contains(DownTour,plannedTour.Bottom))
yield return bitMap[DownTour,plannedTour.Bottom];
for (int LeftTour = plannedTour.Y; LeftTour < plannedTour.Height; LeftTour++)
if (tourBoundaries.Contains(plannedTour.X,LeftTour))
yield return bitMap[plannedTour.X,LeftTour];
}