Sign in to follow this  

Coordinate Container Class

This topic is 4307 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

In the little tetris clone I'm building, I've been using the STL std::vector<int> to store lists of X-Y coordinate pairs so far (storing block shapes, etc.). Unless I'm mistaken (and correct me if I'm wrong here, please), sets and maps are not a good solution for this because they automagically sort new entries into the list. So, I've just come to the point in the code where I'm going to need to code in motion of the blocks, and I realized that it would be much more efficient if I created a custom template container class which derives from vector and stores pairs of values. That way, you could just code something like
Coord offset  ( 0, 1),
      Position( 3, 4);

Position+=offset;
// Position is now (3,5)



with overloaded addition and subtraction operators and save yourself the overhead of typing the for loop to add them together every time. Now, I'm sure this is something that's been done before for both 2d and 3d (and better than my meager efforts can come up with). Is there something like this in the STL or somewhere similar that my beginner book isn't listing? Thanks, [Edited by - enigma555 on March 3, 2006 1:14:57 AM]

Share this post


Link to post
Share on other sites
... and if there isn't something readily available, can someone take a look at the header file I put together? I think all the functions are fairly evident from the comments.


/* X-Y Coordinate
Container Template Class
XY.h
my name */


#ifndef XY_H
#define XY_H

#include <vector>
#include <algorithm>

template< class T >
class XY
{
public:
// Constructors
XY( T = 0, T = 0 );
XY( std::vector<T> & );
XY( const XY<T> & );

// Overloaded additive operators
XY operator+( const XY<T> & );
XY operator-( const XY<T> & );

XY &operator+=( const XY<T> & );
XY &operator-=( const XY<T> & );

// Overloaded comparison operators
bool operator==( const XY<T> & ) const;
bool operator!=( const XY<T> & ) const;

// Get functions

XY<T> GetXY() const;
XY<T> GetCoords() const;

T GetX() const;
T GetY() const;

T Get1st() const;
T Get2nd() const;

// Set functions

// Set both values entering a 2 element vector
bool SetXY( const std::vector<T> );
bool SetCoords( const std::vector<T> );

// Set first value from type <T>
void SetX( const T );
void Set1st( const T );

// Match first value from input coordinate
void SetX( const XY<T> & );
void Set1st( const XY<T> & );

// Set second value from type <T>
void SetY( const T );
void Set2nd( const T );

// Match second value from input coordinate
void SetY( const XY<T> & );
void Set2nd( const XY<T> & );

private:
std::vector< T > Coords;

};

#endif






Anything I should add or am doing inefficiently? I doubled up the set and get functions to help avoid confusion if working in a different coordinate system (like the row/column coord system I'm using for the tetris clone).

[Edited by - enigma555 on March 3, 2006 1:34:33 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by enigma555
... and if there isn't something readily available, can someone take a look at the header file I put together? I think all the functions are fairly evident from the comments.

*** Source Snippet Removed ***

Anything I should add or am doing inefficiently? I doubled up the set and get functions to help avoid confusion if working in a different coordinate system (like the row/column coord system I'm using for the tetris clone).
Basically you're implementing what's usually called a 'vector' class. (Just to avoid confusion, such classes are different than the stl vector in that they deal with a vector of values in a geometric sense.) There are countless such classes out there; just about everyone has written his or her own, and they can be found online in various libraries and in professional source code (try the Doom 3 sdk, for example). A google for 'vector class' should turn up quite a few hits.

Your class will probably work (especially for a simple Tetris game), but there are various issues with nomenclature and efficiency that you'll want to address if you use it for anything more demanding. I think the easiest thing to do would be to look around a bit and see what other people have done, which I'm guessing will give you many ideas for possible improvements (IMO).

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
I think the easiest thing to do would be to look around a bit and see what other people have done, which I'm guessing will give you many ideas for possible improvements (IMO).


One of which would possibly re-thinking making it a template class. Because then, if I wanted to, I could make an XY<std::list<int> > object.

If you want to keep it that way, no problem since at this stage you'll probably be the only one using it. Personally I've kept my Vector3d class just floats. You could always leave the class as a template and then just make a couple of typedefs.

typedef XY<float> XYf;
typedef XY<int> XYi;

Or something like that.

</post>

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar

One of which would possibly re-thinking making it a template class. Because then, if I wanted to, I could make an XY<std::list<int> > object.



I actually was going to create it as an <int> and turned it into a template so I could easily port it to <float>. There are only specific locations that the blocks can hold, so <int> seems more natural to me.

Thanks for your comments, guys. I'll rummage around and see what others have done.

Share this post


Link to post
Share on other sites

This topic is 4307 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.

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