Help with MAP LOGIC

Cableguy

Expert
Licensed User
I'm trying to make sense of some map Logic...
Imagine a map, divided in cells, extending from -250 to 250, including 0,in both x and y axes...
Its easy obtaining the total amount of squares.
(250*250+1) will give me the number of Lines and of collumns, so the total amout of cell is (rows*Collumns)....
In this case:
501*501=251001

For some reason, I HAVE TO USE THIS IN A SEARCH, but Then it gets complicated....
How do I get the Coordinates for a specific cell, trough its number...
Imagine I need to convert cell 36862 to X:Y , HOW????
 

Cableguy

Expert
Licensed User
Thanks, but it shed litle light for what i need..
you forgot that I have negative values, referenced to a 0 colun and a 0 row, so my map look a litle like this:

-3 -2 -1 0 1 2 3
+--+--+--+--+--+--+--+
3 |01|02|03|04|05|06|07|
+--+--+--+--+--+--+--+
2 |08|09|10|11|12|13|14|
+--+--+--+--+--+--+--+-
1 |15|16|17|18|19|20|21|
+--+--+--+--+--+--+--+
0 |22|23|24|25|26|27|28|
+--+--+--+--+--+--+--+
-1 |29|30|31|32|33|34|35|
+--+--+--+--+--+--+--+
-2 |36|37|38|39|40|41|42|
+--+--+--+--+--+--+--+
-3 |44|45|46|47|48|49|50|
+--+--+--+--+--+--+--+

Only a LOT larger...
 
Last edited:

klaus

Expert
Licensed User
No problem.

The previous code
B4X:
index=36862
row=Int(index/501)
col=index Mod 501
becomes

B4X:
rowo=-177     ' row
colo=39       ' col
row0=-250     ' first row number
col0=-250     ' first col number 
rownb=501     ' number of rows
colnb=501     ' number of cols
index=(rowo-row0)*colnb+col0)  ' =36862  cell index

row=Int(index/501)+row0   ' calculated row
col=index Mod 501+col0    ' calculated col
Best regards
 

Cableguy

Expert
Licensed User
Yes, it works fine, but could you explain a bit your logic...
How the actual calculations works....
 

RandomCoder

Well-Known Member
Licensed User
Klaus you must be a math-magician!
I like Cableguy couldn't see how your code works, but the explanation was great and you made it look so simple. Personally I hate maths and always struggle with it :)

Regards,
RandomCoder
 
Top