#include "total.h"

/******************************class Vertex*******************************/
Vertex::Vertex(double x, double y):Point(x,y)	//constructor
{ }

Vertex::Vertex(Point &p):Point(p)			//constructor
{ }

Vertex *Vertex::cw()	//return this vertex's successor
{ return (Vertex*)_next; }


Vertex *Vertex::ccw()	//return this vertex's predecessor
{ return (Vertex*)_previous; }


Vertex *Vertex::neighbor(int rotation)  	//return whichever neighbor is 
							//specified by parameter rotation
{
	return ((rotation==CLOCKWISE)? cw():ccw());
}


Vertex *Vertex::insert(Vertex *v)
{ return (Vertex *)(Node::insert(v)); }

Vertex *Vertex::remove()
{ return (Vertex *)(Node::remove()); }

void  Vertex::splice(Vertex *v)
{ Node::splice(v); }

Point Vertex::point()
{ return *((Point*)this); }


//function Vertex::split partitions a polygon along the chord connecting 
//this vertex to vertex b. It returns a pointer to vertex bp, the 
//duplicate of b.
Vertex *Vertex::split(Vertex *b)
{
//insert bp before vertex b
	Vertex *bp=b->ccw()->insert(new Vertex(b->point()));
//insert ap after this vertex
	insert(new Vertex(point()));
	splice(bp);
	return bp;
}


/**************************class Edge******************************/
#define INFINITY 1.0e20 

Edge::Edge(Point &_org,Point &_dest):org(_org), dest(_dest)	  //constructor
{ }

Edge::Edge():org(Point(0,0)),dest(Point(1,0))	//constructor
{ }

bool Edge:: isVertical()
{ return (org.x == dest.x); }

double Edge::slope()
{
	if (org.x!=dest.x)
		return (dest.y-org.y)/(dest.x-org.x);
	return INFINITY;		//return INFINITY if the edge is vertical
}

double Edge::find_y(double x)		//return the value y such that (x,y)
						//is a point on this infinite line.
{ return slope()*(x-org.x)+org.y; }


/*************************class Polygon******************************/
 //constructors and destructors
Polygon::Polygon():_v(NULL), _size(0) 
{ }

Polygon::Polygon(Polygon &p)  	//copy constructor
{
	_size=p._size;
	if(_size==0)
		_v=NULL;
	else
		{
		_v=new Vertex(p.point());
		for(int i=1;i<_size;i++)
			{p.advance(CLOCKWISE);
			 _v=_v->insert(new Vertex(p.point()));
                  }
		p.advance(CLOCKWISE);
		_v=_v->cw();
	}
}     



void Polygon::resize(void)	//resize is called whenever a vertex chain 
					//of unknown length is added to or removed 
					//from a polygon.
{
	if (_v==NULL)
		_size=0;
	else
		{
	         Vertex *v=_v->cw();
		 for(_size=1; v!=_v; ++_size, v=v->cw())
			{ }
		}
}


Polygon::Polygon(Vertex *v):_v(v) 	//constructor
{ resize(); }
				


Polygon::~Polygon()	//destructor
{
	if(_v)
	{
		Vertex *w=_v->cw();
		while(_v!=w)
		{
			delete w->remove();	//call member function "remove" 
							//of class Node for vertex w 
			w=_v->cw();
    		}
	delete _v;
	}
}

//access functions

Vertex *Polygon::currentVertex()
{ return _v; }

int Polygon::size()	//return this polygon's size
{ return _size; }

Point Polygon::point()	//return the point where the current vertex lies
{ return _v->point(); }

Edge Polygon::edge()	//return the current edge which originates at the 
				//current vertex and terminates at the current
				//vertex's successor.
{ return Edge(point(), _v->cw()->point()); }

Vertex *Polygon::cw()	//return the current vertex's successor
				//without moving the window
{ return _v->cw(); }

Vertex *Polygon::ccw()	//return the current vertex's predecessor
				//without moving the window
{ return _v->ccw(); }

Vertex *Polygon::neighbor(int rotation)	//return the current vertex's 
							//successor or predecessor, as
							//specified by the argument
{ return _v->neighbor(rotation); }


//update functions

Vertex *Polygon::advance(int rotation)	//move window to the current 
                                          //vertex's successor or predecessor, 
							//as specified by the argument
{ return _v=_v->neighbor(rotation); }

Vertex *Polygon::setV(Vertex *v)
{ return _v=v; }

Vertex *Polygon::insert(Point &p)	//insert a new vertex after the current 
						//vertex and then moves window over the 
						//new vertex
{
	if(_size++ == 0)
		_v=new Vertex(p);
	else
		_v=_v->insert(new Vertex(p));
	return _v;
}

void Polygon::remove()   	//remove the current vertex, the window is moved 
					//over the predecessor
{
	Vertex *v=_v;
	_v=(--_size == 0)? NULL:_v->ccw();
	delete v->remove();
}

Polygon *Polygon::split(Vertex *b)	//split this polygon along the chord 
						//connecting its current vertex to vertex 
						//b, retuns a pointer to the new polygon, 
						//whose window is placed over bp, the 
						//duplicate of b. This polygon's window
						//is not moved.
{
	Vertex *bp=_v->split(b);
	resize();
	return new Polygon(bp);
}


