Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:43:29+00:00 2026-06-10T04:43:29+00:00

I have a few values which are offsets to a multidimensional array , and

  • 0

I have a few values which are offsets to a multidimensional array , and look like this :

static const int TILE_SIZE = 32;
int Offset2D = (y * TILE_SIZE) + (x * TILE_SIZE);
int Offset3D = (y * TILE_SIZE) + (x * TILE_SIZE) + (z * TILE_SIZE);

Now what i would like to do is to convert an offset to x,y,z pair , like so :

void ConvertBack(int offset,int size,int& x,int& y,int& z)
{
    //What's wrong with this code ?
    x = offset / size;
    y = offset % size;
    z = ??; //How to get Z?
}

or

//Get back offsets from any dimension ?

void ConvertBackComplex(unsigned int offset,int size,int* vector,int len)
{
    for (int i = 0;i < len;i++)
    {
        vector[i] = offset ?... ?
    }
}

…So far all of my attempts have failed….So i would really welcome any help!…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:43:30+00:00Added an answer on June 10, 2026 at 4:43 am

    First of all I think you indexing system is a bit off. The way you have things arranged different values of x, y, and z can give the same offset. So, first of all, assuming that TILE_SIZE is how many cells of the array store the data for a given point:

    myArray = new arr[xSize*ySize*zSize*TILESIZE]
    int offset2D = (x*ySize*zSize + y*zSize)*TILE_SIZE;
    int offset3D = (x*ySize*zSize + y*zSize + z)*TILE_SIZE;
    

    To get x,y,z back from the offset one simply does the following:

    temp = offset/TILE_SIZE;
    x = temp/(ySize*zSize);
    y = (temp%(ySize*zSize))/zSize;
    z = (temp%(ySize*zSize))%zSize;
    

    For multiple dimensions:

    temp = offset/TILE_SIZE;
    sizeProduct = 1;
    for(int k=1; k<numDims; ++k)
    {
        sizeProduct*=size[k];
    }
    for(int i=0; i<numDims; ++i)
    {
        vector[i]=temp/sizeProduct;
        temp = temp % sizeProduct;
        if((i+1)<numDims)
        {
            sizeProduct/=sizes[i+1];
        }
    }
    

    To calculate array sizes in multiple dimensions:

    int arraySize = TILE_SIZE;
    for(int i=0; i<numDims; ++i)
    {
        arraySize*=sizes[i];
    }
    

    To calculate array indices in multiple dimensions (assuming vector is your array of coordinates):

    int index =0;
    sizeProduct = 1;
    for(int k=1; k<numDims; ++k)
    {
        sizeProduct*=size[k];
    }
    for(int i=0; i<numDims; ++i)
    {
        index+=sizeProduct*vector[i];
        if((i+1)<numDims)
        {
            sizeProduct/=sizes[i+1];
        }
    }
    index*=TILE_SIZE;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few NSDate objects which contain values compliant to this format yyy-MM-dd'T'HH:mm:ss.SSS When
I have a few types that are like this // a value that is
I have a few checkboxes values of which are posted to action, and when
I have one spinner in which few values are there from strings.xml and I
I have a ini file which contains some sample values like: [Section1] Value1 =
I have a very simple mapping which looks like this (I streamlined the example
I have few different applications among which I'd like to share a C# enum.
I have an Array var cars = [2,3,..] which holds a few integers. I've
I have a table which consists of a few columns, including a Key Value
I have a SelectItem object which has a few items. I set a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.