Public Group

# 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.

## 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  }

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633759
• Total Posts
3013715
×