[RESOLVED] locating position in a grid
I am looking for some help or code for the following (it must have been done many times before but I don't quite know how to word a search):
I have a series of values with x,y coordinates;
23,5
31,19
47,7
etc
and a grid which is from 20-50 in steps of 10 along x and 0-20 in steps of 2 along y.
I want to find which cell each point is in (and the center of that cell).
Eg the first point falls in cell x1, y3 with center at x=25, y=5
Any help or suggestions where to look would be greatly appreciated
Thanks
Re: locating position in a grid
This seems to be working. About the mathematical calculation: i'm not sure if it is efficient enough, maybe it's possible to do the same with simpler calculations or applying some given formula, but anyways, this is what i've came to..
Add a CommandButton and this code to your Form. For testing change the values where it says "Example", now it's x=23,y=5.
Code:
Private Type TypeCoordAxis
xFrom As Single
xTo As Single
xStep As Single
yFrom As Single
yTo As Single
yStep As Single
End Type
Private Type TypePoint
x As Single
y As Single
End Type
Private Sub Command1_Click()
Dim udtAxis As TypeCoordAxis, udtPoint As TypePoint
Dim lCell As TypePoint, lCellCenter As TypePoint
'Set Axis for X
udtAxis.xFrom = 20
udtAxis.xTo = 50
udtAxis.xStep = 10
'Set Axis for Y
udtAxis.yFrom = 0
udtAxis.yTo = 20
udtAxis.yStep = 2
'Example
udtPoint.x = 23
udtPoint.y = 5
GetCellAndCenter udtAxis, udtPoint, lCell, lCellCenter
MsgBox "CELL NUMBER: (" & lCell.x & ", " & lCell.y & ")"
MsgBox "CELL CENTER: (" & lCellCenter.x & ", " & lCellCenter.y & ")"
End Sub
Private Sub GetCellAndCenter(pAxis As TypeCoordAxis, pPoint As TypePoint, _
pCell As TypePoint, pCellCenter As TypePoint)
Dim xUnits As Long, yUnits As Long
Dim xRange As Single, yRange As Single
xRange = pAxis.xTo - pAxis.xFrom
yRange = pAxis.yTo - pAxis.yFrom
xUnits = xRange / pAxis.xStep
yUnits = yRange / pAxis.yStep
pCell.x = Fix((pPoint.x - pAxis.xFrom) / xRange * xUnits + 1)
pCell.y = Fix((pPoint.y - pAxis.yFrom) / yRange * yUnits + 1)
pCellCenter.x = pAxis.xFrom + pCell.x * pAxis.xStep - pAxis.xStep / 2
pCellCenter.y = pAxis.yFrom + pCell.y * pAxis.yStep - pAxis.yStep / 2
End Sub
Re: locating position in a grid
Thanks a lot jcis - works brilliantly. I don't realy understand how it works but, hey, it works! Even works when the point lies out side the given limits of x and y.
Thanks again
Re: locating position in a grid
You're welcome, and welcome to the Forums. If your problem is resolved remember to "Mark Thread Resolved" you'll find this option in menu "Thread Tools"