Results 1 to 6 of 6

Thread: Control Index and X,Y Cooridinates

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Control Index and X,Y Cooridinates

    My program uses a 32x32 grid of image boxes in it's interface. The images are dynamically created (using a control array) at startup. I need a way to take the control index and convert it into the X and Y coordinates of the control (reletive to the grid).

    For a better example of what i'm looking for, take a look at this picture: I want to convert an number in yellow into the corrisponding "green" numbers.

    (picture won't load here)
    http://agent_153.tripod.com/grid.html

    I this example, if I passed 7 to the sub, the return values would be x=2 and y=3

  2. #2
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    Something like this will work:

    VB Code:
    1. Public Sub GetXY(ByVal num As Integer, ByRef X As Integer, ByRef Y As Integer)
    2.    
    3.     If num Mod 4 = 0 Then
    4.         X = num / 4
    5.         Y = 4
    6.     Else
    7.         X = Int(num / 4) + 1
    8.         Y = num Mod 4
    9.     End If
    10.  
    11. End Sub

    In this example, num is the number you are looking at (in your example 7) and 4 is the number of rows or columns.

    You run it as follows:

    VB Code:
    1. Dim X As Integer
    2.     Dim Y As Integer
    3.    
    4.     GetXY 7, X, Y

    Not sure if this is what you're after though.

    Hope this helps,

    Nathan.
    "The difference between genius and stupidity is that genius has its limits."

    - Albert Einstein

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    nope it doesn't work right. btw, the grid my program uses is 32x32 squares. (32x32=1024)

    everything should be based on one, not zero. the index of the square at 1,1 is 1. there is no row/coloumn 0

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Here's a general formula for that, and some things to work with it that I took from some stuff I did a long time ago:
    VB Code:
    1. Public Type Position
    2.    Row As Long
    3.    Col As Long
    4. End Type
    5.  
    6. Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
    7.    With GetRowCol
    8.       .Row = (lngIndex Mod lngNumColumns) + 1
    9.       .Col = lngIndex - ((.Row - 1) * lngNumColumns)
    10.    End With
    11. End Function
    12.  
    13. Public Function GetIndex(RowCol As Position, ByVal lngNumColumns As Long) As Long
    14.    GetIndex = ((RowCol.Row - 1) * lngNumColumns) + RowCol.Col
    15. End Function
    It's based on the number of columns. You could also base it on number of rows, and the calculations would look slightly different. This is designed for a square array/diagram.
    Last edited by Kaverin; Jul 5th, 2001 at 03:08 AM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    I just modified the one function to handle a non square grid. The other function is independent of being square
    VB Code:
    1. Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
    2.    With GetRowCol
    3.       .Col = lngNumColumns - (lngIndex Mod lngNumColumns)
    4.       .Row = ((lngIndex - .Col) \ lngNumColumns) + 1
    5.    End With
    6. End Function
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    Originally posted by Kaverin
    Here's a general formula for that, and some things to work with it that I took from some stuff I did a long time ago:
    VB Code:
    1. Public Type Position
    2.    Row As Long
    3.    Col As Long
    4. End Type
    5.  
    6. Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
    7.    With GetRowCol
    8.       .Row = (lngIndex Mod lngNumColumns) + 1
    9.       .Col = lngIndex - ((.Row - 1) * lngNumColumns)
    10.    End With
    11. End Function
    12.  
    13. Public Function GetIndex(RowCol As Position, ByVal lngNumColumns As Long) As Long
    14.    GetIndex = ((RowCol.Row - 1) * lngNumColumns) + RowCol.Col
    15. End Function
    It's based on the number of columns. You could also base it on number of rows, and the calculations would look slightly different. This is designed for a square array/diagram.
    Thanks. This code worked. I had to modify it to fit in the place of the previous (but non-working) functions. I had to get rid of the Position type in favor of a sub the returns the values in the parameters. This is because many parts of the code in my program were written to work that way.

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