# What is the best way to store X and Y coords

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to store x and y coords in a way that there link togther so if you remove x it removes the linking y. I was thinck and array but it dont look like a 2D array I can accsess both set independitly from one another at the same time being able to destroy a set when need to. I am thincking mabey a link list but I am still not sure it will do what I want. what would be the best way to store 100 x and y coordinates or intgers that stay in pairs but are able to be acsessed indviduely and delteted in pairs?

##### Share on other sites
struct Coord{   int x;   int y;}// Access coordinates by pos[0].x or pos[0].y, etcCoord pos[100];// Can use a linked list or vector to store the set of Coords as well// i.e. vector<Coord> pos(100);// If you remove an element from the vector, it deletes both the x and y position

Is this what you're asking for?

##### Share on other sites
struct vec2f {  float x,y;  vec2f(float x,float y) : x(x),y(y) {}  vec2f() {}};

Then you can do all kinds of great things with operator overloading:
vec2f operator+(const vec2f & a,const vec2f & b){ vec2f r(a.x+b.x,a.y+b.y); return r;}

Stick them into a vector for more fun:
std::vector<vec2f> fun;fun.push_back(vec2f(1,2));fun.push_back(vec2f(2,3));

##### Share on other sites
Using a struct would probably be the best solution for you, but there is also a pair template. Check out an example here.

##### Share on other sites
So with those I deas how can I get a loop to fill them with the x and y coords then recall them to set them to a function like

for (y = 30;y <200; y+=30)
for ( x = 50;x <600; x+=50)

load the x and y values accordingly

draw(x,y);

whould I be able to use the stryct and vector to do this or would I need to do it another way. I just need to be able to fill in x and y and remove it when its been destroyed

##### Share on other sites

[Edited by - Kobo on May 6, 2008 3:10:14 PM]

##### Share on other sites
Quote:
 Original post by KoboIf you use a struct, you will probably want to typedef it.

There's no need to typedef a struct in C++

##### Share on other sites
   for (y = 30;y <200; y+=30)   for (x = 50;x <600; x+=50)     fun.push(vec2f(x,y));

##### Share on other sites
BTW if you're using DirectX (like your sig seems to indicate) there's really no reason not to use the D3DXVECTOR2 class.

##### Share on other sites
Quote:
 Original post by kingpinzsSo with those I deas how can I get a loop to fill them with the x and y coords then recall them to set them to a function likeload() for (y = 30;y <200; y+=30) for ( x = 50;x <600; x+=50) load the x and y values accordingly draw(x,y);whould I be able to use the stryct and vector to do this or would I need to do it another way. I just need to be able to fill in x and y and remove it when its been destroyed

You'd probably wouldn't want to increment y and x by anything other than 1. You'd want to specify some sort of algorithm that will generate the correct coordinates inside the for loops rather than trying to do that in the definition of the for loop. Something like this:

struct Coordinate { int x; int y;}for(int y = 0; y < 20; ++y) for(int x = 0; x < 20; ++x){  // However you want to handle these  int y_coord = y * 20;  int x_coord = x * 20;  some_array[y_coord][x_coord];  // Or  Coordinate coord;  coord.x = x * 20;  coord.y = y * 20;  some_vector.add(coord); // Or some other way of storing these coordinates  }

##### Share on other sites
Shakedown the issue to begin with is how to acsess

some_array[y_coord][x_coord];

the y_coord and x_coord so it would load into

draw(x,y)

I could never get it to work so I thought I was using the wrong data type.

Was I wrong thincking this?

If not how do I load y_coord to the y and the x_coord to the x?

Thanks for every ones replys it is helping me understand the problem better so I can find the answer or at least ask the better question.

##### Share on other sites
I think you are confusing the indices of an array with the values stored in an array.

You may need to brush up on arrays: http://www.cplusplus.com/doc/tutorial/arrays.html

some_array[y_coord][x_coord] "points" to a specific value.

int main(){   int some_array[2][2];   some_array[0][0] = 5;   some_array[0][1] = 10;   some_array[1][0] = 20;   some_array[1][1] = 30;   cout << some_array[1][0];  // prints '20'}

You don't get the x and y value from those indices - you access the data stored at that position in the array with them.

Say you use the structure I illustrated above:

struct Coord{   int x;   int y;};int main(){   Coord pos[50];   // Object #0   pos[0].x = 5;   pos[0].y = 5;   // Object #1   pos[1].x = 10;   pos[1].y = 10;   // ... populate all of the positions however you wish ...      // Draw all your objects at their position   for ( int i = 0; i < 50; i++ )   {     draw( pos.x, pos.y );   }   return 0;}

##### Share on other sites
Thanks CaspianB and Shakedown I took both your advice and came up with a way to store my x and y coords. next stop deleting the coords.

TO load the coordinatesfor(int i=0;i<8; i++)           {                for(int l =0;l <12;l++ )                   {                                                int y_coord = i * 30;                        int x_coord = l * 50;                      pos[l].x = x_coord;                      pos.y = y_coord  ;                                          }           }

Just for testing purposes

Reading the coordsfor(int i=0;i<8; i++)     {               for(int l = 0;l <12;l++ )              {                 cout<<pos.y<<" "<<pos[l].x<<endl;               }    }

##### Share on other sites
while I tryd this to remove the coords that I didnt want

void remove_coord(int x, int y){      for(int i=0;i<8; i++)           {                for(int l =0;l <12;l++ )                   {                     int y_coord = i * 30;                        int x_coord = l * 50;                    if((x_coord == x) && (y_coord == y))                     {                            pos[l].x = 0;                            pos.y = 0;                        }                   }           }}

but it turns all elments that == x to 0 even if the elments dont == y
and vice versa

why would that be?

##### Share on other sites
You still don't seem to understand how arrays or structures work and I would hazard a guess you don't quite grasp how nested loops work either.

I strongly suggest you go back and look into C++ array tutorials in addition to looking up a tutorial on C++ structures.

For starters, the whole purpose of using a structure (at least in this particular situation here) is to have one single dimension array of coordinates. You won't use nested loops.

You are trying to store the x and y values at completely separate indices (pos[l].x and pos.y). Ideally, you would have a single loop (not nested loops) and do something like I outlined in the post above:

for ( int i = 0; i < 50; i++ ){   pos.x = x_coord;   pos.y = y_coord;}

The point is to keep the x and y values together in the same array position.

I think, perhaps, you understand the basic concepts of arrays, but I'll try to make a short explanation anyway:

Example:
// Say you want 10 variables of an integer type, all the sameint num0;int num1;int num2;... // etcint num9;num0 = 5;num1 = 5;...num9 = 5;// Or you can just use arraysint num[10];for ( int i = 0; i < 10; i++ ){   num = 5;}

As I said, you may understand this concept. And I think you do. It's the mixing of structures in there that is throwing you off.

struct Coordinate{   int x;   int y;};int var;         // This makes a SINGLE variable of type 'integer'var = 5;var = 10;        // You can set it like thisCoordinate pos;  // This makes a SINGLE variable of type Coordinatepos.x = 10;      // Coordinate you defined above to hold two integers, x and ypos.y = 10;      // You set them like thisCoordinate posArray[10];  // This makes an ARRAY variable of type CoordinateposArray[0].x             // This accesses the array at position 0 for the x varposArray[0].y             // and accessing y

As I said, I very strongly suggest googling "C++ array tutorial" and "C++ structure tutorial" to get a stronger grip on these concepts.

##### Share on other sites
Do you understand what exactly you're doing? Because it looks like you're misunderstanding quite a few things.

// To load the coordinatesfor(int i = 0; i < 8; i++){	for(int l = 0; l < 12; l++)	{		int y_coord = i * 30;		int x_coord = l * 50;		pos[l].x = x_coord;		pos.y = y_coord;	}}

First of all, most of the time you're setting the coordinates of two different Coord structs! i is not the same as l, so pos[l] and pos do not refer to the same Coord instance. That's going to lead to quite some confusion later on (actually, it's already messing up your code). Just a note: avoid using i and l in the same scope, because they look too similar. It's easy to mess up with just a small type.
Second, you don't need the temporary x_coord and y_coord variabeles, because you can simply assign these values to pos.x and pos.y directly.

So, that piece of code would become:

for(int i = 0; i < 100; i++){	pos.x = i * 30;	pos.y = i * 50;}

Now onto 'removing' coordinates. I assume you want to reset the Coord instances that have the given x and y values? In that case, the following code would do the trick:

void removeCoord(int x, int y){	for(int i = 0; i < 100; i++)	{		if(pos.x == x && pos.y == y)		{			pos.x = 0;			pos.y = 0;		}	}}

##### Share on other sites
I dont hinck I stated the problem correctly thne.

heres what I need

x = 50 y = 30
x = 100 y = 30
..
x = 550 y = 30
x = 50 y = 60
x = 100 y = 60
..
x=550 y = 60
x = 50 y = 90

ect

so the only way I can thinck of to have them corsponde like that is to use a nested loop for every y there needs to be 11 x's

like columes and rows

they are to repersent the x and y coords of the bricks for my game

there is 12 across and 8 down

so when I look for x = 550 and y = 60 and removes those two at the same time then the brick gets deleted

unless my logic is flawed some how. I tryed on for loop and could not get the above resultes when tested

##### Share on other sites
In that case wouldn't it be easier to actually use an array of tile structs? You can always calculate the real coordinates from the array indices:

struct Tile{  bool   Exists;  DWORD  Color;  Tile() :    Exists( false ),    Color( 0xffffffff )  {  }};Tile     m_Tiles[12][8];

##### Share on other sites
In this case what you need is not a grid of possible bricks, instead a list of known bricks and where they are in your 12x8 grid.

You need to use either a vector or a list but in this example I'll use a vector (array essentially).

#include <vector> // This is our brick informationstruct Brick{	int x, y; // this is our grid X/Y (i.e. 12x8)	unsigned int color; // add a color to each brick}; // Our array of bricks in our 12x8 gridstd::vector <Brick> g_Bricks; // Load our bricks from a 2D arrayvoid LoadBricks (bool is_brick[12][8]){	// setup our loops thru i and j (x/y)	for (int i = 0; i < 12; i++)		for (int j = 0; j < 8; j++) {			if (is_brick[j]) {				Brick brick;				brick.x = i + 1; // add 1 to make our x index an x position from 1-12 not 0-11				brick.y = j + 1;				// set a color (could be randomised as well)				brick.color = 0xFFFF00; // yellow								// add our new brick to the array of bricks				g_Bricks.push_back (brick);			}		}} // Find out which brick in our array we may be hitting// x/y coordinates are in grid coordinates not pixel coordinatesint DoHitTest (int player_x, int player_y){	for (int i = 0; i < g_Bricks.size (); i++)	{		// if the brick we're testing is the same position as the player		// thats the brick we need to remove		if (g_Bricks.x == player_x && g_Bricks.y == player_y) {			// return the index of brick we found our player was hitting			return i;		}	}	// if we didn't hit any bricks lets return -1	return -1;} // Here we update the bricks by doing a hit test then removing the brickvoid UpdateBricks (int player_x, int player_y){	// find out if we hit a brick	int brick = DoHitTest (player_x, player_y); 	// if we did (not -1) remove it	if (brick != -1) {		// remember, DoHitTest returns the index in our array		// of the brick we hit so we can use this to remove it from the array		g_Bricks.erase (brick);	}}

##### Share on other sites
Well I think I should explain my theory behind the reason I am trying to do it this way.
for(int i=0;i<8; i++) // this makes the rows           {                for(int l =0;l <12;l++ ) //this makes the columns                    {                        pos.y = i * 30;                          pos[l].x = l * 50;                                                               // so the first time through it make y equal 30                       // and make x = 50 which to understand why I just                                   // need the upper left corner of the brick I have                      // the size already constant. So what I was                            // thinking is that I create parallel arrays. So                      // they both contain different values but are                      // always incrementing the same.   // pos[1].y == 30// pos[1].x == 50// pos[2].y == 30// Pos[2].x == 50   ..// pos[2].y == 60// pos[2].x == 50                }           }

So should my thoughts on using this array correct?

##### Share on other sites
You only have a single dimensional array. Single dimensional arrays do not have rows and columns *.

You want a two dimensional array. Or, better still, something looking like what some of the other posters suggested.

* Technically, you can access a single dimensional array as if it had rows and columns, but that is beyond you for the moment