Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 179
Eclipse IDE - Cannot copy/paste from console output 8 127
max float value 3 25
Installshield for Embarcadero EX 10.1 Berlin 4 18
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

910 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

23 Experts available now in Live!

Get 1:1 Help Now