class Polygon suggestions? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

class Polygon suggestions?

I've been working on a game idea for awhile now and I'm having difficulty with makeing and drawing polygons. I've had success with what I made, but the foundation is weak. Basically, the NODE holds the information, the Edge will point to two known NODEs and for future development the FACE will point to three EDGES.

/***************/
//From file Polygon.h
class Polygon
{
public:
struct Nodes
{
double x;
double y;
double z;
Nodes( double x0 = 0, double y0 = 0, double z0 = 0 ): x(x0), y(y0), z(z0) {}
};
struct Edges
{
Nodes * start;
Nodes * end;
Edges( Nodes * a = NULL, Nodes * b = NULL ): start(a), end(b) {}
};
public:
Polygon( Nodes * n = NULL, int n_num = 0, Edges * e = NULL, int e_num = 0 ): nodes(n), nodes_num(n_num), edges(e), edges_num(e_num) {}
~Polygon();
void draw( Monitor * m );

Nodes addNode( const Nodes & n );
Edges addEdge( const Edges & e );
void subNode( Nodes & n );
void subEdge( Edges * e );

void setNodes( Nodes * x, int a );
void setEdges( Edges * x, int a );

Nodes * getNodes() { return nodes; }
Edges * getEdges() { return edges; }
int getNodesNum() { return nodes_num; }
int getEdgesNum() { return edges_num; }

private:
Nodes * nodes;
int nodes_num;
Edges * edges;
int edges_num;
};

/************/
//From file Polygon.cpp

#include "Polygon.h"

Polygon::~Polygon()
{
delete [] nodes;
delete [] edges;
}


void Polygon::draw( Monitor * m )
{
for( int temp = 0; temp < edges_num; temp++ )
m->DrawLine( int( edges[temp].start->x ), int( edges[temp].start->y ),
int( edges[temp].end->x ), int( edges[temp].end->y ),
WHITE );
}

Polygon::Nodes Polygon::addNode( const Nodes & n )
{
Nodes * temp = new Nodes[ getNodesNum() + 1 ];

int a = 0;
for( a = 0; a < getNodesNum(); a++ )
{
temp[a] = getNodes()[a];
}
temp[a] = n;
setNodes( temp, getNodesNum() + 1 );

return temp[a];

}

Polygon::Edges Polygon::addEdge( const Edges & e )
{
Edges * temp = new Edges[ getEdgesNum() + 1 ];
int a = 0;
for( a = 0; a < getEdgesNum(); a++ )
temp[a] = getEdges()[a];
temp[a] = e;

setEdges( temp, getEdgesNum() + 1 );

return *temp;
}


void Polygon::subNode( Nodes & n )
{

}
void Polygon::subEdge( Edges * e )
{

}



void Polygon::setNodes( Nodes * x, int a )
{
delete [] nodes;
nodes = x;
nodes_num = a;
}
void Polygon::setEdges( Edges * x, int a )
{
delete [] edges;
edges = x;
edges_num = a;
}


Any suggestions has to what I might be doing wrong? Or maybe changes to make it better? The more universal the better.

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    Sure.

    It looks as if you've coded everything by hand. If you want to consider using classes designed by experts, you might want to follow the following advices:
    - Your Node class has the same functionality as boost::tuple. Why not use that one? If you do, every expert program immediately understands your node class
    - You manage the memory allocation of Node manually. Why not use a std::vector ? It at least saves you the headaches of memory management (by the way, you have made errors you are not aware of yet there (think of what would happen if you copy a Polygon and one of these would go out of scope) ) (by the way, it will also improve the speed of your program, as your Polygon::addNode is programmed more naive then std::vector::push_back).

    See ya, Bilderbikkel



    Good luck,
    bilderbikkel
  • MadifierMadifier Posts: 10Member
    Thanks for the info, I'll take a look at it. I've had a hard time in the past with implementing other people's code into my own. That's why I write all my own stuff. Still... I'm making progress.
Sign In or Register to comment.