PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Expand Function-VBForums
Results 1 to 1 of 1

Thread: Expand Function

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    South Africa

    Expand Function

    Hi all,
    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 Code:
    1. Function Expand(ByVal P As Point, ByVal Levels As Integer) As Point()
    2.         Dim Bound As Integer = (2 * Levels) + 1
    3.         Dim TotalPoints As Integer = Bound * Bound
    4.         Dim Points(TotalPoints - 1) As Point
    5.         Dim Origin As Point = New Point(P.X - Levels, P.Y - Levels)
    6.         For X = 0 To Bound - 1
    7.             For Y = 0 To Bound - 1
    8.                 Points(Y * Bound + X) = New Point(Origin.X + X, Origin.Y + Y)
    9.             Next
    10.         Next
    11.         Return Points
    12.     End 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.

    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.
    Last edited by BlindSniper; Dec 6th, 2011 at 05:20 PM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width