Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-06
4
Medium Priority
?
216 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Paladin_VB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 17883246
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);
    loadBitmap(VMAP, ...);

    ...

you can simplify expressions like

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

by

    VMap(i+1, j)/HScale


Regards, Alex

   
0
 
LVL 1

Author Comment

by:Paladin_VB
ID: 17883490
sorry OneDimensionArray isn't a container class, it jsuta  one dimensional array hehe
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17883755
>>>> 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
0
 
LVL 1

Author Comment

by:Paladin_VB
ID: 17884030
Sorry misread what you put , I apologise - great answer , that sorted it,

Thank you ,

Mark
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question