Sign in to follow this  
kingpinzs

What is the best way to store X and Y coords

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 this post


Link to post
Share on other sites

struct Coord
{
int x;
int y;
}

// Access coordinates by pos[0].x or pos[0].y, etc
Coord 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 this post


Link to post
Share on other sites
How about using a struct?

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 this post


Link to post
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

load()
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 this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
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

load()
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 this post


Link to post
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 this post


Link to post
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[i].x, pos[i].y );
}

return 0;
}




Share this post


Link to post
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 coordinates

for(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[i].y = y_coord ;

}
}



Just for testing purposes


Reading the coords

for(int i=0;i<8; i++)
{

for(int l = 0;l <12;l++ )
{

cout<<pos[i].y<<" "<<pos[l].x<<endl;

}
}



Share this post


Link to post
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[i].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 this post


Link to post
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[i].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[i].x = x_coord;
pos[i].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 same
int num0;
int num1;
int num2;
... // etc
int num9;

num0 = 5;
num1 = 5;
...
num9 = 5;

// Or you can just use arrays
int num[10];
for ( int i = 0; i < 10; i++ )
{
num[i] = 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 this

Coordinate pos; // This makes a SINGLE variable of type Coordinate

pos.x = 10; // Coordinate you defined above to hold two integers, x and y
pos.y = 10; // You set them like this

Coordinate posArray[10]; // This makes an ARRAY variable of type Coordinate
posArray[0].x // This accesses the array at position 0 for the x var
posArray[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 this post


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

Let's look at the loading code:

// To load the coordinates

for(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[i].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[i] 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[i].x and pos[i].y directly.

So, that piece of code would become:

for(int i = 0; i < 100; i++)
{
pos[i].x = i * 30;
pos[i].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[i].x == x && pos[i].y == y)
{
pos[i].x = 0;
pos[i].y = 0;
}
}
}

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 information
struct 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 grid
std::vector <Brick> g_Bricks;

// Load our bricks from a 2D array
void 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[i][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 coordinates
int 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[i].x == player_x && g_Bricks[i].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 brick
void 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 this post


Link to post
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[i].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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this