Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Coder's Corner Lounge (http://www.programmingforums.org/forum11.html)
-   -   Weird math problem (http://www.programmingforums.org/showthread.php?t=14191)

Wizard1988 Oct 19th, 2007 11:04 PM

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.

DaWei Oct 19th, 2007 11:45 PM

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.

OpenLoop Oct 20th, 2007 11:28 AM

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.

SamReidHughes Oct 20th, 2007 1:33 PM

Re: Weird math problem
 
Convert coordinates to square number by computing the following pseudocode algorithm:

:

if (coord `mod` 29 >= 4)
  then (1 + coord `div` 29)
      -- added one because your unholy square numbers start at 1, not 0.
  else (error "we're in between squares")


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.

WaltP Oct 20th, 2007 2:08 PM

Re: Weird math problem
 
Quote:

Originally Posted by Wizard1988 (Post 135392)
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.

So each square is effectively 29x29, with the lowest 4 units unused. A simple equation can be designed to test for correct placement, using the % operator.

grimpirate Oct 20th, 2007 3:22 PM

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
$borderWidth = 4;
$squareWidth = 25;
$locationX = 35;
$locationY = 8;
$outerBorderOffset = $borderWidth / 2;
$locationX -= $outerBorderOffset;
$locationY -= $outerBorderOffset;
$squareLocationX = $locationX % ($squareWidth + $borderWidth);
$squareLocationY = $locationY % ($squareWidth + $borderWidth);
if($squareLocationX >= $outerBorderOffset && $squareLocationX < ($squareWidth + $outerBorderOffset)){
        $squareLocationX = floor($locationX / ($squareWidth + $borderWidth));
        $squareLocationX++;
}else{
        $squareLocationX = 'border';
}
if($squareLocationY >= $outerBorderOffset && $squareLocationY < ($squareWidth + $outerBorderOffset)){
        $squareLocationY = floor($locationY / ($squareWidth + $borderWidth));
        $squareLocationY++;
}else{
        $squareLocationY = 'border';
}
echo '(' . $squareLocationX . ',' .$squareLocationY . ')';
?>



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