 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
qwerty Guest
|
Posted: Sat Feb 25, 2006 11:06 pm Post subject: Help: Can't access more than one element in class |
|
|
Hello,
I'm having problems accessing elements in the polygon class every time
I try to move the iterator it stays at the same element...
code:
#include <iostream>
using namespace std;
class Vertex
{
public:
Vertex(int x=0, int y=0, Vertex *n = NULL, Vertex *p=NULL)
{xcoord = x; ycoord=y; next=n; prev=p;}
int getx() {return xcoord;}
int gety() {return ycoord;}
void setx(int a) {xcoord = a;}
void sety(int b) {ycoord = b;}
private:
int xcoord, ycoord;
Vertex *next, *prev;
friend class Polygon;
};
class Polygon
{
public:
Polygon() {header = NULL; currItr=NULL;}
bool isEmpty(){return (header == NULL);}
void reset(){currItr = header;}
void forward(){if (currItr) {currItr = currItr->next;}}
void backward(){if (currItr) {currItr = currItr->prev;}}
void insertAfter(int x, int y);
void insertBefore(int x, int y){backward(); insertAfter(x,
y);}
void readPolygon();
void printPolygon();
double perimeter();
double area();
int findLeftmostxcoord();
int rindRightmostxcoord();
int findTopmostycoord();
int findBottommostycoord();
bool leftTurn();
bool isConvex();
private:
Vertex *header;
Vertex *currItr;
};
int main()
{
Polygon one, two;
one.readPolygon();
two.readPolygon();
cout<<endl;
one.printPolygon();
return 0;
}
void Polygon::readPolygon()
{
int x1, y1, n;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>x1>>y1;
// cout<<x1<<" "<<y1<<" ";
insertAfter(x1, y1);
forward();
}
}
void Polygon::insertAfter(int x, int y)
{
Vertex *ptr = new Vertex(x, y, NULL, NULL);
//cout<<x<<" "<<y<<" ";
if (isEmpty())
{
ptr->prev = ptr;
ptr->next = ptr;
currItr = ptr;
header = ptr;
}
else
{
ptr->prev = currItr;
ptr->next = currItr->next;
currItr->next;
}
}
double Polygon::perimeter() //Find the perimeter of a polygon
{
Vertex *ptr1 = currItr;
double perm = 0.0;
double x1 = ptr1->getx();
double x2 = ptr1->next->getx();
double y1 = ptr1->gety();
double y2 = ptr1->next->gety();
double distance;
distance = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
perm = perm + distance;
return perm;
}
bool Polygon::leftTurn()
{
Vertex *ptr1 = currItr;
int x1 = ptr1->getx();
int x2 = ptr1->next->getx();
int x3 = ptr1->next->next->getx();
int y1 = ptr1->gety();
int y2 = ptr1->next->gety();
int y3 = ptr1->next->next->gety();
double a = x1*y2-y1*x2+y1*x3-x1*y3+x2*y3-x3*y2;
return(a>0);
}
void Polygon::printPolygon()
{
Vertex *itr = currItr;
cout<<itr->getx()<<" "<<itr->gety()<<endl;
cout<<itr->next->next->next->getx()<<"
"<<itr->next->next->next->gety()<<endl;
} |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Feb 26, 2006 12:06 am Post subject: Re: Can't access more than one element in class |
|
|
qwerty wrote:
| Quote: | I'm having problems accessing elements in the polygon class every time
I try to move the iterator it stays at the same element...
code:
[..]
|
You're not trying "to move the iterator". "Moving" the iterator would
actually mean changing its value. Something like
iter++
or
iter = iter->next;
V
--
Please remove capital As from my address when replying by mail |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|