印度阿三数据结构——链表——凸包

struct POINT{
	POINT(double x, double y) :x(x),y(y){}
	POINT(){}
	typedef double coordinate;
	coordinate x = 0;
	coordinate y = 0;
	double distance=0;
	double cosine=1;
};

ostream& operator<<(ostream& os,POINT p){
	os << "(" << p.x << ',' << p.y << ")" << std::ends;
	return os;
}


bool compareX(Dnode*left, Dnode*right){
	return left->data.x > right->data.x;
}

bool compareY(Dnode*left, Dnode*right){
	return left->data.y > right->data.y;
}

bool compareD(Dnode*left, Dnode*right){
	return left->data.distance > right->data.distance;
}
bool compareC(Dnode*left, Dnode*right){
	return left->data.cosine < right->data.cosine;
}



class CONVEX_POLYGON
{
public:
	typedef doublyCircularList list;
	CONVEX_POLYGON(){}
	CONVEX_POLYGON(std::initializer_list);
	templateCONVEX_POLYGON(const POINT(&)[i]);
	CONVEX_POLYGON(POINT*,int);
	//CONVEX_POLYGON(const CONVEX_POLYGON&);
	~CONVEX_POLYGON(){}

	void output(){ chain.output(std::cout); }
	void sort();

private:
	
	list chain;
	Dnode* P0,* P1,* P2, head;
};

CONVEX_POLYGON::CONVEX_POLYGON(std::initializer_listinit){
	for (const auto& iter : init){
		chain.insert(iter, 0);
	}
}

templateCONVEX_POLYGON::CONVEX_POLYGON(const POINT(&arr)[len]){
	for (const auto& a : arr){
		chain.insert(a, 0);
	}
}

CONVEX_POLYGON::CONVEX_POLYGON(POINT* p, int len){
	while (len-- != 0){
		chain.insert(*(p + len - 1), 0);
	}
}



void CONVEX_POLYGON::sort(){
	chain.sort(compareX);
	chain.sort(compareY);
	auto headNode=chain.findIndex(0);
	double x = headNode->data.x, y = headNode->data.y;
	for (int i = 1; i != chain.size(); ++i){
		headNode = headNode->next;
		headNode->data.distance = std::sqrt((headNode->data.x - x)*(headNode->data.x - x) + (headNode->data.y - y)*(headNode->data.y - y));
		headNode->data.cosine = (headNode->data.x - x) / (headNode->data.distance);
	}
	chain.sort(compareD);
	chain.sort(compareC);
	P0=headNode = chain.findIndex(0);
	P1 = P0->next; P2 = P1->next;
	
	while (P1 != headNode){
		if ((P2->data.x - P1->data.x)*(P0->data.y - P1->data.y) - (P2->data.y - P1->data.y)*(P0->data.x - P1->data.x) < 0){
			P0->next = P1->next;
			P2->previous = P1->previous;
			delete P1;
			--chain.length;
			if (P0 == headNode){
				P1 = P2; P2 = P2->next;
			}
			else{
				P1 = P0;
				P0 = P0->previous;
			}
		}
		else {
			P0 = P1; P1 = P2; P2 = P2->next;
		}
	}

}


你可能感兴趣的:(印度阿三数据结构——链表——凸包)