Skip to main content
gamedev.net
🔒 Locked

need simple algorithm

Started by bradbobak Sep 12, 2005 at 12:25 PM 4 replies 1.1k views
Original Post
bradbobak
bradbobak
hey guys..I'm looking for a simple algorithm to do the following: on in : ofs can be any int, block can be any int on out : ofs must be in the range (0..31) if ofs is -32..-1 block will be decremented by one and ofs will have 32 added to it (to get it in the range of 0..31) if ofs is -64..-33 block will be decremented by 2 and ofs will have 64 added to it..etc.. if ofs is >= 32 block will have ofs / 32 added to it and ofs will be ofs % 32.. I'm looking for a simple couple line solution.. I have it working now but it does alot of operations and I'd like to condense it if at all possible..thanks in advance -- Bradd [Edited by - bradbobak on September 12, 2005 12:47:13 PM]
Dwiel
Dwiel
possibly something like this:
int delta = ofs / 32;ofs += delta * 32;block += delta;

I'm pretty sure this is what you are asking for. Let me know if it isn't what you were looking for.
bradbobak
bradbobak
nah, i need to be able to process negative numbers too..
for e.g.

ofs block new_ofs new_block
-1 1 31 0
-35 7 29 5

all offsets between (-1..-32) add -1 to block and add 32 to itself
between (-33..-64) add -2 to block and 64 to itself
all offsets > 0 add ofs/32 to block and set itself to ofs%32
in effect, ofs will end up being between 0..31 at finish time
Enigma
Enigma
Dwiel wasn't far off:
int delta = ofs / 32;if (ofs < 0){	--delta;}ofs -= delta * 32;block += delta;

You really should use better variable names though. Even I knew what context it was used in the name ofs almost certainly tells me nothing about the value it represents.

Enigma
samv
samv
Your description is somewhat inconsistent (-32 + 64 = +32 is not in 0..31).

Ignoring that, the algorithm you describe implemented in Haskell:
f::(Integer,Integer)->(Integer,Integer)f (ofs,block)=(mod ofs 32,block+div ofs 32)

Note: The definition of div and mod are very important here. Other languages might only offer the equivalents of the similar functions quot and rem. Here is a good summary of the differences that also explains how to implement them.
_________Buggrit, millennium hand and shrimp!
bradbobak
bradbobak
wonderful..thank you.. yeah I know I should have used better names..
the names I used were the same as the ones in my code.. map [block] pixel [ofs]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.