|
View previous topic :: View next topic |
Author |
Message |
| ChPicard
| Joined: 12 Mar 2008 | Posts: 82 | : | Location: Montreal, Canada | Items |
|
Posted: Tue Sep 08, 2009 6:10 pm Post subject: Box number |
|
|
HI
What is the formula to calculate the box number (b=1; b<10;) from the given's position (x=0; x<81;)?
Thank you |
|
Back to top |
|
|
| humble_programmer
| Joined: 27 Jun 2006 | Posts: 69 | : | Location: Colorado Springs, USA | Items |
|
Posted: Tue Sep 08, 2009 9:20 pm Post subject: |
|
|
Try this, where index is the square index in the range [0..80]
Code: |
iBox = ((( index / 9) / 3) * 3) + (( index % 9) / 3);
|
The result in iBox will be in the range [0..8], so you may have to add 1; also, don't simplify the algebra, as it relies on the truncation of integers. _________________ Cheers!
Humble Programmer
,,,^..^,,,
www.humble-programmer.com |
|
Back to top |
|
|
| Lunatic
| Joined: 11 Mar 2007 | Posts: 166 | : | Location: Ghent - Belgium | Items |
|
Posted: Wed Sep 09, 2009 3:06 pm Post subject: |
|
|
humble_programmer wrote: | Try this, where index is the square index in the range [0..80]
Code: |
iBox = ((( index / 9) / 3) * 3) + (( index % 9) / 3);
|
The result in iBox will be in the range [0..8], so you may have to add 1; also, don't simplify the algebra, as it relies on the truncation of integers. |
However, you can simplify it...
Code: |
iBox = (( index / 27) * 3) + (( index % 9) / 3);
|
In my program i use some global arrays, and the one for this purpose is called iCell2Box[0 to 80]
Initiation at program start:
Code: |
For (i=0;i<81;i++) iCell2Box(i) = ((i / 27) * 3) + ((i % 9) / 3);
|
Wherever you want to know in which box a cell (index) is in, just use:
Code: |
iBox = iCell2Box[index];
|
_________________ Marc
~~~<><~~~<><~~~<><~~~<><~~~ |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
Igloo Theme Version 1.0 :: Created By: Andrew Charron
|