Reversing an ordinal assignment

Recommended Posts

This problem is more of a mathsy one that a forge one but it's been bugging me for so long now that I think I am incapable of noticing the solution

Let's say I have an integer set of coordinates, each ranging from 0 to some independent finite limit (which may be different for each. This can be thought of as an N dimensional grid with coordinates

`(i, j, k, ...)`

and lengths

`(I, J, K, ...)`

. In this case each coordinate that can be occupied/intersect of the grid can be counted with an ordinal assigned by

`o=i+j*I+k*I*J+...`

. this is useful wen trying to assign a metadata value or some such. The problem I'm having is figuring out a way of reversing this operation to get the original

`(i,j,k,...)`

given the ordinal and

`(I,J,K,...)`

Share on other sites

Thats a dense, yet slow way of indexing things typically people just set bit ranges for certian things:

o = (x << 0) | (y << || (z << 16)

x = (o >> 0) & 7

y = (o >> & 7

z = (o >> 16) & 7

But you way it's similiar but can't easily do the shifts.

So:

o = x+y*(X)+z*(X*Y)

x = o % X

y = floor((o % (X*Y))/X)

z = floor((o % (X*Y*Z))/(X*Y))

Complicated and ugly, but should do what you want.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Share on other sites

Thanks, that's been bugging be for ages. think I'd just looked at it for too long to notice as I said. Did initially think of just packing like you say but it wound up being important that they remain in order and without gaps (various rng operations etc...)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements

×

• Activity

×
• Create New...