Solved

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

Posted on 2006-11-06
4
214 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

738 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