Solved

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

Posted on 2006-11-06
4
208 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
  • 2
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
Comment Utility
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
Comment Utility
sorry OneDimensionArray isn't a container class, it jsuta  one dimensional array hehe
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
Comment Utility
Sorry misread what you put , I apologise - great answer , that sorted it,

Thank you ,

Mark
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now