#include <stddef.h>

/****************************class Point********************************/
class Point
{
  public:
     	double x;  //x coordinate of the point
	double y;  //y coordinate of the point

	//constructor, if arguments are not provided, default arguments 
  	//initialize the point to (0,0) 
      Point (double x_coordinat=0.0, double y_coordinate=0.0);  
	
	int operator==(Point&);   	//overload operator ==
	int operator!=(Point&);		//overload operator !=
	int operator<(Point&);		//overload operator <
	int operator>(Point&);		//overload operator >
        Point operator+(Point&);	//overload operator +
	Point operator-(Point&);	//overload operator -
 
	int classify(Point&,Point&);     //classify this point                                                     relative to a directed 
						//line segment p0---->p1
	//	int classify(Edge&);		//overload member function classify, pass an 
						//edge rather than a pair of points
      double length();		    	//return the length of vector
     
};




/*******************************class Node*********************************/
class Node 
{ 
   public:
	Node();			//constructor
 	virtual ~Node();		//virtual destructor
	Node *next();		//return pointer to the next node
	Node *previous();		//return pointer to the previous node
	Node *insert(Node *);	//insert node just after this node
	Node *remove();		//remove this node from its linked list
      void splice(Node *);	//splice two nodes a and b
   protected:
	Node *_next;		//link to successor node
	Node *_previous;		//link to predecessor node
};

 

/************ template class ListNode(derived from class Node)***********/
//   A node in a linked list is represented by an object of class ListNode, 
//which is derived from Class Node. Class ListNode possesses a data member 
//_value, which points to the actual item contained in node.

template <class T> class List;    //declaration of calss List because
                                  //class List is friend of class 
                                  //ListNode, and its definition
                                  //appears later.  
template <class T> class ListNode: public Node
{
   public:
	T _value;
	ListNode(T value);	//constructor
        friend class List<T>;
};

//constructor "ListNode" implicitly invokes the constructor for 
//base class Node, since constructor "Node" takes no argument.

template<class T> ListNode<T>::ListNode(T value): _value(value)
   { }

//   I will not define a destrucor for class ListNode. Whenever a 
//ListNodeabject os deleted, the virtual destructor "~Node" of 
//bass class "Node" is invoked. Note that the item pointed to by
//data member ListNode::_value is not deallocated. It would be 
//safe to deallocate the item only if it were known to have been 
//allocated with "new", but there is no guarantee of this.
	



/*************************template class List******************************/
//class List defines a circular doubly linked list, it has 3 data members:
//1. _length: number of nodes in this linked list
//2. header: pointer to the header node
//3. window: pointer to the node that the list's window is 
//		 currently positioned over

template<class T> class List
{
   private:
        int _length;
	ListNode<T> * header;
    	ListNode<T> * window;
   public:
	List();	//constructor
	~List();	//destructor
	T insert(T);
	T remove();
	T prepend(T);	//insert a new item at the beginning of the list 
	T append(T);	//insert a new item at the end of the list
	List *append(List*);	//overload function "append" to append a 
					//list to the end of this list
	void change_value(T);	//replace the item currently in the window by v 
	T value();		//return the item in the window
	T next();		//move the window to the next position
	T previous();	//move the window to the previous position
	T first();	  	//reset the window to the first position
	T last();		//reset the window to the last position
	int length();	//return the length of the list
	bool isHead();	//return true if window is in the head position
	bool isFirst();	//return true if window is in the first position
	bool isLast(); 	//return true if window is in the last position

};

//constructor creates and initializes an empty list, represented 
//by a single header node linked to itself
template<class T> List<T>::List():_length(0) 	
{
 	header=new ListNode<T> (NULL);
	window=header;
}

template<class T> List<T>::~List() 	//destructor
{   
	while (length()>0)
		{
		 first();
		 remove();
		}
	delete header;
}

//insert a new iem after the window and returns the item
template<class T> T List<T>::insert (T value)
{
 	window->insert(new ListNode<T>(value));
	++_length;
	return value;
}

//remove the item in the window, move the window tho the previous
//position, and returns the just-removed item. The operation does 
//nothing if the window is header.
template<class T> T List<T>::remove()
{
	if (window==header)
		return NULL;
	T value=window->_value;
	window=(ListNode<T> *)window->previous();
      delete (ListNode<T> *)window->next()->remove();
			//call member function "remove" of class Node 
			//for object window, returns a pointer to this 
			//node, then delete this node
	--_length;
	return value;
}
//insert a new item at the beginning of the list, then return 
//the new item  
template<class T> T List<T>::prepend(T value)
{
	header->insert(new ListNode<T>(value));
			//call member function "insert" of class Node
			//for object header
	++_length;
	return value;
}

//insert a new item at the end of the list, then return 
//the new item  
template<class T> T List<T>::append(T value)
{
	header->previous()->insert(new ListNode<T>(value));
			//call member function "insert" of class Node
			//for the node just before header(the last
			//node of the list
	++_length;
	return value;
}

template<class T> List<T>* List<T>::append(List<T> *list)
{
	ListNode<T> *a=(ListNode<T>*) header->previous();
        a->splice(list->header);
        _length += list->_length;
	list->header->remove();
	list->_length=0;
	list->window=header;
	return this;
}

template<class T> void List<T>::change_value(T v)
{
	if (window != header)
		window->_value=v;
}


//Accessing functions value, next, previous, first, last, length
template<class T> T List<T>::value() 	//return the item in the window,
							//or NULL if window is in head 
							//position
   { return window->value; }

//function "next" move the window to the next position, return the item 
//in the window's new position 
template<class T> T List<T>::next()		
{
	window=(ListNode<T>*)window->next();
	return window->_value;
}
  
//function "previous" move window to the provious position, return the item 
//in the window's new position 
template<class T> T List<T>::previous()
{
	window=(ListNode<T>*)window->previous();
	return window->_value;
}
  
//function "first" move the window to the first position, return the item 
//in the window's new position 

template<class T> T List<T>::first()
{
	window=(ListNode<T>*)header->next();
	return window->_value;
}
  
//function "last" move the window to the last position, return the item 
//in the window's new position 

template<class T> T List<T>::last()
{
	window=(ListNode<T>*)header->previous();
	return window->_value;
}
  

template<class T> int List<T>::length()	//return the length of the list.
   { return _length; }

template<class T> bool List<T>::isFirst() 	//return true if the window
							//is in the forst place
{
	return ((window == header->next()) && (_length>0));
}


template<class T> bool List<T>::isLast()	//return true if the window 
							//is in the last position 
{
	return ((window == header->previous()) && (_length>0));
}


template<class T> bool List<T>::isHead()	//return true if the window 
							//is in the head position
{
	return (window == header);
}



/**************************template class Stack******************************/
//I will presue an implementation of stacks based on the List class 
//template, then my stacks will then be dynamic since List objects 
//are dynamic.
//The class template Stack contains a private data member "s" which 
//points to the List object representing the stack. The list is 
//ordered from the top of the stack to hte bottom; in particular,
//the top item of the stack is in the first position of the list, 
//while the bottom item of the stck is in the last position of the 
//the list.

template<class T> class Stack
{
  public:
	Stack();		//constructor
	~Stack();		//destructor
	void push(T v);	//push item v onto this stack
	T pop();		//pop the topmost item from this stack
	int size();		//return the # of items on this stack
	T top();		//return the stack's top item
	T nextToTop();	//return the item just below the top item
	T bottom();		//return the bottom item
	bool empty();	//return true if and only if this stack is empty
   private:
	List<T> *s;
};

template<class T> Stack<T>::Stack():s(new List<T>) 	//constructor
{ }

template<class T> Stack<T>::~Stack()			//destructor
{
	delete s;
}

template<class T> void Stack<T>::push(T v)
{
	s->prepend(v);
}

template<class T> T Stack<T>::pop()
{
	s->first();
	return s->remove(); 	//return the popped item
}

template<class T> int Stack<T>::size()
{
	return s->length();
}

template<class T> T Stack<T>::top()
{
	return s->first();
}

template<class T> T Stack<T>::nextToTop()
{
	s->first();		//call member function "first" of 
				//class List for object s
	return s->next();	//call member function "next" of 
				//class List for object s
}

template<class T> T Stack<T>::bottom()
{
	return s->last();
}

template<class T> bool Stack<T>:: empty()
{
 	return (s->length()==0);
}



/*********class Vertex(derived from both classes Point and Node)**********/
//class Vertex is derived from both class Point and class Node.
//Vertex inherits data members _next and _previous from base class
//Node, and x and y from base class Point. By convention, _next 
//points to this vertex's successor(its clockwise neighbor), and 
//_previous to this vertex's predecessor(its counterclockwise neighbor.

enum {CLOCKWISE, COUNTER_CLOCKWISE};
class Vertex: public Node, public Point
{   
  public:
	Vertex(double x, double y);	//constructor
	Vertex(Point&);			//constructor
	Vertex *cw();	//return this vertex's successor(clockwise)
	Vertex *ccw();	//return this vertex's predecessor(counter-clockwise)
	Vertex *neighbor(int rotation); 	//return whichever neighbor 
							//specified by parameter rotation,
							//one of the enumeration values
							//CLOCKWISE or COUNTER_CLOCKWISE	
 	Vertex *insert(Vertex *);	//call member function of base class Node
	Vertex *remove();			//call member function of base class Node
	void splice(Vertex *);		//call member function of base class Node
	Vertex *split(Vertex *);
	Point point();	//return the point in the plane where this vertex lies
	friend class Polygon;
};





/***************************class Edge*******************************/
class Edge
{
   public:
	Point org;		//origin point
 	Point dest;		//destination point
	Edge();		//constructor
	Edge(Point & _org, Point & _dest);	//constructor
	bool isVertical();	//return true if edge is vertical
        double slope();		//return the slope of the edge
	double find_y(double);  //find y for given x on the infinite line 
					//including this edge
};


/******************************class Polygon******************************/
class Polygon
{
   public:
//constructors and destructor
	Polygon();			//constructor, initialize an empty polygon
	Polygon(Polygon &);	//copy constructor, performs a deep copy
	Polygon(Vertex *);	//constructor, initialize with a circular 
  					//doubly linked list of vertices
	~Polygon();			//destructor
//access functions
	Vertex *currentVertex(); 	//return this polygon's current vertex
  	int size();				//return this polygon's size
	Point point();	//return the point where the curent vertex lies 
	Edge edge();	//return the current edge
	Vertex *cw();	//return current vertex's successor
	Vertex *ccw();	//return current vertex's predecessor
	Vertex *neighbor(int rotation);	//return current vertex's successor 
							//or predecessor depending on the 
							//value of rotation
//update functions
	Vertex *advance(int rotation);
	Vertex *setV(Vertex *);
	Vertex *insert(Point &);
  	void remove(); 
	Polygon *split(Vertex *);
   private:
	Vertex* _v;		//pointer to current vertex(vertex in the window)
	int _size;        //size of the polygon
	void resize();
};


/******************base class DifferentTriangulation******************/



class DifferentTriangulation     //it is an abstract class
{ 
   public:
	virtual  List<Polygon*> *triangulateMonoPolygon(Polygon &);
};


/******************derived class Triangulation_1.h********************/
class Triangulation_1 :public DifferentTriangulation
{
   public:
	virtual List<Polygon*> *triangulateMonoPolygon(Polygon &);
};



// declare the external function  extfunc_of_triangulation()
extern List<Polygon*>* extfunc_of_triangulation(DifferentTriangulation &, Polygon &); 


