|
-
Jul 5th, 2001, 01:11 AM
#1
Thread Starter
Fanatic Member
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
-
Jul 5th, 2001, 01:38 AM
#2
Addicted Member
Something like this will work:
VB Code:
Public Sub GetXY(ByVal num As Integer, ByRef X As Integer, ByRef Y As Integer)
If num Mod 4 = 0 Then
X = num / 4
Y = 4
Else
X = Int(num / 4) + 1
Y = num Mod 4
End If
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:
Dim X As Integer
Dim Y As Integer
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
-
Jul 5th, 2001, 02:15 AM
#3
Thread Starter
Fanatic Member
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
-
Jul 5th, 2001, 02:59 AM
#4
Fanatic Member
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:
Public Type Position
Row As Long
Col As Long
End Type
Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
With GetRowCol
.Row = (lngIndex Mod lngNumColumns) + 1
.Col = lngIndex - ((.Row - 1) * lngNumColumns)
End With
End Function
Public Function GetIndex(RowCol As Position, ByVal lngNumColumns As Long) As Long
GetIndex = ((RowCol.Row - 1) * lngNumColumns) + RowCol.Col
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 
-
Jul 5th, 2001, 03:25 AM
#5
Fanatic Member
I just modified the one function to handle a non square grid. The other function is independent of being square
VB Code:
Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
With GetRowCol
.Col = lngNumColumns - (lngIndex Mod lngNumColumns)
.Row = ((lngIndex - .Col) \ lngNumColumns) + 1
End With
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 
-
Jul 5th, 2001, 03:40 PM
#6
Thread Starter
Fanatic Member
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:
Public Type Position
Row As Long
Col As Long
End Type
Public Function GetRowCol(ByVal lngIndex As Long, ByVal lngNumColumns As Long) As Position
With GetRowCol
.Row = (lngIndex Mod lngNumColumns) + 1
.Col = lngIndex - ((.Row - 1) * lngNumColumns)
End With
End Function
Public Function GetIndex(RowCol As Position, ByVal lngNumColumns As Long) As Long
GetIndex = ((RowCol.Row - 1) * lngNumColumns) + RowCol.Col
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|