What is the best way to store X and Y coords

Started by
25 comments, last by 3Dgonewild 16 years ago
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?
Advertisement
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?
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));

Using a struct would probably be the best solution for you, but there is also a pair template. Check out an example here.
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
Nevermind about typedef in c++

[Edited by - Kobo on May 6, 2008 3:10:14 PM]
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++

   for (y = 30;y <200; y+=30)   for (x = 50;x <600; x+=50)     fun.push(vec2f(x,y));
BTW if you're using DirectX (like your sig seems to indicate) there's really no reason not to use the D3DXVECTOR2 class.
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  }

This topic is closed to new replies.

Advertisement