![]() |
Weird math problem
I am writing a program where I have a grid (16x16) of squares which are 25 by 25 units. Each square is separated from other squares by 4 units. The squares coordinates start at 4 in the top left corner.
My problem is that I want to be able to tell which square contains a given point. For example the point (35,8) would be in square (2,1) I know I could look thought the array of the squares and use the contains method, but I don't think that would a very good way to do this. So I have been trying to come up with an expression which could be used to figure this out without having to use a loop. I tried using the modulus operator but I'm having a hard time. Any help would be greatly appreciated. |
Re: Weird math problem
Your problem probably stems from the fact that you have space between the units. This means that you have some no-man's land that doesn't qualify. Try a two-tiered approach: reject certain ranges around certain multiples, then zero in on the remainder.
|
Re: Weird math problem
check rows first (x coordinate) , then check columns(y coordinate) using this:
(63,12): Check row: A = 63 / 25 = 2.5 = 3 (change to the next whole number) 63 - 3*4 = 51 B = 51 / 25 = 2.04 = 3 (change to the next whole number) Since A = B, then the point is in the 3rd row of the grid. Do the same for the column: 12 / 25 = 1 12 - 1*4 = 8 8 / 25 = 1 Now we know the point is in the 3rd row, 1st column. That's the third square. If A is not equal to be, then the point is out of range: (61,12) A = 61/25 = 3 61 - 3*4 = 49 out B = 49 / 25 = 2 I haven't test it this mutch, so let me know if it works. |
Re: Weird math problem
Convert coordinates to square number by computing the following pseudocode algorithm:
:
if (coord `mod` 29 >= 4)You need to find what 29-unit interval it's in, by dividing by 29, and you also need to check if the coordinate is in the part of the interval that comprises your squares, by moduloing it. |
Re: Weird math problem
Quote:
|
Re: Weird math problem
Wrote up this little php code snippet which does what you want so the algorithm should hold in whatever language it is you want to implement (no real language dependent functions or anything except for floor())
:
<?php |
| All times are GMT -5. The time now is 3:26 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC