Hooker Posted March 1, 2007 Share Posted March 1, 2007 I'm trying to write a little script to calculate distances in a grid.. basicaly i have a grid 10,000 by 10,000 large. In that grid quite randomly i have data on certain coordinates (i.e 395 | 900 etc), each coordinate on the grid has its own page, people take over their own part of the grid (basicaly there own place on a map) and fill information in on their grid, what i would like to do is calculate what other coordinates within 10 grid points in all directions are currently "inhabited", like that grid referrances neighbours. I hope that makes sense, its kind of confusing for me to explain but if you need any more info just ask. *edit* Hopefully this will help: Quote Link to comment Share on other sites More sharing options...
btherl Posted March 1, 2007 Share Posted March 1, 2007 Have you decided yet how you will store the data? Quote Link to comment Share on other sites More sharing options...
Hooker Posted March 1, 2007 Author Share Posted March 1, 2007 in a mysql table, basicaly the table will be set out like so (sorry i didn't include this sooner ): UID | X_axis | Y_axis | Owner UID = unique ID X_axis = the x axis on the grid Y_axis = the y axis on the grid Owner = the owners UID then the info held on the coordinates will be held in a different table and so will the info on the owner Quote Link to comment Share on other sites More sharing options...
anatak Posted March 1, 2007 Share Posted March 1, 2007 Hello, This is not too difficult to solve with a query. You have to query your database to select all the grids where X_axis is between 10 higher and 10 lower than the grid point you use as a reference and do the same thing for the Y_axis. Of course that will always give you 441 rows (21 X 21) so you can limit it with a where clause to only get the results with owners. Is the Owner set to NULL or some other value when a grid place is not yet owned ? how to do this practically. get the grid coordinates from the gridpoint you are using to check if there are any neighbours within a 10 point. example grid is X_axis=15 and Y_axis =20 put those values in a variable so you can use it in the query. so your query would be something like Select * from TABLENAME WHERE X_axis > $x_axis_variable-10 AND X_axis < $x_axis_variable+10 AND Y_axis > $y_axis_variable-10 AND Y_axis < $y_axis_variable+10 AND Owner <> NULL; Here are some potential problems. you have to take in account that the X and Y position maybe lower than 10 for example gridpoint x=5 and y=3 That will give you negative values in your query but that should not be a real problem unless you have a numerical datatype with unsigned parameter look here for more info http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html I am sure you can change this X_axis > $x_axis_variable-10 AND X_axis < $x_axis_variable+10 AND into something like this $x_axis_variable+10 > X_axis > $x_axis_variable-10 AND but I am not sure about syntax hope that will get you started kind regards anatak Quote Link to comment Share on other sites More sharing options...
Hooker Posted March 1, 2007 Author Share Posted March 1, 2007 thats exactly what i was looking to find out, it's been a long night and i just could'nt get my head around a simple way to do it, thanks! Quote Link to comment Share on other sites More sharing options...
anatak Posted March 1, 2007 Share Posted March 1, 2007 I have a stupid question. What is your application if I can ask ? Some kind of game ? glad you could get it to work. anatak Quote Link to comment Share on other sites More sharing options...
Hooker Posted March 1, 2007 Author Share Posted March 1, 2007 Yes, a game.. still in planning, peicing things together before writing it all out but i'm trying to make it as graphical as possible (including maps showing where players are etc). This part would basicaly be for how far a person can see as they move around and start villages etc Nothing huge, just something to keep myself busy Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 1, 2007 Share Posted March 1, 2007 Yes, a game.. still in planning, peicing things together before writing it all out but i'm trying to make it as graphical as possible (including maps showing where players are etc). This part would basicaly be for how far a person can see as they move around and start villages etc Nothing huge, just something to keep myself busy good idea. i once heard, 'don't document your code; code your documentation.' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.