Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say I have some grid that looks like this
 _ _ _ _ _ _ _ _ _ 
|     |     |     |
|  0  |  1  |  2  |
|_ _ _|_ _ _|_ _ _| 
|     |     |     |
|  3  |  4  |  5  |
|_ _ _|_ _ _|_ _ _| 
|     |     |     |
|  6  |  7  |  8  |
|_ _ _|_ _ _|_ _ _| 

rows 34      length in mm = 11400mm 

columns 20   length in mm = 7450mm



How do I find cell No., if I only know the x-y coordinates in lengths.
For example

X	    Y

8663	3224


is belongs to which cell no in the grid.

What I have tried:

A=Grid number
N= number columns
C= number of rows

A = (R*N) + C


This works when coordinates are in (0,0),(0,1)..... form
Posted
Updated 8-Feb-20 6:09am
v3

Quote:
Coordinates to grid box number

This is not a programming problem, it is a mathematics problem.
You need to figure out the solution, and you don't need a computer for this, even if it can help.
All you need is a sheet of paper, a pencil and your head.
- You know the size of field and number of rows ans cols.
- It is not a big deal to deduce position of each row and of each column.
- Deducing row number and column number from coordinates is not complicated from there.
- Then convert to box number is not complicated either.

The whole problem is not really complicated if approached with method.
As you didn't stated a problem and didn't showed your work, I hope you only wanted general advice, I let you solve the problem as practicing.
[Update]
Quote:
This is not giving correct results

Just look at what you did, your x is outside of range.
C++
X=8663
Y=3224

#Total No. columns
C=20

columnXY= (X/7450)
rowXY= (Y/11400)

and the formulas are wrong, you should have another look at solution 2.

Your problem is an instance of:
You know that 7450 apples cost 20, you want only 3224 apples.
How much will it cost?

This is among the most simple problem you will have to solve in programming computer.
If you can't solve this by yourself, you are in deep sh*t and should think of another job.
 
Share this answer
 
v7
Comments
User-14705882 9-Feb-20 18:29pm    
I tried following

#From XY coordinated to box no.
X=8663
Y=3224

#Total No. columns
C=20

columnXY= (X/7450)
rowXY= (Y/11400)


#box no. can be calculated as:
boxno = columnXY + (rowXY * C)
print(math.floor(boxno))

This is not giving correct results
User-14705882 10-Feb-20 2:45am    
sorry, I mean
X=3224
Y=8663

its just typo mistake.

In solution 2 if i do following

columnXY= ConvertToInteger(X / W)
rowXY= ConvertToInteger(Y / H)

it gives always zero , as W and H ( total width and height ) is always be greater than X and Y and if convert to integers , it will give zero
CPallini 10-Feb-20 6:59am    
5.
Patrice T 10-Feb-20 7:06am    
Thank you.
As @ppolymorphe mentioned it is not a big problem to calculate it.

Given is:
Field width: W
Field height: H
Columns: C
Actual coordinates: X, Y

With this you can easely calculate column and row with something like this:

columnXY= ConvertToInteger(X / W)
rowXY= ConvertToInteger(Y / H)

Finally the cell number can be calculated:
cellNumber= columnXY + rowXY * C

*) ConvertToInteger
Because I don't know Python you need to do this by yourself ;)
 
Share this answer
 
v2
Comments
User-14705882 9-Feb-20 18:32pm    
I tried following

#From XY coordinated to box no.
X=3224
Y=8663

#Total No. columns
C=20

columnXY= (X/7450)
rowXY= (Y/11400)


#box no. can be calculated as:
boxno = columnXY + (rowXY * C)
print(math.floor(boxno))

This is not giving correct results
[no name] 10-Feb-20 3:06am    
'W' and 'H' are single box width and height, not toal width and height.
Patrice T 10-Feb-20 6:15am    
not what is written in solution.
CPallini 10-Feb-20 6:58am    
"Because I don't know Python you need to do this by yourself ;)"
Python 'features' the integer division ( // operator ).
By the way, have my 5.
[no name] 10-Feb-20 7:46am    
Thank you very much for the hint and the 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900