We help IT Professionals succeed at work.

# Transferring (X,Y) to a position on an array[]

on
Medium Priority
224 Views
Right I have a bitmap which I load into an array. and that array is used to draw a height map , the code for that is shown below.

I would like to know , how to transfer x,y cordinates to an index position in an array

ie.         OneDimensionarray[<corresponding value from x,y>]

Thank You,

Mark

//VMap contains the contents of a Loaded greyscale bitmap to turn into a heightmap
// the values are then used to draw a map

for (int i= 0 ; i < (Height-1); i++)
{

glBegin(GL_TRIANGLE_STRIP);
glBindTexture(GL_TEXTURE_2D, MapTexture[0]);

Zoffset = Height/2;
Xoffset = -Width/2;
HScale = 10;

for (int j = 0;j < Width; j++)
{

glVertex3f(
j,
VMap[(i+1)*Width + (j)]/HScale,
- (i+1));

glVertex3f(
j,
VMap[(i)*Width + j]/HScale,
- i);

} // end of 2nd for loop

glEnd();
} // end of 1st for loop
Comment
Watch Question

## View Solution Only

Commented:
If OneDimensionArray is a container class of your own, you could override operator(), e. g. like that

class OneDimensionArray
{
int height;
int width;

vector<GLfloat> v;

public:
...
GLfloat operator() (int x, int y) const { return v[(x*width) + y]; }
};

If you have

OneDimensionArray VMap(Width, Height);

...

you can simplify expressions like

VMap[(i+1)*Width + (j)]/HScale

by

VMap(i+1, j)/HScale

Regards, Alex

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
sorry OneDimensionArray isn't a container class, it jsuta  one dimensional array hehe

Commented:
>>>> it jsuta  one dimensional array hehe

Why not:

class OneDimensionalArray : public vector<GLfloat>
{
int height;
int width;
public:
OneDimensionalArray(int w, int h) : height(h), width(w) {}
GLfloat operator() (int x, int y) const { return v[(x*width) + y]; }

};

That would do the job.

Regards, Alex

Commented:
Sorry misread what you put , I apologise - great answer , that sorted it,

Thank you ,

Mark
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile