Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

What is the best way to store X and Y coords

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

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Nevermind about typedef in c++

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

Share this post


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


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!