I've recently had to do a lot of pixel processing and other types of processing that uses a 2d Cartesian plane. Many times I have had to get all the points directly adjacent to the one I'm working with. I usually just used to add and subtract the X and Y values, but when you need to do more than one level of adjacent points that method gets old really quickly.
So I made this function
It's average time with 10 levels on my computer is 9 Microseconds (Microsecond is one millionth of a second) so it is pretty quick, although it takes roughly 30 times longer than an integer declaration.
Function Expand(ByVal P As Point, ByVal Levels As Integer) As Point()
Dim Bound As Integer = (2 * Levels) + 1
Dim TotalPoints As Integer = Bound * Bound
Dim Points(TotalPoints - 1) As Point
Dim Origin As Point = New Point(P.X - Levels, P.Y - Levels)
For X = 0 To Bound - 1
For Y = 0 To Bound - 1
Points(Y * Bound + X) = New Point(Origin.X + X, Origin.Y + Y)
I realise that not many people will understand my explanation since my engrish is not very awesome, So I made an image!
Say you are working with the red block. you need all the immediately adjacent block locations. then you would call this function with the location of the red block say (100;100) and then it will return a array of point that has the locations of all the green blocks.