Q
qwerty
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:erimeter() //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:rintPolygon()
{
Vertex *itr = currItr;
cout<<itr->getx()<<" "<<itr->gety()<<endl;
cout<<itr->next->next->next->getx()<<"
"<<itr->next->next->next->gety()<<endl;
}
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:erimeter() //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:rintPolygon()
{
Vertex *itr = currItr;
cout<<itr->getx()<<" "<<itr->gety()<<endl;
cout<<itr->next->next->next->getx()<<"
"<<itr->next->next->next->gety()<<endl;
}