Multi-Dimensional Vectors

Pages: 1234
Jul 19, 2010 at 10:42pm
Well, that's what you'd also have to do if you used an array... What does your program do? How do you want to use this multi-dimensional array in your program? I don't suppose you enter the values for 9000000 elements yourself... How are these calculated?
Jul 19, 2010 at 10:49pm
Well, not every element will have a value. Only a select few hundred will have values. The user of the program will enter values for the points that he/she wishes to be marked as a point of interest on the map and then the elements with values will be marked as a yellow dot on the map. The rest will be green.

I plan on integrating SDL into this.
Jul 19, 2010 at 11:19pm
Sounds to me like you would be better off just storing the location of the points with a value to me:

1
2
3
4
5
6
struct coord
{
    int x, y;
};

std::vector<coord> locations;


That would be far more efficient.

EDIT: Fixed bug.
Last edited on Jul 19, 2010 at 11:27pm
Jul 19, 2010 at 11:21pm
Well if I don't have all of the coordinates in an array, how will I display the array onto a map?
Jul 19, 2010 at 11:25pm
Well you clear the background to yellow and then for every coordinate in your location array you plot a green dot:

1
2
3
4
5
6
7

clear(screen, yellow);

for(size_t i(0); i < locations.size(); ++i)
{
    plot(screen, locations[i].x, locations[i].y, green);
}
Last edited on Jul 19, 2010 at 11:26pm
Jul 19, 2010 at 11:29pm
But without the array, how will it know where each location is? I forgot to tell you, the map is a diamond. A very squished down diamond. Like this:


-----------^

<-------------------->

-----------^

not a square: [|]
Last edited on Jul 19, 2010 at 11:29pm
Jul 19, 2010 at 11:44pm
Wander wrote:
I forgot to tell you, the map is a diamond.

How does that make Galik's suggestion not applicable?
Jul 19, 2010 at 11:47pm
Wander wrote:
how will it know where each location is?


That is what you stored in your vector<>. The location of each point that is green.
Jul 19, 2010 at 11:49pm
@r0shi: It doesn't... I didn't even know Galik asked a question.

@galik: so would i need two vectors? one for the green points (the entire map) and one for yellow points (yellow gets overlayed over green)?


Dang, I keep forgetting to look at the usernames. lol

(P.S. Dang I suck at this whole programming thing.)

EDIT: Edited to fit with Galiks latest post.
Last edited on Jul 19, 2010 at 11:53pm
Jul 19, 2010 at 11:51pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct coord
{
    int x, y;
    coord(int x, int y): x(x),y(y){}
};

int main()
{
    std::vector<coord> location;
    
    /// store populated the locations in the vector
    location.push_back(coord(10, 35));
    location.push_back(coord(5, 72));
    location.push_back(coord(109, 2));
    location.push_back(coord(60, 1));
}

Jul 19, 2010 at 11:54pm
Wander wrote:
how will the program know where to put the yellow dots if you don't have an array telling it where each and every coordinate is at


If its a regular shape then you can do that programmatically. Or use a shape drawing library.
Jul 19, 2010 at 11:56pm
@ galik: wut is struct, and i guess i oughta learn about those.... wait a minute,, Shape Drawing Library (SDL). is that wut SDL stands for? the two dimensional graphics library.
Jul 19, 2010 at 11:57pm
If you do it with the huge array[][] you have exactly the same problem of how to fill in the correct locations with the correct colour. Basically your huge array[][] is mirroring the screen buffer. So its rather redundant really. Might as well just use the screen buffer and keep a note of the important locations.
Jul 20, 2010 at 12:00am
Won't using the screen buffer color the entire screen green? instead of just the diamond?
Jul 20, 2010 at 12:01am
SDL = Simple DirectMedia Layer. There is a shape drawing library for SDL: Called SDL_gfx.
Jul 20, 2010 at 12:03am
Your screen buffer is no different from your original array[][] idea. You have to work out how to fill each location with the right colour. You can clear the screen to black, then draw a green diamond and finally plot your array of user points in yellow.
Last edited on Jul 20, 2010 at 12:03am
Jul 20, 2010 at 12:05am
OKAY!!! That my friend sounds like the best idea! Now, all i gotta do is learn SDL... :) LOL

Thanks r0shi and Galik!!!!! :D

Any pointer?
Jul 20, 2010 at 12:14am
Jul 20, 2010 at 12:16am
Thank you!! :D Ill check it out.

I feel like I have used up all of you guy's day. haha. If you're feeling up to it, would you mind checking out my other help topic on this forum. Feel free to opt out if you dont feel up to it.
http://www.cplusplus.com/forum/beginner/26322/
Jul 20, 2010 at 12:17am

You can have a rectangular coordinate system for your points and still translate them to a diamond shaped one.

Example:

This creates a few random points:
1
2
3
  const int w=256,h=256;
  vector<point2d> points;
  for (int i=0;i<30;i++)points.push_back(point2d(random(w),random(h)));


Now this plots them to a square map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  BitmapReference bRef(new TBitmap(w,h,clBlue));
  for(int y=0;y<h;y++)
  {
    for(int x=0;x<w;x++)
    {
      bRef->setPixel(x,y,RGB(y,y,y));
    }
  }
  foreach(p,points)
  {
    const int tx=p.x;
    const int ty=p.y;
    const int pd=2;
    for (int y=0;y<pd;y++)for (int x=0;x<pd;x++)bRef->setPixel(tx+x,ty+y,clYellow);
  }


and results in this: http://94.23.22.190/etc/square.png

With some transformation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  BitmapReference bRef(new TBitmap(w*2,h*2,clBlue));
  for(int y=0;y<h;y++)
  {
    for(int x=0;x<w;x++)
    {
      bRef->setPixel(x+y,h-x+y,RGB(y,y,y));
      bRef->setPixel(x+y+1,h-x+y,RGB(y,y,y));
    }
  }
  foreach(p,points)
  {
    const int tx=p.x+p.y;
    const int ty=h-p.x+p.y;
    const int pd=2;
    for (int y=0;y<pd;y++)for (int x=0;x<pd;x++)bRef->setPixel(tx+x,ty+y,clYellow);
  }


results in this (same points plotted): http://94.23.22.190/etc/diamond.png
Pages: 1234