Solved

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

Posted on 2006-11-06
4
212 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 36
Need some help with listview limit text 20 112
Getting IP address 8 89
Create a path if not exists 7 89
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

861 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