#include "total.h"


/*********************class Point****************************/
//constructor
Point::Point(double x_coordinate, double y_coordinate):x(x_coordinate),y(y_coordinate)
{ }

//relational operators "==", "!=", ">", "<"
int Point::operator==(Point &p)   	//determine whether 2 points are equivalent
   { return (x==p.x)&&(y==p.y); }

int Point::operator !=(Point &p)
   { return !(*this==p); }

//operator "<", ">" give the lexicographic order relation in which point a is 
//less than point b if either:
//(1)a.x<b.x
//(2)a.x=b.x and a.y<b.y

int Point::operator < (Point &p)
   { return ((x<p.x)||((x==p.x)&&(y<p.y))); }

int Point::operator > (Point &p)
   { return ((x>p.x)||((x==p.x)&&(y>p.y))); }


//vector arithmatic operators +, -, and scalar-vector product operator *
Point Point::operator +(Point &p)
   { return Point(x+p.x,y+p.y); } 

Point Point::operator -(Point &p)
   { return Point(x-p.x,y-p.y); }

//scalar-vector product operator * is made a friend of class Point because 
//the first operand is not of type Point.
Point operator *(double n, Point &p)
   { return Point(n*p.x,n*p.y); }

//Point-line classification
//Now we classify a point relative to a directed line segment:
//
//		  origin----------->destination
//  (behind)         (between)             (beyond)      
//
//The directed line segment effectively partitions the plane into 
//7 non-overlaping regions: 
//1. origin point
//2. destination point 
//3. left of the directed line segment
//4. right of the directed line segment
//5. on the line, behind origin point
//6. on the line, betwween origin and destination points
//7. on the line, beyond the destination point
 
enum{LEFT,RIGHT,BEYOND,BEHIND,BETWEEN,ORIGIN,DESTINATION};
int Point::classify(Point &p0,Point &p1)	//classify this point relative to 
							//the directed line segment
							//p0---->p1
  {
    	Point p2=*this;
    	Point m=p1-p0;
    	Point n=p2-p0;
 	double SignedArea=m.x*n.y-n.x*m.y;
 	if (SignedArea>0.0)
		return LEFT;
	if (SignedArea<0.0)
		return RIGHT;
	if ((m.x*n.x<0.0)||(m.y*n.y<0.0))
		return BEHIND;
	if (m.length()<n.length())
		return BEYOND;
	if (p0==p2)
		return ORIGIN;
	if (p1==p2)
		return DESTINATION;
  	return BETWEEN;
  }

//The second version of member function "classify" passes an edge 
//rather than a pair of points.
//int Point::clssify(Edge &edge);
//	{ return classify(edge.org,edge.dest); }
//    	
double Point::length()
	{ return sqrt(x*x+y*y); }


/******************class Node************************/
Node::Node():_next(this),_previous(this)	//constructor create a new node which 
							//doubly linkes to itself. The new node 
							//represents a single node linked list.
   { }

Node::~Node()	//virtual destructor, objects of derived class will be  
			//correctly deallocated.	
   { }

Node *Node::next()	//return pointer to the next node
   { return _next; }

Node *Node::previous() 	//return pointer to the previous node
   { return _previous; }

Node *Node::insert(Node *n)	//insert node just after this node 
   {
	Node *c=_next;
	n->_next=c;
	n->_previous=this;
	_next=n;
	c->_previous=n;
	return n;
   }

Node *Node::remove()		//remove this node from its linked list
   {
	_previous->_next=_next;
	_next->_previous=_previous;
	_next=_previous=this;
	return this;		//return a pointer to this node, 
					//so it can be deallocated later.
   }


//function "splice" is used to splice two node a and b. If a and b 
//belong to the same linked list, the linked list is splitted into 
//two smaller linked lists; if they belong to different linked lists,
//the two linked lists are joined into one larger linked list.   
void Node::splice(Node *b)
{
	Node *a=this;	//this node plays the role of node a
	Node *an=a->_next;
	Node *bn=b->_next;
	a->_next=bn;
	b->_next=an;
 	an->_previous=b;
	bn->_previous=a;
}


